Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting button click with UIAlertView

Tags:

I am trying to call and alert when a button is pressed. i use this :

-(IBAction)Add {      UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"add button pressed"                                                     message:@"Add to record"                                                    delegate:nil                                                cancelButtonTitle:@"Cancel"                                            otherButtonTitles:@"OK", nil   ];     [alert show];     [alert release];     }  

ok , no problem here, two button came up, OK and cancel. Now i want detect which button is pressed i use:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {     // the user clicked one of the OK/Cancel buttons     if (buttonIndex == 0)     {        //just to show its working, i call another alert view         UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"OK WORKIng well"                                                         message:@"no error"                                                                                                            delegate:nil                                                cancelButtonTitle:@"IWORKS"                                                otherButtonTitles:@"NO PRB", nil];         [alert show];         [alert release];           }     else     {         NSLog(@"cancel");            } } 

now here is the problem. i cannnot detect which button is pressed; the 2nd alertview doesnt show. i've check through the code a couple of times, there doesn't seem to be any problem with it. no error/warning too.

like image 525
Stefan Avatar asked Aug 16 '10 08:08

Stefan


1 Answers

To detect the button clicks the alert view must have an associated delegate, e.g.

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"add button pressed"                                                 message:@"Add to record"                                                delegate:self    // <------                                       cancelButtonTitle:@"Cancel"                                       otherButtonTitles:@"OK", nil]; 
like image 64
kennytm Avatar answered Nov 06 '22 01:11

kennytm