【问题标题】:Stop tasks until AlertController action occurs Swift停止任务,直到发生 AlertController 操作 Swift
【发布时间】:2016-06-27 19:42:34
【问题描述】:

我尝试从presentcompletion 参数执行一个任务,以便它仅在UIAlertController 关闭后执行所需的功能。但是,在警报中执行操作之前调用了该函数。如何等待执行该函数,直到采取行动?

let alert = UIAlertController(title: "Wild Card Played", message: "Choose your suit", preferredStyle : .alert);
for suit in suits {
    alert.addAction(UIAlertAction(title: suit, style: .default, handler: crazyEightPlayed))
                  }
self.present(alert, animated: true, completion: cpuTurn) //Upon completion call the cpuTurn() function

【问题讨论】:

    标签: ios xcode swift swift3 xcode8


    【解决方案1】:

    当前的问题是cpuTurn 在向用户显示警报时被调用,而不是在用户按下“确定”时被调用。正如你在documentation here 中看到的,self.present 方法中的完成函数是“在演示完成后[执行]。这个块没有返回值,也没有参数。你可以为这个参数指定 nil。”向用户显示警报,第一个 UIViewController 说“我已完成呈现警报”,然后运行 ​​cpuTurn 函数。

    您需要将代码放入您似乎已经拥有的 UIAlertAction 的处理程序中。您应该将 cpuTurn 调用移至 crazyEightPlayed 函数(或至少从 crazyEightPlayed 调用 cpuTurn)

    【讨论】:

      【解决方案2】:

      您可以尝试禁用视图的具有交互作用的子视图。我会记下这些子视图,然后才激活那些子视图。

      斯威夫特 2:

      var disabledSubviews = [UIView]()
      
      let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
      alert.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { (action) in
          for subview in disabledSubviews {
              subview.userInteractionEnabled = true
          }
      }))
      
      self.presentViewController(alert, animated: true) { 
          for subview in self.view.subviews {
              if subview.userInteractionEnabled == true {
                  disabledSubviews.append(subview)
                  subview.userInteractionEnabled = false
              }
          }
      }
      

      斯威夫特 3:

      var disabledSubviews = [UIView]()
      
      let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
      alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in
          for subview in disabledSubviews {
              subview.isUserInteractionEnabled = true
          }
      }))
      self.present(alert, animated: true) { 
          for subview in self.view.subviews {
              if subview.isUserInteractionEnabled == true {
                  disabledSubviews.append(subview)
                  subview.isUserInteractionEnabled = false
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2013-12-12
        • 1970-01-01
        • 2016-07-20
        • 2014-09-07
        • 2021-02-18
        • 1970-01-01
        • 1970-01-01
        • 2017-08-02
        • 2013-12-27
        相关资源
        最近更新 更多