Reputation: 759
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?
Upvotes: 0
Views: 212
Reputation: 4815
[Button addTarget:self action:@selector(aMethod:) forControlEvents: UIControlEventTouchUpInside];
-(void)aMethod:(UIButton*)sender
{
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.TABLENAME];
NSIndexPath *indexPath = [self.TABLENAME indexPathForRowAtPoint:buttonPosition];
NSLog(@"%@",indexPath);
}
Upvotes: 0
Reputation: 8322
Replace your following line of code
[button addTarget:self action:@selector(showSomething) forControlEvents:UIControlEventTouchUpInside];
With
[button addTarget:self action:@selector(showSomething:) forControlEvents:UIControlEventTouchUpInside];
Upvotes: 3
Reputation: 1658
I think the problem is in here
[button addTarget:self action:@selector(showSomething) forControlEvents:UIControlEventTouchUpInside];
Change to
[button addTarget:self action:@selector(showSomething:) forControlEvents:UIControlEventTouchUpInside];
Upvotes: 1