ios - UIAlertViewDelegate :clickedButtonAtIndex and two buttons

标签 ios uialertview

小伙伴们:

在我的测试应用程序的 viewController 中有两个按钮,我称之为“否”的正确按钮,

另一个是"is"。两个按钮会调用两个不同的函数,而当

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

我知道使用 UIAlertViewDelegate

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

但是有两个按钮,我很纳闷。我怎么知道按下了哪个按钮。

所以,请帮我解决这个问题,在此先感谢您!

最佳答案

当你创建一个UIAlertView时,你可以为它设置一个标签

-(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];
}

在委托(delegate)方法中检查正在显示的警报

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

关于ios - UIAlertViewDelegate :clickedButtonAtIndex and two buttons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9976471/

相关文章:

objective-c - 为什么我的警报显示两次?

iphone - 如何在 Swift 中使用警报 View 解除 segue?

ios - 是否可以声明类似 lambda 函数的东西,UIAlert 中的 OK 按钮将执行什么操作?

iphone - 如何使用NSJSONSerialization输出数据?

ios - 从 NSMutableArray 中移除所有符合条件的对象

iOS - 在父级插入 AnotherVC 后如何在 NavigationController 堆栈中找到 ChildVC

ios - 带有 block 回调的自定义 UIAlertView

带有 UIActivityIndi​​catorView 的 iOS 7 样式弹出窗口/警报 View

ios - 如何居中混合变形 SCNNode 以查看

iphone - 从钥匙串(keychain)访问导出公钥