开发者

How do I switch between Navigation Controllers?

开发者 https://www.devze.com 2022-12-30 22:22 出处:网络
Situation: I have an Xcode project b开发者_C百科ased on the \"Navigation-Based-Application\" template. So that means I have a SINGLE UINavigationController that manages a UIViewController.

Situation:

I have an Xcode project b开发者_C百科ased 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号