user1309226
user1309226

Reputation: 759

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?

Upvotes: 0

Views: 212

Answers (3)

Himanshu Moradiya
Himanshu Moradiya

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

KKRocks
KKRocks

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

nynohu
nynohu

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

Related Questions