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

发布于 2024-09-18 15:10:34 字数 602 浏览 2 评论 0原文

我有两个 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 则此代码不起作用。

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

I have two UIAlertView's which are not displayed one after another. Both of the have two buttons and I need to determine which button was pressed. I've tried to use

- (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

}
}

But this code doesn't work if I have two UIAlertViews.

Can you help me? Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烙印 2024-09-25 15:10:34

看来您可以稍微优化一下您的设计。为什么不在 UIAlertView 周围包装一个方法,然后传入显示警报所需的信息。

然后使用

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

}

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

Looks like you might be able to optimize your design a little bit. Why not wrap a method around your UIAlertView, then pass in the information you need to display the alert.

Then use

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

}

To check which button was clicked based on the title.

夜吻♂芭芘 2024-09-25 15:10:34

需要较少资源的另一个选项是只为每个警报窗口分配一个标签值。上面列出的方法确实有效,但比较字符串比仅使用标记值会增加一点内存使用量。通过为每个标签分配标签值,您仍然可以使用 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 服务(因此我们需要检查网络)连接并显示重试呼叫的警报),并且还针对其他一些交互出现了警报。使用上面的标签选项可以很容易地确定正在与哪个警报视图进行交互。

Another option that would require less resources is to just assign a tag value to each Alert Window. The method listed above does work but comparing strings adds a little more memory use than just using tag values. By assigning tag values to each tag you can still use the clickedButtonAtIndex option and then you just need to check which Alert View was clicked:

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

I did this in one of my apps since there were Web Services being called (so we needed to check for network connections and show an alert to retry the call) and also had alerts that came up for some of the other interactions as well. Using the tag option above made it really easy to determine which alertview was being interacted with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文