views:

129

answers:

1

hi,

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.

some help?

thks in advance!

+4  A: 

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];
KennyTM
+1: What I was halfway through writing. I'd also mention that the view controller needs to implement the `UIAlertViewDelegate` (though I believe it gives a warning if it doesn't.)
sdolan