iosuinavigationcontrollerviewdidloadpopviewcontroller

Popping view controller immediately after pushing it


I want the view controller to check something, and if it's true it would allow the user to use the view controller, and if it's false it would show an alert to the user and send the user back where he/she came from.

So I tried adding this to an if-statement at the end of viewDidLoad:

[self.navigationController popViewControllerAnimated:YES]

However, I got the error:

2014-08-09 20:12:59.731 ProjectName[1978:60b] nested pop animation can result in corrupted navigation bar

2014-08-09 20:13:00.118 ProjectName[1978:60b] Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.

I learned from this StackOverflow question that calling pushViewController or popViewController before viewDidLoad finishes is unsafe. Are there any ways around this?

TLDR: I'm trying to push a view controller onto the stack, show an alert, then pop the view controller off of the stack.

EDIT: Added code.


Solution

  • This should be fairly easy provided you want to show the alert.

    1 - Call [UIAlertView show] method from within viewDidAppear. It would be advisable to use viewDidAppear as it would be last in the view life cycle, when the view controller is fully pushed on the stack. So popping will be harmless.

    2 - In clickedButtonAtIndex delegate method, call popViewControllerAnimated.