开发者

I have a UISwtch embedded in a UITableViewCell defined in a XIB file. How can I respond to a change to the switch state?

I built the cell with Interface Builder. I load the cells like this:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"MassCircleNGTableCell" owner: self options: nil];
    cell = circleNGCell;
    self.circleNGCell = nil;
}   
UISwitch *s = (UISwitch*)[cell.contentView viewWithTag: 20];
UILabel  *label = (UILabel*)[cell.contentView viewWithTag:19];
label.text = @"some useful text";

And that part works, I get my ta开发者_运维知识库ble with custom cells. But, although I can set the initial state, I don't know how to respond to the user flipping the switch! I tried this:

[leftSwitch addTarget:self action:@selector(setCircle) forControlEvents:UIControlEventTouchUpInside];

But the app crashes with "[MassCircleNGViewController setCircle]: unrecognized selector sent to instance 0xdd024b0'"

Any ideas out there?

[Added Later (as requested)]:

// implementation of [MassCircleNGViewController setCircle] really just a stub here
- (IBAction) setCircle: (id) sender {
    NSLog(@"setCircle sender == %@", (UISwitch*)sender);
}

The real problem was how I was trying to add the target. Need that colon after the selector name in the argument list!

[leftSwitch addTarget:self action:@selector(setCircle:) forControlEvents:UIControlEventValueChanged];


Okay, I found the problem. Where I had done this:

 [leftSwitch addTarget:self action:@selector(setCircle) forControlEvents:UIControlEventTouchUpInside];

I needed to add a COLON after the selector name to get this:

 [leftSwitch addTarget:self action:@selector(setCircle:) forControlEvents:UIControlEventTouchUpInside];

I know I saw a comment about doing that somewhere in these fora, but it took a couple of hours to sink in, and I have forgotten where I saw it. Oh, I did find a Very Nice Tutorial on XIB based custom UITableViewCells here


There may be 1 of these problems:

1/ Do you have a method called setCircle inside your UITableViewCell, MassCircleNGViewController

2/ Double check all the release. Maybe your UITableViewCell is released when the method is invoked and another object in the same memory area now

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜