views:

9894

answers:

5

I'm triying to combine a TabBar + Navigation app.

I have 5 tab bars, 4 are listing of stuff and drill down to details views.

I try to follow this tutorial:

http://www.iphonedevforums.com/forum/iphone-sdk-development/124-view-controller-problem.html

But always get a blank view.

This is what I do, with a clean project:

  • I start with a TabBar template app.
  • I put 5 tab bar buttons.
  • I create a controller like:

    @interface FirstViewController : UINavigationController {

    }

  • I put the main window.xib on tree mode & change the selected first view to FirstViewController
  • I select the TabBar Controller in Interface builder, go to TabBar Attributes & change the class to navigation controler.
  • Select the fist view & put the nib name "SecondView"

In response, I get a blank screen.

I must add that I wanna navigate from the details views, no from the main windows.

i.e in the main window tab bar 1 is the list of people. I select a person then wanna navigate to the detail window.

A: 

Here is an tutorial that I was able to get working.

I also read the official SDK documentation on the topic: Combining Tab Bar and Navigation Controllers. Since I'm still learning, the tutorial helped me more than the docs.

NOTE: in the tutorial, i don't think you need to subclass UINavigationController, and I'm experimenting with this idea now.

bentford
+1  A: 

First, you never want to subclass UINavigationController or UITabBarController.

Second, I did not quite get what you did, but a correct sequence to create a navigation controller inside a tab bar controller is something like this:

// in MyAppDelegate.h
UINavigationController *nc1, *nc2;
FirstTabRootViewController *vc1;
SecondTabRootViewController *vc2;
UITabBarController *tbc;

// in MyAppDelegate.m
nc1 = [[UINavigationController alloc] init];
vc1 = [[FirstTabRootViewController alloc] initWithNibName:nil bundle:nil];
vc1.tabBarItem.title = @"Tab 1";
vc1.tabBarItem.image = [UIImage imageNamed:@"tab1.png"];
vc1.navigationItem.title = "Tab 1 Data";
nc1.viewControllers = [NSArray arrayWithObjects:vc1, nil];

nc2 = [[UINavigationController alloc] init];
vc2 = [[SecondTabRootViewController alloc] initWithNibName:nil bundle:nil];
vc2.tabBarItem.title = @"Tab 2";
vc2.tabBarItem.image = [UIImage imageNamed:@"tab2.png"];
vc2.navigationItem.title = "Tab 2 Data";
nc2.viewControllers = [NSArray arrayWithObjects:vc2, nil];

tbc = [[UITabBarController alloc] init];
tbc.viewControllers = [NSArray arrayWithObjects:nc1, nc2, nil];

Note that it's your view controller that controls the text/icon in the tab bar and in the navigation bar. Create a UINavigationController instance for each of your tabs; UINavigationController contains a stack of view controllers (see viewControllers property) which should contain at least one item — your root controller for that tab. Also create an UITabBarController to manage the tabs.

Of course, you can (and probably should) use interface builder instead of code to instantiate the mentioned classes and set the properties. But it's important that you understand what happens behind the scenes; interface builder is nothing more than a convenient way to instantiate and set up objects.

Hope this is helpful; please refine your question if it's not.

Andrey Tarantsov
A: 

Hi Andrey

I tried to create an iphone application with UITabBarController and some UINavigationController inside it and faced the same problems as "mamcx". With your example-code i get it to run :) Thanks a lot.

Here is how it works for me.

// YOURS
fourthNavigation = [[UINavigationController alloc ] init ];
fourthViewController = [[[FourthTabRootController alloc] initWithNibName:@"FourthView" bundle:nil] autorelease];
fourthNavigation.tabBarItem.title = @"YOURS";
fourthNavigation.viewControllers = [NSArray arrayWithObjects:fourthViewController, nil];

// Add self-defined UIViewControllers to the tab bar
tabBarController.viewControllers = [NSArray arrayWithObjects:firstNavigation,secondNavigation, thirdNavigation, fourthNavigation, nil];
// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];

The other UINavigationControllers "firstNavigation, ... " are created the same way. I load the view elements from nib-files that are connected to my subclassed UIViewController classes. You dont need to add a NavigationBar in the IB to your view, cause the UINavigationController has allready one. So you only need to set the title in "initWithNibName"

  // The designated initializer. Override to perform setup that is required before the view is loaded.
  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
    // Custom initialization
 self.title = @"YOURS";
}
return self;
}

I hope that helps.

A: 

Still getting the blank screen On Starting the application after implementing the above code. Where i 'm writing it wrong.

nc1 = [[UINavigationController alloc] init]; nc2 = [[UINavigationController alloc] init]; vc1 = [[FirstRootViewController alloc]initWithNibName:@"FirstRootViewController" bundle:nil]; vc1.tabBarItem.title = @"Item 1"; vc1.tabBarItem.image= [UIImage imageNamed:@"home.png"]; vc1.navigationItem.title = @"Tab1 Data"; nc1.viewControllers = [NSArray arrayWithObjects:vc1,nil]; vc2 = [[SecondRootViewController alloc]initWithNibName:@"SecondRootViewController" bundle:nil]; vc2.tabBarItem.title = @"Item 2"; vc2.tabBarItem.image= [UIImage imageNamed:@"home.png"]; vc2.navigationItem.title = @"Tab2 Data"; nc2.viewControllers = [NSArray arrayWithObjects:vc2,nil]; tbc = [[UITabBarController alloc]init]; tbc.viewControllers = [NSArray arrayWithObjects:nc1,nc2,nil]; [window addSubview:tbc.view]; [window makeKeyAndVisible];

success_anil
A: 

Check this great video tutorial

Building an iPhone App Combining Tab Bar, Navigation and Tab

http://www.youtube.com/watch?v=LBnPfAtswgw

Hope this help you

Andres Garcia