我有时在调用 UIAlert
时收到此警告
Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior ()
code used:
func alert (dictKey: String){
print(dictKey)
let alertController = UIAlertController(title: nil, message: promptsArr[dictKey], preferredStyle: .Alert )
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
}
alertController.addAction(OKAction)
if self.presentedViewController == nil {
delay(1.0){
self.presentViewController(alertController, animated: true, completion: nil)
}
} else {
self.presentedViewController!
}
最佳答案
移除
delay()
将逻辑放在
self.presentedViewController
中: (如果存在则无需创建警报)func alert (dictKey: String){ if self.presentedViewController == nil { let alertController = UIAlertController( title: nil, message: promptsArr[dictKey], preferredStyle: .Alert ) let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in } alertController.addAction(OKAction) self.presentViewController( alertController, animated: true, completion: nil) } }
关于ios - 警告时 UIAlertController : 0x7ff211650140 called in Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33705748/