Brett Brett - 1 year ago 50
Objective-C Question

Replace a UIViewController in the UINavigationController hierarchy

I have an iPhone app which uses a standard implementation of

for the app navigation.

I am trying to figure out a way to replace a view controller in the hierarchy.
In other words, my app loads into a
and when the user presses a button, the app pushes to
. Then the user pushes another button to navigate the app to
. Again the use navigates down to another view controller, thirdViewController. However, I want the BackButton of the
to pop back to firstViewController.

Essentially, when the user pushes to
, I would like it to replace
in the navigation hierarchy.

Is this possible? I know it is using Three20, but I'm not in this case. Nevertheless, if it's possible in Three20, then it certainly should be using straight SDK calls. Does anyone have any thoughts?


Answer Source

Pretty Simple, when about to push the thirdViewController inseat of doing a simple pushViewController do this:

NSArray * viewControllers = [self.navigationController viewControllers];
NSArray * newViewControllers = [NSArray arrayWithObjects:[viewControllers objectAtIndex:0], [viewControllers objectAtIndex:1], thirdController,nil];
[self.navigationController setViewControllers:newViewControllers];

where [viewControllers objectAtIndex:0] and [viewControllers objectAtIndex:1] are your rootViewController and your FirstViewController.