Adding a Navigation/Table View/Image Picker Controller into a UITabBarController seems to be subject of many new iPhone developers frustrations. I did have a lot of trouble figuring it out myself, but I finally did it. Here’s the correct way, fool proof I believe!
- To start with, add a new file, using the UIViewController template.
- Change the class’s type, on the new header file, from UIViewController to UINavigationController.
- Open a .xib file containing a UITabBarController.
- Select your Tab Bar Controller in the “your_xib_name.xib” window and press +1 to open the Attributes Inspector.
- Select Navigation/Table View/Image Picker Controller from the drop down for the correct tab.
- Go back to the “your_xib_name.xib” window, expand the Tab Bar Controller and select the correct tab.
- Press +4 to open the Identity window.
- Fill the class name with the navigation controller class that you created during steps 1 and 2. (if the class name doesn’t show up, you didn’t follow step 2, bad! bad! boy!).
- Go back to the “your_xib_name.xib” window, expand the Tab Bar Controller and also expand the correct tab.
- You should see a view controller in there, select it.
- Press +1 to open the Attributes Inspector window.
- Select a .nib for a view you wish to display when the navigation controller opens.
- Press +4 to open the Identity window.
- Fill the class name with the controller’s class for the .nib view you selected during step 12.
Or you could do it through code, like this:
UITabBarController * t =
[[UITabBarController alloc] init];
MyViewController * c = [[MyViewController alloc] init];
UINavigationController * n = [[UINavigationController alloc] initWithRootViewController:c];
[c
release];
NSArray * tabs = [NSArray arrayWithObject:n];
[n release];
[t setViewControllers:tabs];
[window addSubview:[t view]];
Voil?.¡.
Of course, you wouldn’t want your tabBar controller
to be a local variable, so it would need to be an ivar of the object. This was just to illustrate how it’s MUCH easier to do this sort of thing through code.
Cheers,
Dave
Thanks! I’ve been trying to figure out how to do exactly that for
days now.
What’s the next step here though? Specifically, when I’m in the class (from step 14) and I want to move onto the next view, how do I access the UINavigationController that was created
so that I can call pushViewController?
I
can’t remember by heart, but iirc you can use [self controller] or similar in any view controller. Check the docs.
Thanks for this, Alexandre. This has cleared up for me how to do this through Interface Builder. However, I still find it *much* easier to build these sorts of things through code: http://davedelong.com/blog/2009/05/13/adding-uinavigationuitableview-controllers-uitabbarcontroller
Dave
Just a quick note to say thank you.