vkrishnan23 vkrishnan23 - 3 months ago 11
iOS Question

Trying to present 2 UIAlertControllers back to back

I have a

in which the options are populated from an array and are presented to the user. The user then selects an option from the alert. After this, I have a separate alert that provides the user with a confirmation message that has an okay button.

myAlert.addAction(UIAlertAction.init(title: item, style: .Default, handler: {
(UIAlertAction) in
self.presentViewController(myAlert, animated: true, completion: nil)

The code above gathers the data from the array and pushes it into actions in myAlert. The code above is inside of a for loop.

After this I use a function to retrieve the topmost view controller, and then push the next alert.

let top = topMostController()
let alertController = UIAlertController(title: "Location pinned", message: "You've successfully pinned this location, good work!", preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
(result : UIAlertAction) -> Void in

self.presentViewController(myAlert, animated: true, completion: nil)
top.presentViewController(alertController, animated: true, completion: {
_ in

The error I receive is:

Attempting to load the view of a view controller while it is
deallocating and is not allowed and may result in undefined behavior.
UIAlertController: 0x1535b1cd0.

Can someone help me with this?


I think this is what you are looking for. The second must be called with the dismissal action of the first. Also, anytime you work with UI, It is safer to use dispatch_async(dispatch_get_main_queue()) { \\code }

than not if you are not positive you are currently on the main queue.

let firstAlertController = UIAlertController(title: "First", message: "This is the first message.", preferredStyle: UIAlertControllerStyle.Alert)

let secondAlertController = UIAlertController(title: "Second", message: "This is the second message.", preferredStyle: UIAlertControllerStyle.Alert)
let secondDismissAction = UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default, completion: nil)

let firstDismissAction = UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default) {
    UIAlertAction in
    dispatch_async(dispatch_get_main_queue()) {
        self.presentViewController(secondAlertController, animated: true, handler: nil)

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