![](/img/trans.png)
[英]Attempting to load the view of a view controller while it is deallocating … UIAlertController
[英]Attempting to load the view of a view controller while it is deallocating… (<UIAlertController:>)
我在視圖控制器上遇到了以上錯誤,盡管我在另一個視圖控制器中卻獲得了完全相同的代碼,但在這里我沒有收到錯誤:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
alertController.addAction(UIAlertAction(title: "Settings", style: UIAlertActionStyle.Default, handler: {
action in
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
}
))
if self.presentedViewController == nil{
self.presentViewController(alertController, animated: true, completion: nil)
}
}
我最終不得不將alertController聲明為類屬性,然后按如下所示修改viewDidAppear:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if self.alertController.actions.count == 0 {
self.alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.alertController.addAction(UIAlertAction(title: "Settings", style: UIAlertActionStyle.Default, handler: { action in UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
}
))
if self.presentedViewController == nil {
self.presentViewController(self.alertController, animated: true, completion: nil)
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.