【问题标题】:How to handle multiple UIAlertControllers in iOS?如何在 iOS 中处理多个 UIAlertController?
【发布时间】:2015-10-15 19:20:11
【问题描述】:

在 iOS8 之前,我必须在一个 viewController 中显示多个 UIAlerts,我们可以使用带有标签的 UIAlerts,我们可以使用类似标签在 clickedButtonAtIndex 中进行识别。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag == 1)
{
//UIAlert1 button clicked
}

if(alertView.tag == 2)
{
//UIAlert2 button clicked
}
}

所以我们可以做一些事情。 如何识别不同 UIAlertControllers 的按钮点击。 因为一个 alert1 按钮单击我必须更改一些文本颜色和 alert2 按钮单击我必须弹出视图控制器。

【问题讨论】:

标签: ios iphone uialertcontroller


【解决方案1】:

UIAlertController 是基于块的。

为每个操作创建一个UIAlertAction 实例,并在点击按钮后传递要执行的块。

如需了解更多信息,请阅读 Mattt Thompson 的 UIAlertController "review"

【讨论】:

  • 是的,我可以传递要为按钮操作执行的块。就像我有两个带有取消和完成、取消和保存按钮的警报。当用户单击完成时,我必须执行一项功能,当用户单击保存时,我必须执行其他功能。
【解决方案2】:

您可以通过以下方式进行。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
 {
  if(alertView.tag == 1)
    {
      //UIAlert1 button clicked
         if(buttonIndex==0){//say, **Cancel** button tag
            //alert2 "Cancel" button has tapped
          }else if(buttonIndex==1){say, **OK** button tag
            //alert2 "OK" button has tapped
           }
    }

  if(alertView.tag == 2)
    {
      //UIAlert2 button clicked
      if(buttonIndex==0){//say, **Yes** button tag
           //alert2 "YES" button has tapped
          }
    }
}

【讨论】:

  • 此委托方法用于 UIAlertView,但不用于 UIAlertController,它已从 iOS8 中接管 UIAlertview 和 UIActionSheet
  • @jamil65able 从 iOS8 开始我们不能使用 UIAlertView。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-07
  • 2021-01-20
  • 1970-01-01
  • 1970-01-01
  • 2012-08-02
相关资源
最近更新 更多