Ios 试图在视图控制器解除分配时加载视图控制器的视图。。。(<;UIAlertController:>;)

Ios 试图在视图控制器解除分配时加载视图控制器的视图。。。(<;UIAlertController:>;),ios,uialertcontroller,Ios,Uialertcontroller,我在一个视图控制器上得到上述错误,尽管我在另一个视图控制器中有完全相同的代码,但我没有得到错误: override func viewDidAppear(animated: Bool) { super.viewDidAppear(animated) let alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert) alertContro

我在一个视图控制器上得到上述错误,尽管我在另一个视图控制器中有完全相同的代码,但我没有得到错误:

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声明为类属性,然后修改我的ViewDidDisplay,如下所示:

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)
            }
        }
}

最后,我不得不将alertController声明为类属性,然后修改我的ViewDidDisplay,如下所示:

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)
            }
        }
}