简体   繁体   English

UIAlertController在呈现之前已释放

[英]UIAlertController Gets Deallocated Before Presenting It

I want to make a "rate app" alert, but for some reason it gets deallocated before showing it. 我想发出“评价应用程序”警报,但是由于某种原因,它在显示之前被释放了。

Here's the code: 这是代码:

func showAlert() {
    if #available(iOS 8.0, *)
    {
        let alertController = UIAlertController(title: "Rate App", message: "Rate this app now", preferredStyle: .Alert)
        let neverAction = UIAlertAction(title: "Never Show This Again", style: .Destructive, handler: { (action: UIAlertAction) in
            self.userDefaults.setBool(true, forKey: "rateAlertRejected")
        })
        let rateAction = UIAlertAction(title: "Rate Now", style: .Default, handler: { (action: UIAlertAction) in
            // Rate App
        })
        let remindAction = UIAlertAction(title: "Remind Me Later", style: .Cancel, handler: nil)

        alertController.addAction(rateAction)
        alertController.addAction(neverAction)
        alertController.addAction(remindAction)

        presentViewController(alertController, animated: true, completion: nil)
    }
    else
    {
        // Identical code (using UIAlertView) for iOS 7 which works perfectly
    }
}

The method executes (in certain conditions, but for testing purposes it does it every time) after a custom unwind segue. 该方法在自定义展开序列之后执行(在某些条件下,但每次都是出于测试目的)。

Why do I have this problem? 为什么会有这个问题? I used UIAlertController before but I had no issues. 我以前使用过UIAlertController ,但没有任何问题。

According to your comment you show showAlert in a method that executes after a custom unwind segue. 根据您的评论,在自定义展开 showAlert 执行后执行的方法中显示showAlert

unwind segue dismisses the view heriarchy and therefore your alert does not get reference to a view controller to show from. unwind segue消除了视图层次结构,因此您的警报未获得要显示的视图控制器的引用。

To solve this, show your alert in the View controller you unwind to or wait for the alert controller action to be completed before unwinding. 要解决此问题,请在展开的View控制器中显示警报,或等待警报控制器的操作完成后再展开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM