Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

            }
       })
like image 981
cmii Avatar asked Dec 06 '14 17:12

cmii


People also ask

How do I dismiss an action sheet in Swift?

To dismiss an action sheet, simulate a touch on its Cancel item. To do this, use the TouchItem method of the iOS ActionSheet test object that TestComplete associates with the control. This method simulates a single short touch on the specified item.

How do I dismiss an alert in Swift?

You can dismiss the alert by calling dismissViewControllerAnimated method on alertController object. Save this answer.

How do I dismiss an alert?

1. From the Alerts tab, under Alerts, click Inbox. 2. On the Alerts Inbox page, click the check boxes in front of the alerts you want to dismiss, then click the Dismiss button.

How do you dismiss an alert with click on outside of the alert iOS?

Step 1 − Open Xcode and create a single view application and name it UIAlertSample. So basically, when we tap on the button an alert will be displayed, when the user taps outside the alert the alert will be dismissing.


1 Answers

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.

like image 60
Sergey Kalinichenko Avatar answered Sep 27 '22 22:09

Sergey Kalinichenko