开发者

Creating a navigation controller that doesn't show up on startup

I want to do this simple开发者_StackOverflow中文版 thing: create a uinavigationcontroller but that doesnt show up on launch. Let's say I want to have a welcome screen with a "Go" button that leads to the uinavigationcontroller. In all the examples that I have seen it looks like the navigationcontroller appears right away. How should I go about doing that?

Thanks!


If by "show up" you mean using a navigation controller with no visible footprint, that's easy. Simply do this in the root view controller:

// Root
- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

For the child view controllers, you need to do something similar in order to display the navigation bar when they appear.

// Child
- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

This is a pretty common technique for apps with main menus where you don't want to display a navigation bar in the main menu view.


The mecanic of displaying the navigation controller's view takes place in the -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions; method, in the app delegate. There the NC is added as the rootViewController of the window.
If you want to display another one, just set your custom view controller right in place of the NC and then switch the two view controllers (replace the first custom view controller with the NC) in the action method called when the button is pressed.
Assuming myCustomController defines a UIButton property called touchButton :

// in the app delegate
-(BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //self.window.rootViewController = self.navigationController;
    [self.myCustomController.touchButton addTarget:self
                                            action:@selector(switchVC)
                                  forControlEvents:UIControlEventTouchUpInside];
    self.window.rootViewController = self.myCustomController;
    [self.window makeKeyAndVisible];
    return YES;
}

Now write in your app delegate an action method :

-(void)switchVC {
    self.window.rootViewController = self.navigationController;
}


in viewcontroller that shows go hide navigation bar and in other show navigation bar

[self.navigationController setNavigationBarHidden:YES];

hope this will help

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜