Ios 停止任务,直到AlertController动作发生
我试图从Ios 停止任务,直到AlertController动作发生,ios,xcode,swift,swift3,xcode8,Ios,Xcode,Swift,Swift3,Xcode8,我试图从present的completion参数执行一项任务,这样它只有在UIAlertController关闭后才能执行所需的功能。但是,在警报中执行操作之前调用了该函数。在采取操作之前,我如何等待执行该函数 let alert = UIAlertController(title: "Wild Card Played", message: "Choose your suit", preferredStyle : .alert); for suit in suits { alert.ad
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
,而不是在用户按下“OK”时调用。正如您在中所看到的,self.present
方法中的完成函数是“[执行]在演示完成后。此块没有返回值,也不接受任何参数。您可以为此参数指定nil。”警报为用户显示,第一个UIViewController显示“我已完成提示”然后运行cpuTurn
函数
您需要将代码放入UIAlertAction的处理程序中,该处理程序似乎已经存在。您应该将cpuTurn调用移动到
crazyEightPlayed
函数(或者至少从crazyEightPlayed调用cpuTurn)您可以尝试禁用具有交互的视图的子视图。我将记录这些子视图,然后仅在之后激活这些子视图
Swift 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
}
}
}
Swift 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
}
}
}