Can someone explain how the delegate to a UIAlertView
works? Is it automatically called or do I have to call it? Eg:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
Can someone explain how the delegate to a UIAlertView
works? Is it automatically called or do I have to call it? Eg:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
Let's say you showed an alert where the delegate was "self"
In order for the following to work in your .m file:
Your .h file will need to reference the UIAlertViewDelegate in the implementation statement like so:
This is what allows your .m file to respond to UIAlertViewDelegate method calls.
The
alertView:clickedButtonAtIndex:
method of the delegate is automatically called byUIAlertView
. The init method forUIAlertView
takes a delegate as one of the parameters. Just make sure to pass in an object that responds toalertView:clickedButtonAtIndex:
.So long as you're correctly setting the delegate property of the UIAlertView and implementing the protocol, it will be automatically called when a user clicks on a button in your alert.
Take a look at the projects listed under "Related sample code" at http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html to see it in action.
Here is a wrapper for the delegate so that you can use blocks instead. The flow of execution will be the same but the flow of the code will be easier to follow. So, usage:
...and here is the helper class: