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
The navigation stack isn't limited any more to 4 view controllers. Björn B. Björn B. Björn B.
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.
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.
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.
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.
NSMutableArray *viewController = [NSMutableArray arrayWithArray:[navController viewControllers]]; [viewController replaceObjectAtIndex:1 withObject:replacementController]; [navController setViewControllers:viewController];
See the UINavigationController class reference for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With