How to add a TabBar to NavigationController based iPhone app

烂漫一生 提交于 2019-11-30 10:52:23


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?


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.


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.


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.


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.


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.


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...