Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss a UIViewController from a UIAlertController's UIAlertAction handler?

I would like to present a simple UIAlertController to the user with one of the options triggering the closing of the parent view controller. Here is the code that I am using:

    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)

    alert.addAction(UIAlertAction(title: "Close View", style: UIAlertActionStyle.Destructive, handler: {
        action in

        self.dismissViewControllerAnimated(true, completion: nil)
    }))

    alert.addAction(UIAlertAction(title: "CANCEL", style: UIAlertActionStyle.Cancel, handler: nil))

    self.presentViewController(alert, animated: true, completion: nil)

It doesn't have any effect. After executing the "Close View" handler the view controller that presents the alert is still there.

I also tried self.navigationController?.dismissViewControllerAnimated(true, completion: nil) in the UIAlertAction action block but that didn't work either.

like image 525
Chris Balavessov Avatar asked Mar 04 '16 22:03

Chris Balavessov


1 Answers

Using

self.navigationController?.popViewControllerAnimated(true)

instead of

self.dismissViewControllerAnimated(true, completion: nil)

works as required and closes the view controller which displays the alert.

like image 185
Chris Balavessov Avatar answered Oct 22 '22 18:10

Chris Balavessov