代码:
func showAlert(vc:UIViewController, title:String, message:String? = nil){
dispatchMain_async({
let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
let sureAlertAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Destructive, handler: nil)
alertController.addAction(sureAlertAction)
vc.presentViewController(alertController, animated: true, completion: nil)
})
}
当前vc,已经通过点击导航栏的返回按钮,消失了,然后调用到:
vc.presentViewController(alertController, animated: true, completion: nil)
出错:
Presenting view controllers on detached view controllers is discouraged
界面效果:
此处情况是:
调用
vc.presentViewController(alertController, animated: true, completion: nil)
时
对应的vc,已经(通过点击导航栏中的返回按钮)消失了
也的确是有问题的。
Presenting view controllers on detached view controllers is discouraged
Swift: "Presenting view controllers on detached view controllers is discouraged"
ios – Presenting view controllers on detached view controllers – Stack Overflow
ios – Presenting view controllers on detached view controllers is discourage – Stack Overflow
swift check view controller not detached
最后改为:
func showAlert(vc:UIViewController, title:String, message:String? = nil){
dispatchMain_async({
let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
let sureAlertAction = UIAlertAction(title: "确定", style: UIAlertActionStyle.Destructive, handler: nil)
alertController.addAction(sureAlertAction)
if isCurrentShowingVc(vc) {
vc.presentViewController(alertController, animated: true, completion: nil)
}
})
}
即可。
至少目前实现了:
当前的vc不是当前页面的话,就不提示了。
-》之前的case是:
点击返回按钮,当前页面已经不在导航栏的视图堆栈中了,所以就不显示了。
转载请注明:在路上 » [已解决]ViewController去presentViewController时出错:Presenting view controllers on detached view controllers is discouraged