tags:

views:

17

answers:

1

Hi so say I have this code:

UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"Yay"
message: msg
delegate: self
cancelButtonTitle: @"Proceed..."
otherButtonTitles: @"1", @"2", nil];

How do I control the other buttons "1" and "2"? (Suppose all other necessary code is in place, such as the undefined variable msg)

Thanks!

+2  A: 

You need to set the delegate property of your UIAlertView instance (i.e. alert) and implement the method alertView:clickedButtonAtIndex:.

Let's assume that you want the current class to be the delegate of the UIAlertView. You would use the following line of code:

alert.delegate = self;

Next, you would setup the current class to implement the UIAlertViewDelegate protocol in the .h file. For example:

@interface MyClass : NSObject <UIAlertViewDelegate>

Then you would simply implement the alertView:clickedButtonAtIndex: method in the .m file:

- alertView:a clickedButtonAtIndex:i {
  NSLog(@"You clicked %d.", i);
}
Senseful