Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I switch between Navigation Controllers?

Situation:
I have an Xcode project based on the "Navigation-Based-Application" template. So that means I have a SINGLE UINavigationController that manages a UIViewController.

What I want To Do:
What I want to do is add one more UINavigationController to my project -- and be able to switch back and forth between them. (I want to do this to make space for some seperate unrelated content so that it does not have a back button pointing back to the root view controller.)

Question:
How do I add one more UINavigationController to my project and switch between the two UINavigationControllers?

like image 225
RexOnRoids Avatar asked May 15 '10 03:05

RexOnRoids


People also ask

How do I switch between view controllers?

Right-click the control or object in your current view controller. Drag the cursor to the view controller you want to present. Select the kind of segue you want from the list that Xcode provides.

How do I get navigation controller root view controller?

The root view controller is simply the view controller that sits at the bottom of the navigation stack. You can access the navigation controller's array of view controllers through its viewControllers property. To access the root view controller, we ask for the first item of the array of view controllers.

How do I get the most popular view controller?

In addition, you can check for UINavigationController and ask for its topViewController or even check for UITabBarController and ask for selectedViewController . This will get you the view controller that is currently visible to the user.

How do I segue to my navigation controller?

Step 3: Create a segue to navigate to a new view controller Add the view controller you want to navigate to in the storyboard. You can trigger navigation in response to an action by control-dragging from the component that would fire the action to the new view controller and selecting show under Action Segues .


1 Answers

The most common, and natural iPhone OS, way of doing this is to add a UITabBarController to your application. The Xcode template Tab Bar Application will guide you in the right direction on how to use it.

But...

If you don't like to have a Tab Bar in your application, and wish to switch between different UINavigationController instances (or any UIViewController for that matter), you can do something like this.

First you need to create your UINavigationController instances in a appropriate place (for example a new view controller, or in you Application Delegate, if you want to take the easy way out). You can then switch between controllers by just swapping which Navigation Controller's view that should be visible.

Example in the Application Delegate, "firstNavigationController" and "secondNavigationController" are UINavigationController instance variables:

- (void)showFirstNavigationController {
    [secondNavigationController.view removeFromSuperview];
    [self.window addSubview:firstNavigationController.view];
}

This will simply display the first instead of the second Navigation Controller. Note that this example is very simple. I didn't take into consideration that you should correctly handle the methods viewWillAppear:, viewDidAppear: and so on.

like image 136
alleus Avatar answered Oct 21 '22 09:10

alleus