Your code creates a new instance of a view that has never been pushed onto the stack, then tries to pop back to that controller.
If you are popping back to the root view controller, you can uses popToRootViewControllerAnimated:
If you are popping back a known distance you can call popViewControllerAnimated:
more than once. In your example, that would be 2 controllers so to calls. You could do the same thing by looking in viewControllers
for the controller 2 from the end and popping to it.
The above suggestions are quick fixes. One best practice scenario would be to pass the controller you want to return to along to each successive controller you push. First passes itself to second, second passes that reference to third, third pops to the passed reference, which is first.
In effect, you are creating a temporary root controller. You could subclass UINavigationController
and add a temporaryRoot
property and a popToTemporaryRootViewControllerAnimated:
method that would pop to your temporary root and clear it. When first pushes seconds, it would also set itself as the temporary root so that every controller in the stack does not have to pass a reference around. You would have to add some extra checks to unsure you never pop past the temporaryRoot without clearing it.