Search code examples
iosobjective-ciphoneuibuttonunrecognized-selector

Unrecognized selector sent to instance error message from UIButton


I have a UIButton that is added to a tableview programmatically. The problem is that when it is touched I run into the unrecognized selector sent to instance error message.

    UIButton *alertButton = [UIButton buttonWithType:UIButtonTypeInfoDark];     
    [alertButton addTarget:self.tableView action:@selector(showAlert:) 
          forControlEvents:UIControlEventTouchUpInside];
    alertButton.frame = CGRectMake(220.0, 20.0, 160.0, 40.0);

    [self.tableView addSubview:alertButton];

and here's the alert method which I want to trigger when the InfoDark UIButton is touched:

- (void) showAlert {
        UIAlertView *alert = 
         [[UIAlertView alloc] initWithTitle:@"My App" 
                                    message: @"Welcome to ******. \n\nSome Message........" 
                                   delegate:nil 
                          cancelButtonTitle:@"Dismiss" 
                          otherButtonTitles:nil];
        [alert show];
        [alert release];
}

thanks for any help.


Solution

  • Ok you have two problems. one is the selector issue as stated above, but your real problem is:

    [alertButton addTarget:self.tableView 
                    action:@selector(showAlert:) 
          forControlEvents:UIControlEventTouchUpInside];
    

    This is the wrong target, unless you have subclassed UITableView to respond to the alert.

    you want to change that code to:

    [alertButton addTarget:self 
                    action:@selector(showAlert) 
          forControlEvents:UIControlEventTouchUpInside];