简体   繁体   中英

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)

            }
       })

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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