Search code examples
iosswiftuistoryboardsegueuialertcontroller

Swift - Warning when presenting alert inside prepareForSegue method


I keep getting a following warning when I present UIAlertController:

2016-08-16 13:29:48.138 MyProject[602:98207] pushViewController:animated: called on while an existing transition or presentation is occurring; the navigation stack will not be updated.

here is the relevant piece of code:

  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "toUserData" && didSelect
    {
        let vc:UserDataViewController = segue.destinationViewController as! UserDataViewController
        vc.unitIndex = self.selectedIndex - 1
    }
    else
    {
        self.showAlert()
        return
    }

}

    func showAlert()
    {
        let alert =  UIAlertController(title: "Error", message: "Are you sure that you set units properly?", preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
    self.presentViewController(alert, animated: true, completion: nil)
    }

}

I used this kind of structure before in other project and I don't remember getting such warining.

Also, I don't really know why I get it, because presenting alert is not interrupting any other action regarding transitions.

Thanks in advance


Solution

  • I used the wrong method to check if everything is all-set to perform segue, this kind of things should be done using shouldPerformSegueWithIdentifier method:

    override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
    
        if identifier == "toUserData" && didSelect
        {
            return true
        }
        else
        {
            self.showAlert()
            return false
        }
    }