cmii
cmii

Reputation: 3626

How to dismiss an UIAlertController with no actions in SWIFT?

I display an UIAlertController when my image is downloading. When the downloading is finished, I want to push a view controller. I have an error in the console, because I don't dismiss the alert controller :

pushViewController:animated: called on <UINavigationController 0x7fb190c6ee00> while an existing transition or presentation is occurring; the navigation stack will not be updated.

In my main view controller, when the downloading is finished, I push another view :

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    //....

    var alert = UIAlertController(title: "Alert", message: text, preferredStyle: UIAlertControllerStyle.Alert)
    self.presentViewController(alert, animated: true, completion: nil)


    dispatch_async(dispatch_get_main_queue(), {
        if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
               self.performSegueWithIdentifier("postview", sender: self)

        }
   })
}

I tried wit dismissViewControllerAnimated but I have exactly the same error :

dispatch_async(dispatch_get_main_queue(), {
            if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
                   alert.dismissViewControllerAnimated(true, completion: nil)
                   self.performSegueWithIdentifier("postview", sender: self)

            }
       })

Upvotes: 5

Views: 6494

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

You should not call performSegueWithIdentifier before the previous view controller has been dismissed. To time this correctly, do it from the completion handler:

dispatch_async(dispatch_get_main_queue(), {
    if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) {
       alert.dismissViewControllerAnimated(true, completion: {
           self.performSegueWithIdentifier("postview", sender: self)
       })
    }
})

Now the call to perform segue would not start until the dismissal is over, preventing the error that you see.

Upvotes: 10

Related Questions