Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIAlertView Delegates

Tags:

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

like image 951
Joe Avatar asked Sep 29 '10 23:09

Joe


1 Answers

Let's say you showed an alert where the delegate was "self"

- (void)showAlert {
        UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"My Alert" 
                                                       message:@"Do you want to continue?"
                     delegate:self
                  cancelButtonTitle:nil
                  otherButtonTitles:@"No", @"Yes", nil];
        [myAlert show];
        [myAlert release];
}

In order for the following to work in your .m file:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

Your .h file will need to reference the UIAlertViewDelegate in the implementation statement like so:

@interface myViewController : UIViewController <UIAlertViewDelegate> {
}

This is what allows your .m file to respond to UIAlertViewDelegate method calls.

like image 71
dredful Avatar answered Oct 08 '22 02:10

dredful