ios - 停止任务,直到 AlertController 操作发生 Swift

标签 ios xcode swift swift3 xcode8

我尝试从 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

最佳答案

当前的问题是,cpuTurn 是在向用户显示警报时调用的,而不是在用户按下“确定”时调用的。正如您在 documentation here 中看到的那样self.present 方法中的完成函数是“在演示完成后执行。此 block 没有返回值且不带任何参数。您可以为此参数指定 nil”。向用户显示警报,第一个 UIViewController 表示“我已完成警报呈现”,然后运行 ​​cpuTurn 函数。

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

关于ios - 停止任务,直到 AlertController 操作发生 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38062001/

相关文章:

Swift 在函数返回枚举时给我错误

ios - "Type ' 任务。在 Swift 中键入 ' has no subscript members"错误

iphone - 使用 NSSortDescriptor 时发现一些错误

xcode - 来自 Xcode 的 Applescript 术语列表

swift - SKPhysicsBody 仅使用 SKTexture 的一部分

iphone - NSMakeCollectable 和 ARC 不起作用

iOS 和 OpenCV : how to initialise CvVideoCamera object to reflect UIImage size?

IOS:创建全屏 'overlay' UIView (Monotouch)

ios - 改变tableHeaderView框架,tableview的contentSize每次增加0.5

ios - 帧改变图像大小