2015-10-11 54 views
1

我得到上述錯誤的視圖控制器,雖然我有完全相同的代碼在另一個視圖控制器,其中我沒有收到錯誤:嘗試加載視圖控制器的視圖,而它正在釋放...(<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) 
    } 
} 

回答

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) 
      } 
     } 
} 
相關問題