I have an application where I need to remove one view from the stack of a UINavigationController and replace it with another. The situation is that the first view creates an
I've discovered you don't need to manually mess with the viewControllers
property at all. Basically there are 2 tricky things about this.
self.navigationController
will return nil
if self
is not currently on the navigation controller's stack. So save it to a local variable before you lose access to it.retain
(and properly release
) self
or the object who owns the method you are in will be deallocated, causing strangeness.Once you do that prep, then just pop and push as normal. This code will instantly replace the top controller with another.
// locally store the navigation controller since
// self.navigationController will be nil once we are popped
UINavigationController *navController = self.navigationController;
// retain ourselves so that the controller will still exist once it's popped off
[[self retain] autorelease];
// Pop this controller and replace with another
[navController popViewControllerAnimated:NO];
[navController pushViewController:someViewController animated:NO];
In that last line if you change the animated
to YES
, then the new screen will actually animate in and the controller you just popped will animate out. Looks pretty nice!