在取消分配时尝试加载视图控制器的视图...(<uialertcontroller:>)

时间:2015-10-11 01:23:08

标签: ios 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)
    }
}

1 个答案:

答案 0 :(得分:0)

我最终不得不将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)
            }
        }
}