Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 915
hanumanDev Avatar asked May 17 '11 14:05

hanumanDev


1 Answers

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];
like image 108
Grady Player Avatar answered Oct 31 '22 21:10

Grady Player