0

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.

Lawrence413
  • 1,976
  • 1
  • 14
  • 28

1 Answers1

1

According to your comment you show showAlert in a method that executes after a custom unwind segue.

unwind segue dismisses the view heriarchy and therefore your alert does not get reference to a view controller to show from.

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.

Shubhank
  • 21,721
  • 8
  • 65
  • 83
  • I am showing it IN the view controller to which I unwind. Basically I have VC1 and VC2. VC1 has an unwind method (required for custom unwind segues). The method gets executed when the user goes from VC2 to VC1. `showAlert()` is in that unwind method. I want to come back from VC2 to VC1 and show the alert. – Lawrence413 Apr 30 '16 at 10:41
  • in that case , call it after a delay.. the method is called when the unwind segue is taking place.. so call it with a 0.5 sec delay or 1 sec and check – Shubhank Apr 30 '16 at 10:43
  • You can't call it in the unwind method as VC1 isn't yet active. I would suggest you set a flag in the unwind method and check that in `viewDidAppear` and use that to display the alert – Paulw11 Apr 30 '16 at 10:44
  • @Shubhank Thank you! I delayed it by 0.5 seconds and it works fine now. – Lawrence413 Apr 30 '16 at 10:47
  • @Paulw11 Thanks for the help! I thought of that too, but just delaying the presentation is more convenient in my case. – Lawrence413 Apr 30 '16 at 10:47
  • 1
    Delays are a bit of a hack. I would recommend against that approach. See my answer here http://stackoverflow.com/questions/36888812/showing-alert-after-unwind-segue-stops-the-segue-how-do-i-make-sure-alert-is-sh/36889490#36889490 – Paulw11 Apr 30 '16 at 10:48
  • agree with Paul, if you need a more efficient approach. go with that. If you are relaxed on coding standards. delay will be fine – Shubhank Apr 30 '16 at 10:51
  • Here is an approach that uses neither delays nor state flags http://stackoverflow.com/a/37602422/1552116 – wyu Jun 02 '16 at 21:32