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

Viewed 5789

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.

2 Answers
Related