views:

12234

answers:

7

I have a simple NavigationController based app. The main window shows a TableView and selecting an item loads a sub-view. I used Interface Builder for UI views.

Now I want to add a TabBar to the app. Where do I put it? Do I need a TabBarController? Should it go in the MainWindow.xib, or RootViewController.xib?

How do I tie it together with my NavigationController?

+1  A: 

Take a look at the UICatalog sample code. It has lots of great stuff in it, including the transition from one view to the next.

In your case, you need to use the UITableView delegate method

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

In that method, you'll create and display your tab view controller. This could be a custom controller with an associated Nib, or you could create the whole thing in code depending on your circumstance.

August
A: 

Simple answer is to make it a tab bar app that loads navigation controllers.

The tab bar will be your highest level component, then you can load navigation controllers for each tab. From there it should be fairly standard stuff. I had to learn this one the hard way a while ago.

You can start with the basic Tab Bar application template in XCode, it should setup a basic tab bar with 2 simple views.

Ryan Townshend
Right, but how do I do that?
subjective-c
A: 

It's a better idea to have a navigation bar embedded in a tabbed application whenever possible. Doing it the other way creates some complications, is difficult when done with IB and leads to a lot of trouble.

lostInTransit
Are you saying I should create a new project based on TabBarController and then integrate my navigation controller code into it? I would prefer to keep my existing project, but if it's going to create a lot of trouble I guess I can start a new one.
subjective-c
+3  A: 

I do this in a couple of my apps. The trick to adding a tab bar to a navigationController based app is to NOT use a TabBarController. Add a Tab Bar to the view, make the view controller for that view a TabBarDelegate, and respond to user selections on the tab bar in the code of the view controller.

I use Tab Bars to add additional views to the Tab Bar's view as sub-views, to reload a table view with different datasets, to reload a UIPickerView, etc.

Alpinista
What if you have more than 5 tabs and want to allow the user to "edit" the primary tabs via the more... button? This is something you get for free with the tab controller
Rob Fonseca-Ensor
I don't know the answer to that. I've never used more than 4 tabs in a tabBar, and never needed to allow users to edit the bar. I just know that you can't really add a TabBarController to a NavigationBar based app
Alpinista
can u show the sample code?
iPhoneDev
A: 

Answering my own question - I did not use CGRectMake in my init, so it was hiding away in a corner of the UI

Sid
Should be a comment
Casebash
+1  A: 

You can use Interface Builder to edit MainWindow.xib, and add a Tab Bar Controller to your Navigation Controller. You can also change UITabBarController's view controllers to View/Navigation/Table View/Image Picker Controllers with IB's Inspector window. It will work.

However, I don't really suggest putting Tab Bar Controller into Navigation Controller. The relationship between these controllers will get confused if you go further (make it more complex).

Assuming you have Table View Controller in your Tab Bar Controller, and want to bring up the detail view. By design, you might want to tell the Navigation Controller to push view into stack, but doing this will make detail view becomes same level with your Tab Bar Controller (both under Navigation Controller) and Table View Controller hold the relation as detail view's parent. This is quite strange, at least to me...

digdog
Could you replicate or update the content which is no longer available?
dlamblin
+3  A: 

The definitive answer to this question is presented in the iPhone SDK documentation in the View Controller Programming Guide for iPhone.

View Controller Programming Guide: Combining View Controllers

This link goes straight to the section and includes a discussion of the correct ways to create navigation and tab bar-based interfaces.

Jonathan Watmough