The cancel button is always index 0, and all other buttons in the array start at index 1 counting up.
For completion, here’s how to create it:
1 2 3 4 5 6 |
- (IBAction)showAlert:(id)sender { // create an alert view with three buttons UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Three Button Alert" message:@"This is an alert view with more than two buttons. Will it work?" delegate:self cancelButtonTitle:@"No Way (0)" otherButtonTitles:@"Perhaps (1)", @"Definitely! (2)", nil]; [alertView show]; } |
And here’s how to react to it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { switch (buttonIndex) { case 0: NSLog(@"That was button at index 0"); break; case 1: NSLog(@"That was button at index 1"); break; case 2: NSLog(@"That was button at index 2"); break; case 3: NSLog(@"We don't have a fourth button"); break; default: break; } } |
This is a delegate method, so the reacting class needs to conform to the UIAlertView Protocol like so:
1 |
@interface ViewController : UIViewController <UIAlertViewDelegate> |