UIAlertViewDelegate:clickedButtonAtIndex和两个按钮

jxd*_*ter 6 uialertview ios

球员:

测试应用程序的viewController中有两个按钮,右边一个我称之为"NO",

另一个是"是".这两个按钮将调用两个不同的功能,以及何时

用户按下其中一个按钮,我想向用户显示一个警告以确认.

我知道使用UIAlertViewDelegate

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
Run Code Online (Sandbox Code Playgroud)

但有两个按钮,我很困惑.我怎么知道按下了哪个按钮.

所以,请帮助我,提前谢谢你!

ber*_*ium 17

创建时,UIAlertView您可以tag为其设置

-(IBAction)yesButtonClick:(id)sender{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle: @"Cancel" otherButtonTitles:@"OK", nil];
    alert.tag = 101;
    [alert show];
}

-(IBAction)noButtonClick:(id)sender{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle: @"Cancel" otherButtonTitles:@"OK", nil];
    alert.tag = 102;
    [alert show];
}
Run Code Online (Sandbox Code Playgroud)

在委托方法中检查正在显示哪个警报

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == 101) {
        // from YES button
    }
    else if (alertView.tag == 102) {
        // from NO button
    }
}
Run Code Online (Sandbox Code Playgroud)