45

I have a simple iPhone app that has a tab bar and 3 tabs. Each tab loads as a separate nib with a corresponding controller. Each nib contains a tableview with some other controls for searching/filtering etc.

What I would like (and cant seem to find an example of) is adding a navigation controller to the app so each nib (i.e. each tab) can drill down to further detail pages.

Should I be adding a navigation controller to the main window and creating an IBOutlet for it or should the NC be added to the nibs.

sudhansu63
  • 6,025
  • 4
  • 39
  • 52
  • I have the same problem, voting u up for this question and lots of answers, thanks @stackoverflow for this awesome forum – abdulrauf618 Mar 05 '15 at 16:28

8 Answers8

63

Once you have a tab bar in a XIB, the easiest way to approach this is to drag a UINavigationController object over from the Library window (looks like a left nav bar button on a gold background) into the Tree View for your tab bar (the text only view, not the GUI). Place the navigation controller inside the tab bar controller, then drag your existing view controller inside the navigation controller.

When you go to view that tab you should then see a navigation bar on the top of it... if you are loading the navigation controller from another xib, you'll modify the nav bar in the tab bar xib.

alsuhr
  • 129
  • 2
  • 11
Kendall Helmstetter Gelner
  • 74,769
  • 26
  • 128
  • 150
  • 1
    Why didn't Apple mention this in the UINavigationController reference? It's a great trick! – Abramodj Mar 12 '11 at 11:08
  • Why didn't apple provide a sample? I found this sample which seems to work with previous XCode versions but not with xcode 4.6+: http://21gingerman.wordpress.com/2009/04/06/tutorial-and-sample-code-for-iphone-app-with-tab-bar-and-nav-bar/ – Warren P Feb 22 '13 at 14:54
  • Does this embed all remaining tab bar view's in Navigation Controllers as well? Does anyone know if this causes problems/still functions in iOS7? – Chisx Jan 03 '14 at 05:46
  • No, only adds a nav controller for that tab. You need a nag controller for each tab you want navigation in. it should still work as a technique. – Kendall Helmstetter Gelner Jan 04 '14 at 02:43
27

This is how to add the NavigationController programmatically (e.g. in you AppDelegate)

  UIViewController  *viewController1, *viewController2;

    viewController1 = [[[UIViewController alloc] initWithNibName:@"FirstViewController_iPhone" bundle:nil] autorelease];
    UINavigationController *navigationcontroller = [[[UINavigationController alloc] initWithRootViewController:viewController1] autorelease];

    viewController2 = [[[UIViewController alloc] initWithNibName:@"SecondViewController_iPhone" bundle:nil] autorelease];

    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:navigationcontroller, viewController2, nil];

If you have a tableview in the Viewcontroller1 you can access the Navigationcontroller by using self.navigationController e.g. in the didSelectRowAtIndexPath to open a DetailView

eg.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
}

In this example the detailViewController inits with a NIB file, which adds a navigation bar with a back button. But if you init the detailViewController programmatically aswell, you can add an action (GoBack) to a UIBarButtonItem to remove the detailViewController from its navigationcontroller.

-(void) GoBack
{    
    [self.navigationController popViewControllerAnimated:YES];
}
Max B.
  • 881
  • 10
  • 21
  • I am new on iphone development. I wrote into viewDidLoad that starting UIViewController *viewController1, *viewController2; . but my app have error. What can I do? How will I use this code ? – Erhan Demirci Apr 11 '13 at 18:53
  • 1
    Put the code in your Appdelegate in the didFinishLaunchingWithOptions function. – Max B. Apr 12 '13 at 12:58
  • So the first item in the tab bar controller shows the navigation bar, but the second item does not. – The Muffin Man May 23 '13 at 03:09
13

I realized this is late, but this question is #1 on google results today and so I figured I'd add an answer. There is a great youtube video tutorial by O'Reilly that walks you through a tabbar + navigation controller app. Check it out here: http://www.youtube.com/watch?v=LBnPfAtswgw

dnstevenson
  • 697
  • 7
  • 16
9

lots of answers, but none checked ...

of course, this is all now super easy with storyboards in Xcode interface builder:

create the Navigation Controller of interest, then choose menu Editor and item Embed In > and then Tab Bar Controller .

this even worked with the navigation controllers that Xcode presents you when you start with the master/detail split-view-controller template for a new project. in both cases, i now have a tab bar controller as my master; for iPhone, it ripples all the way through the end controllers, and on iPad, it keeps the tabs only in the master (i.e. the split on the right in landscape, and the popover in portrait).

john.k.doe
  • 7,533
  • 2
  • 37
  • 64
9

The way to think about it is this:

You want each tab to have a navigation controller. Then, each nav controller needs to have a root view controller and a loaded Nib.

So, in IB, add nav controllers for each of your tabs. Then, configure each nav controller to have the appropriate root view controller and Nib.

August
  • 12,139
  • 3
  • 29
  • 30
1

Open the XIB file of your UITabBarController (from MainWindow.xib)

Go to 'Tab Bar Controller Attributes'. You ll see a 'View Controllers' section there, where the view controllers of each tab will be mentioned. Just change the type of controller to NavigationController for whichever tab you want.

Select the view of the controller you have changed, and check for the loadFromNib option in its attributes, and select the correct nib. ( as after changing the kind of controller, this connection would have been lost )

dsaw
  • 577
  • 5
  • 12
0

The question is too old but SDKs are upgraded and some readers might need new answer. So, now for storyboard users, the answer to use both Tabbar and Navigation controller is pretty simple.

  1. Drag Navigation Controller inside storyboard.
  2. Assign the startup arrow to the new Navigation Controller making the controller as a starting point.
  3. Drag Tabbar controller and set it up with tabs and required controller. You can set up tabbar controller with tabs later also when you wish to do.
  4. Select Navigation controller and bind its 'root viewcontroller' with Tabbar Controller.

This will load the application with both navigation and tabview controllers.

Paresh Thakor
  • 1,795
  • 2
  • 27
  • 47