停止任务,直到发生 AlertController 操作 Swift
Stop tasks until AlertController action occurs Swift
我试图从 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
在向用户显示警报时被调用,而不是在用户按下 "Okay" 时被调用。正如您在 documentation here 中看到的那样,self.present
方法中的完成函数是“在演示结束后[执行]。此块没有 return 值并且不带参数。您可以指定此参数为零。”为用户显示警报,第一个 UIViewController 说 "I have completed presenting the alert" 然后运行 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
}
}
}
我试图从 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
在向用户显示警报时被调用,而不是在用户按下 "Okay" 时被调用。正如您在 documentation here 中看到的那样,self.present
方法中的完成函数是“在演示结束后[执行]。此块没有 return 值并且不带参数。您可以指定此参数为零。”为用户显示警报,第一个 UIViewController 说 "I have completed presenting the alert" 然后运行 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
}
}
}