我尝试从completion参数present执行一个任务,以便它只在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发布于 2016-06-27 19:51:03
当前的问题是在向用户显示警报时调用cpuTurn,而不是当用户按下“not”时。正如您在这里的文件中看到的,self.present方法中的完成函数“在表示完成后执行。这个块没有返回值,也不接受任何参数。您可以为这个参数指定零。”该警报为用户出现,第一个UIViewController表示“我已经完成了警报的呈现”,然后运行cpuTurn函数。
您需要将代码放在UIAlertAction的处理程序中,您似乎已经有了。您应该将cpuTurn调用移动到crazyEightPlayed函数(或者至少从crazyEightPlayed调用cpuTurn )。
发布于 2016-06-27 20:12:03
您可以尝试禁用具有交互作用的视图的子视图。我会注意到这些子视图,然后才会激活这些子视图。
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
}
}
}https://stackoverflow.com/questions/38062001
复制相似问题