Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace a UIViewController in the UINavigationController hierarchy

I have an iPhone app which uses a standard implementation of UINavigationController 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 rootViewController and when the user presses a button, the app pushes to firstViewController. Then the user pushes another button to navigate the app to secondViewController. Again the use navigates down to another view controller, thirdViewController. However, I want the BackButton of the thirdViewController to pop back to firstViewController.

Essentially, when the user pushes to thirdViewController, I would like it to replace secondViewController 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?

Cheers, Brett

like image 722
Brett Avatar asked Dec 24 '11 21:12

Brett


People also ask

How many Uiviewcontrollers can a single UINavigationController hold?

The navigation stack isn't limited any more to 4 view controllers. Björn B. Björn B. Björn B.

What is the difference between viewController and UIViewController?

An UIViewController is just the base class of this already defined "View Controller", you add a viewController as you said, but a viewController has a class associated to it, that can be a UIViewController, UITableViewController, or variations/subclasses.

What is a navigation controller?

NavController manages app navigation within a NavHost . Apps will generally obtain a controller directly from a host, or by using one of the utility methods on the Navigation class rather than create a controller directly. Navigation flows and destinations are determined by the navigation graph owned by the controller.

What is UINavigationController for?

A navigation controller is a container view that can manage the navigation of hierarchical contents. The navigation controller manages the current displaying screen using the navigation stack. Navigation stack can have “n” numbers of view controllers.


2 Answers

Pretty Simple, when about to push the thirdViewController instead 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.

like image 143
Ecarrion Avatar answered Oct 01 '22 06:10

Ecarrion


NSMutableArray *viewController = [NSMutableArray arrayWithArray:[navController viewControllers]]; [viewController replaceObjectAtIndex:1 withObject:replacementController]; [navController setViewControllers:viewController]; 

See the UINavigationController class reference for more information.

like image 30
JustSid Avatar answered Oct 01 '22 07:10

JustSid