简体   繁体   中英

Load a UINavigationController as a subview from UIViewController

I am a newbi in iPhone development.I want to build an app which will have a UIViewController first , which will have a button.Now on clicking the button, it shud load a UINavigation controller. Here is how im approaching :

  1. i created a UIViewController class, where i took a

     -(IBAction) PressMeFunc:(id) sender 

    for the button to be pressed.

  2. Then i created a UIView xib file.I did the required steps in the IB.

  3. Then in the AppDelegate, i added the ViewController's instance as a Subview of the window.

Upto this it is OK.

Next, how do i load a navigationcontroller on the press of the button?

I know how to build a navigationController project from window-based app, but i am having a tough time doing NavigationController as a subview of UIView.

Your help is much appreciated.

The NavigationController is designed to take over the screen when it's used, so you have to decide how to manage the transition to the NavigationController from your original ViewController. You can do this with presentModalViewController, or by handling removing your original view and swapping the NavigationController in programatically.

Here's the Apple documentation for setting up a NavigationController programatically.

The code is going to look something like this (from Apple's doc):

GroupsController *groupsController = [[[GroupsController alloc] initWithNibName:nil bundle:nil] autorelease];
UINavigationController *navigationController =
[[UINavigationController alloc] initWithRootViewController:groupsController];

Now, once you've created the NavigationController, and added its first viewcontroller to it, you need to transition to it. You can do that with CATransitions, or with

[myViewController presentModalViewController: navigationController];

You shouldn't add a UIViewController's (including navigation) view as a subview to a view managed by another view controller.

Here's a relevant read: http://blog.carbonfive.com/2011/03/09/abusing-uiviewcontrollers/

In your case, what you can do is remove the viewcontroller's view from the window , then add the navigation controller's view to the window.

[viewController.view removeFromSuperview];
[window addSubview:navigationController.view];

You can also add it as a modal view controller as was suggested here, or you can make the first view controller a navigation controller, and onto that navigation controller's stack, push the 2nd navigation controller:

[navigationController pushViewController:secondNavigation animated:NO];

Edit: heh just noticed I'm answering a 09 question

Edit #2: This may be irrelevant to iOS 5 and the UIViewController containment thing they've added, still hadn't a chance to check it out, but if you're reading this answer, you might wanna.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM