//Event created alert
let alert = UIAlertController(title: "Event Created", message: "Event successfully created", preferredStyle: UIAlertControllerStyle.Alert);
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler:nil));
self.presentViewController(alert, animated: true, completion: nil);
//Pop back to table
self.navigationController!.popToRootViewControllerAnimated(true);
Here in this code, I create an alert, and directly after the alert I use the popToRootViewControllerAnimated method. This doesn't work for some reason and the workaround i found is to call the method inside the completion of presentViewController.
Why is it that the pop method doesn't work after the presentViewController method unless it's put into the closure?
When you call self.presentViewController(alert, animated: true, completion: nil), the viewController is going to perform the presentation. And when it's progressing, you are not allowed to perform any other transition/presentation.
Actually, when running your code on my machine, I got this log:
popToViewController:transition: called on while an existing transition or presentation is occurring; the navigation stack will not be updated.
That should explain itself pretty clearly. You should move this line self.navigationController!.popToRootViewControllerAnimated(true) into the completion closure instead.
I think you want to use the popToRoot only after the user tapped on OK, in that case:
// Created the alert
let alert = UIAlertController(title: "Event Created", message: "Event successfully created", preferredStyle: UIAlertControllerStyle.Alert)
// Create the action
let OKAction = UIAlertAction(title: "OK", style: .Default) { (action: UIAlertAction!) -> Void in
self.navigationController!.popToRootViewControllerAnimated(true);
}
// Add the action
alert.addAction(OKAction)
// Present the alert
self.presentViewController(alert, animated: true, completion: nil)
Notes:
; in swift. If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With