Swift:UIAlertController 未在 dispatch_async 中显示
Swift: UIAlertController not presented within dispatch_async
我有一个设置,我使用 UIAlertController 来显示任务的进度,直到任务的状态达到 return。
代码如下所示
class AlertVCDemo: UIViewController {
let alertVC = UIAlertController(title: "Search",
message: "Searching....", preferredStyle:
UIAlertControllerStyle.Alert)
override func viewDidLoad() {
super.viewDidLoad()
alertVC.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { action in
switch action.style{
case .Default:
print("default")
case .Cancel:
print("cancel")
case .Destructive:
print("destructive")
}
}))
}
func showAlertVC() {
dispatch_async(dispatch_get_main_queue(), {
self.presentViewController(self.alertVC, animated: true, completion: nil)
})
}
@IBAction func searchButtonClicked(sender: AnyObject) {
showAlertVC()
// Now do the real search that will take a while,
// depending on the result change the message of the alert VC
}
}
不知何故,我看到警报视图控制器没有显示在主线程中。搜索逻辑最终完成。我正在使用 iOS 9.3。在提出这个问题之前,我做了彻底的研究,none 类似线程上建议的解决方案有所帮助。不确定为什么 dispatch_async 在搜索仍在进行时不显示警报 VC。
即使我有 dispatch_async 不在方法中,事情也不会改变。
检查 UIViewController
的 presentedViewController
属性。它应该是零。如果它不是零,presentViewController
方法将不起作用。
@IBAction func searchButtonClicked(sender: AnyObject) {
showAlertVC()
// Now do the real search that will take a while,
// depending on the result change the message of the alert VC
}
在主线程中调用。所以我猜你的 "search code" 阻塞了 UI 线程。
尝试这样的事情
@IBAction func searchButtonClicked(sender: AnyObject) {
showAlertVC()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
// put your search code here
}
}
我有一个设置,我使用 UIAlertController 来显示任务的进度,直到任务的状态达到 return。
代码如下所示
class AlertVCDemo: UIViewController {
let alertVC = UIAlertController(title: "Search",
message: "Searching....", preferredStyle:
UIAlertControllerStyle.Alert)
override func viewDidLoad() {
super.viewDidLoad()
alertVC.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { action in
switch action.style{
case .Default:
print("default")
case .Cancel:
print("cancel")
case .Destructive:
print("destructive")
}
}))
}
func showAlertVC() {
dispatch_async(dispatch_get_main_queue(), {
self.presentViewController(self.alertVC, animated: true, completion: nil)
})
}
@IBAction func searchButtonClicked(sender: AnyObject) {
showAlertVC()
// Now do the real search that will take a while,
// depending on the result change the message of the alert VC
}
}
不知何故,我看到警报视图控制器没有显示在主线程中。搜索逻辑最终完成。我正在使用 iOS 9.3。在提出这个问题之前,我做了彻底的研究,none 类似线程上建议的解决方案有所帮助。不确定为什么 dispatch_async 在搜索仍在进行时不显示警报 VC。
即使我有 dispatch_async 不在方法中,事情也不会改变。
检查 UIViewController
的 presentedViewController
属性。它应该是零。如果它不是零,presentViewController
方法将不起作用。
@IBAction func searchButtonClicked(sender: AnyObject) {
showAlertVC()
// Now do the real search that will take a while,
// depending on the result change the message of the alert VC
}
在主线程中调用。所以我猜你的 "search code" 阻塞了 UI 线程。 尝试这样的事情
@IBAction func searchButtonClicked(sender: AnyObject) {
showAlertVC()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
// put your search code here
}
}