views:

37

answers:

1

Hello,

I have been stuck on this for a few days now and it is killing me... In my viewDidLoad event, I am trying to programmatically add a full screen UINavigationController to a subview of my view controller. So far, I have only succeeded in doing two things...

1) Only a grey screen shows up
OR
2) I get something that resembles a navigation controller added to the view controller, instead of being my navigation controller from a XIB it is just a generic one... even though I loaded from the XIB. Oddly enough it is always shifted 25 pixels downward and slightly cut off.

I have read every single link on google and I can't seem to figure this out. I just created a new viewcontroller... added a UINavigationController to it... try to load that view controller and it messes up.

Any help is greatly appreciated!!!!

+1  A: 

Instead of having the UINavigationController be a child of some other view controller, make the UINavigationController the root controller itself. The navigation controller is one of the special "container" view controllers, and it generally wants to own the whole screen and be at the root of the controller hierarchy (except in certain circumstances).

Try something like this:

UINavigationController * rootNavController = [[UINavigationController alloc] initWithRootViewController:myRootControllerInTheNavController];
[window addSubview:[rootNavController view]];

Which will obscure any existing views with the nav controller (those existing things will still be there when you -removeFromSuperview the nav controller's view). The nuclear option is to set your UIWindow's rootViewController property with the nav controller, but it sounds from your comment that this may not be what you want to do here.

Possibly a cleaner approach: If it accomplishes what you want, I believe you could also take your nav controller and present it modally (see docs for uiviewcontroller) from whatever the current view controller is. Set the transition appropriately, and while you're in the nav stack, the nav controller will be visible.

quixoto
My entire application doesn't depend on this controller... just a couple pages. How do you go about making it the root? I would need to hand it control from the controller that owns the screen, I guess???
Dave C
(See update...)
quixoto
Thanks for the great response!!!The prsenting modally is exactly the kind of thing that i would like to do but it is what is giving me a grey screen... let me show you a cut of code: LiteViewController *lite = [[LiteViewController alloc] initWithNibName:@"LiteViewController" bundle:nil]; [self presentModalViewController:lite animated:YES]; LiteViewController is my controller with my UINavigationController... it does not have a window or view attached to it - it only has a child view attached, not a main one.... maybe that is why it is going grey.
Dave C
Is LiteViewController a subclass of UINavigationController? In this case, it should be. Lite shouldn't be a generic controller with a *child* nav controller.
quixoto
Opps, It seems that LiteViewController was a subclass of UIViewController... let me change that and try a few things... great catch.
Dave C
Well that made progress... the prsent modal still shows a grey screen; however, if I do addsubview it is loading a navigation controller to the view but it is not creating it from the nib... it is just a generic navigation controller. ------------- LiteViewController *lite = [[LiteViewController alloc] initWithNibName:@"LiteViewController" bundle:nil]; [self.view addSubview:lite]; ---------- Sorry I do not know how to do carriage returns in comments.
Dave C
For anyone else tying to figure this out, i just ended up scraping my NIB and doing all of the layout stuff programmatically... Thanks a ton for the help in getting this thing to show up Quixoto
Dave C