Unrecognized selector sent to UIButton instance error message

I have a UIButton that is programmatically added to a tableview. The problem is that when I touch, I start an unrecognized selector sent to the instance with an 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 is the warning method that I want to call when touching UIButton InfoDark:

- (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.

+5
source share
3 answers

You have two problems. one of them is the selection problem as stated above, but your real problem is:

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

This is the wrong target if you did not subclass the UITableView to respond to the warning.

:

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

Crash: showAlert - (void) showAlert:(id) sender.

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

:

(:) selector addTarget, . @selector (buttonTouched:), . .

+5

, , .

, :

@selector( showAlert: )

The colon (:) sets the method signature for the selector requiring one argument. However, your method was defined as -showAlerttaking no arguments, so your object did not actually implement the method that you said when you called UIButton. Overriding your method, as shown by Jhaliya, will work, as it will change the switch of the target button to:

@selector( showAlert )
+3
source

All Articles