tags:

views:

341

answers:

4

Hi,

I just realised that my 'root' viewController should have been a UINavigationController. Now I want to change this to be a UINavigationController instead and just curious what my best option would be. I built this view and all other views using IB if that makes a different.

I'm mostly worried that I would have to do a lot of copy/pasting and recoding to get everything right or will be it be as easy as manually editing my controller and change the extension to UINavigationController.

Thanks

A: 

Something like this? http://starterstep.wordpress.com/2009/03/05/changing-a-uinavigationcontroller%E2%80%99s-root-view-controller/

zapping
I don't have a NavigationController at all. The problem is to somehow make my non navigationcontroller into one...
willcodejavaforfood
+1  A: 

You can instantiate a trivial not-subclassed UINavigationController and give it your original UIViewController as its root controller, like:

YourRootViewController *rootViewController = [[YourRootViewController] initHoweverYouInitIt];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

and maybe an [rootViewController release];, depending on how you are going to manage memory.

MrMage
I am not really doing any init myself, that is all handled by IB and the XIB right? I would have to do all this manually in my AppDelegate?
willcodejavaforfood
That's right. But that's the way to go.
MrMage
+1  A: 

UINavigationController has an 'is-a' relationship with UIViewController, so you should be able to change its class type in Interface Builder with no additional changes.

Winder
I Currently dont have a UINavigationController at all so could you give more detail about how to set this up?
willcodejavaforfood
Select your View Controller in Interface Builder and do command-4 to get to the info tab. The top setting is the Class (currently UIViewController), try typing in UINavigationController instead of what is currently there. I have done this successfully for the view, it should hopefully work for the view controller as well.
Winder
No this does not work. There is no ViewController just a view, file's owner and first responder
willcodejavaforfood
+1  A: 

You do not convert an existing view controller into a navigation controller.

Even though UINavigationController is a subclass of UIViewController, it's task is the management of other view controllers, not the management of views themselves. You don't swap them out one for the other. Instead, you set UIViewControllers to be controlled by the nav.

To add a nav to a project in IB, open the xib and drag over a UINavigationController. Then set the navigation controller's rootControlller property to the existing UIViewController.

And you're done.

TechZen
It cant be that easy! :) is rootController something you set through IB?
willcodejavaforfood
No I don't see how I wire all this together
willcodejavaforfood