Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a TabBar to NavigationController based iPhone app

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?

like image 423
subjective-c Avatar asked Jan 27 '09 03:01

subjective-c


4 Answers

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.

like image 128
Jonathan Watmough Avatar answered Oct 26 '22 14:10

Jonathan Watmough


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.

like image 39
Alpinista Avatar answered Oct 26 '22 14:10

Alpinista


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.

like image 24
August Avatar answered Oct 26 '22 14:10

August


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.

like image 38
lostInTransit Avatar answered Oct 26 '22 13:10

lostInTransit