Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't popviewcontroller work after presentviewcontroller on alert?

Tags:

ios

swift

//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?

like image 806
James Lee Avatar asked Sep 24 '26 10:09

James Lee


2 Answers

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.

like image 93
Hoang Tran Avatar answered Sep 26 '26 16:09

Hoang Tran


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:

  • you don't really need to use ; in swift.
like image 22
Idan Avatar answered Sep 26 '26 17:09

Idan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!