The delegate method "clickedButtonAtIndex:" is not called

I created a two-button alert view using the following code:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: title message: msg delegate:nil cancelButtonTitle:@"Replay" otherButtonTitles:@"Highscore", nil]; [alertView show]; 

I want to run the code when I click one of the buttons. To do this, I added the following file to the delegate.m file:

  - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex==0) //Run some code else //Other code } 

But this method is not called when I press any of the buttons! Can someone tell me why?

Thanks in advance,

Sagiftw

+6
objective-c iphone uialertview
source share
5 answers
 delegate:nil 

how will the delegate delegate notification view be displayed if you specify that there will be no delegates? Replace this part with

 delegate:self 

instead.

+39
source share

Try setting the delegate to yourself instead of nil.

+3
source share

I called the UIAlertView rejectWithClickedButtonIndex: animated method : from the UITextField delegate method, because I also wanted to process the keyboard return key:

 - (BOOL)textFieldShouldReturn:(UITextField *)textField { if (textField.tag == 1001) { [textField resignFirstResponder]; [_alert dismissWithClickedButtonIndex:1 animated:YES]; } return YES; } 

This method alertView: clickedButtonAtIndex: method is never called, even if you configured the proper delegate. Instead, alertView: didDismissWithButtonIndex: is called. Instead, execute this method:

 - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { // your code here } 

If you just want to handle only the alert preview buttons, these first calls clicked ButtonAtIndex and then didDismissWithButtonIndex.

+2
source share

in .h put UIActionSheetDelegate and in .m when creating alertView put the delegate on itself not zero, as is done in the above case

0
source share

The correct answer to this question is delegate:nil . But if the delegate is already configured for itself, and clickedButtonAtIndex still does not work, try to check if a new view controller ( [self.navigationController popViewControllerAnimated:YES]; ) has appeared after displaying alertview. It can also cause clickedButtonAtIndex not to be called. This is what happened to me.

Hope this helps someone.

0
source share

All Articles