Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How combine TabBar + Navigation with XCode

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.

like image 467
mamcx Avatar asked Jan 25 '09 22:01

mamcx


People also ask

How do I add a tab bar to my navigation controller?

To add a tab, first drag a new View Controller object to the storybard. Next control-drag from the tab bar controller to new view controller and select view controllers under Relationship Segue . Your tab bar controller will update with a new tab.

What is the use of tab bar controller in Xcode?

The tab bar interface displays tabs at the bottom of the window for selecting between the different modes and for displaying the views for that mode. This class is generally used as-is, but may also be subclassed. Each tab of a tab bar controller interface is associated with a custom view controller.


2 Answers

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.

like image 57
Andrey Tarantsov Avatar answered Oct 31 '22 19:10

Andrey Tarantsov


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];
like image 39
success_anil Avatar answered Oct 31 '22 19:10

success_anil