确定在两个UIAlertViews之一中按下了哪个按钮

时间:2010-09-09 18:13:02

标签: iphone uialertview

我有两个UIAlertView不会一个接一个地显示。两个都有两个按钮,我需要确定按下了哪个按钮。我试过用

- (void)alertOKCancelAction {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Title"
                                               message:@"Message" delegate:self
                                     cancelButtonTitle:@"Yes" otherButtonTitles:@"No", nil];
[alert show];
[alert release];
}  

 - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
{    //Code

}
else
{//Code

}
}

但是如果我有两个UIAlertViews,这个代码不起作用。

你能帮帮我吗?提前谢谢!

2 个答案:

答案 0 :(得分:3)

看起来您可能会稍微优化您的设计。为什么不围绕UIAlertView包装方法,然后传递显示警报所需的信息。

然后使用

- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated
{
      NSString *btnTitle = [alertView buttonTitleAtIndex:buttonIndex];
      //....Do something based on the btnTitle that was clicked.

}

根据标题检查点击了哪个按钮。

答案 1 :(得分:3)

另一个需要较少资源的选项是只为每个警报窗口分配一个标记值。上面列出的方法确实有效,但比较字符串比使用标记值增加了更多的内存使用。通过为每个标记分配标记值,您仍然可以使用clickedButtonAtIndex选项,然后您只需要检查单击了哪个警报视图:

NSInteger alertTag = alertView.tag
if (alertTag == 1) { 
  if (buttonIndex == 0 { 
    //do something based on first Alertview being clicked
  }
}
if (alertTag == 2) { 
...continue as much as you need

我在我的一个应用程序中执行了此操作,因为调用了Web服务(因此我们需要检查网络连接并显示重试调用的警报),并且还有一些其他交互的警报好。使用上面的标记选项可以非常轻松地确定与之交互的警报视图。

相关问题