我尝试从 present
的 completion
参数执行任务,以便它仅在 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
最佳答案
当前的问题是,cpuTurn
是在向用户显示警报时调用的,而不是在用户按下“确定”时调用的。正如您在 documentation here 中看到的那样self.present 方法中的完成函数是“在演示完成后执行。此 block 没有返回值且不带任何参数。您可以为此参数指定 nil”。向用户显示警报,第一个 UIViewController 表示“我已完成警报呈现”,然后运行 cpuTurn
函数。
您需要将代码放入 UIAlertAction 的处理程序中,您似乎已经拥有了该处理程序。您应该将 cpuTurn 调用移至crazyEightPlayed
函数(或者至少从crazyEightPlayed 调用cpuTurn)
关于ios - 停止任务,直到 AlertController 操作发生 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38062001/