UIAlertView Delegates

后端 未结 4 1169
南旧
南旧 2020-12-30 02:17

Can someone explain how the delegate to a UIAlertView works? Is it automatically called or do I have to call it? Eg:

- (void)alertView:(UIAlertVie

4条回答
  •  长发绾君心
    2020-12-30 02:37

    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  {
    }
    

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

提交回复
热议问题