user1309226 user1309226 - 1 month ago 9
Objective-C Question

Attempting to handle tap event from UIButton in UITableView cell causes invalid selector error

I am attempting to handle the tap event of a UIButton in a UITableView cell but unfortunately the app crashes with the error message:

unrecognized selector sent to instance 0x7f95984cbaf0


This is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// this is a simplified version of this method for brevity.
NSMutableArray* views = [NSMutableArray array];

UIButton* button = [UIButton buttonWithThinBorderedColor_QAN:[UIColor colorForPrimaryUse]
highlightedColor:nil
disabledColor:nil];
[button addTarget:self action:@selector(showSomething) forControlEvents:UIControlEventTouchUpInside];

button.tag = indexPath.row;
[views addObject:button];
[cell setContents:views withConfiguration:configuration];
return cell;
}


In myViewController.h (simplified version) I have:

@interface SEFSFormEditingViewController : UITableViewController

- (void) showSomething;

@end


In myViewController.m I have:

-(void)showSomething:(UIButton *)sender
{
if (sender.tag == 0) {
NSString* test= @"test";
}
}


When I run the app the button is correctly displayed but when I tap it the app crashes.

Could someone please point me to the right direction on to fix this problem?

Answer

Replace your following line of code

[button addTarget:self action:@selector(showSomething) forControlEvents:UIControlEventTouchUpInside];

With

[button addTarget:self action:@selector(showSomething:) forControlEvents:UIControlEventTouchUpInside];
Comments