5

According to Apple, I can combine UINavigationController and UITabBarController using the code, e.g.

MyViewController1* vc1 = [[MyViewController1 alloc] init];
MyViewController2* vc2 = [[MyViewController2 alloc] init];
MyViewController3* vc3 = [[MyViewController3 alloc] init];

MyNavRootViewController* vc4 = [[MyNavRootViewController alloc] init];
UINavigationController* navController = [[UINavigationController alloc]
                        initWithRootViewController:vc4];

NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, vc3, navController, nil];
tabBarController.viewControllers = controllers;

In this setup, only vc4 has the UINavigationController, but what if I want vc1-vc3 also has the UINavigationController?, should I do like..

MyViewController1* vc1 = [[MyViewController1 alloc] init];
UINavigationController* nv1 = [[UINavigationController alloc]
                        initWithRootViewController:vc1];

MyViewController1* vc2 = [[MyViewController2 alloc] init];
UINavigationController* nv2= [[UINavigationController alloc]
                        initWithRootViewController:vc2];

MyViewController1* vc3 = [[MyViewController3 alloc] init];
UINavigationController* nv3 = [[UINavigationController alloc]
                        initWithRootViewController:vc3];


NSArray* controllers = [NSArray arrayWithObjects:nv1, nv2, nv3, nil];
tabBarController.viewControllers = controllers;

Is this the right approach?

Howard
  • 19,215
  • 35
  • 112
  • 184
  • Well, it depends if this is what you want...I would not say the approach is wrong, but maybe it does not give the result you may expect and/or there are better approaches. What is your goal? – Manlio Sep 05 '12 at 11:17
  • if you have to navigate through many views in a single Tab, then you would require use the navigationController. If you have single ViewController to be shown per Tab, then dont use navigationController. It all depends on what your requirement is. – Vimal Venugopalan Sep 05 '12 at 11:55

4 Answers4

2
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];


// Override point for customization after application launch.

self.tabBarController = [[[UITabBarController alloc] init] autorelease];

 self.tabBarController.viewControllers = [self initializeTabBarItems];
self.navigationController = [[UINavigationController alloc]init];
[self.navigationController setNavigationBarHidden:YES];
self.window.rootViewController = self.navigationController;
[self.navigationController pushViewController:_tabBarController animated:YES];
[self.window makeKeyAndVisible];
return YES;
}
- (NSArray *)initializeTabBarItems
{
NSArray * retval;

/* Initialize view controllers */
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil] autorelease];
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil] autorelease];
UIViewController *viewController3 = [[[ThirdViewController alloc]initWithNibName:@"ThirdViewController" bundle:nil]autorelease];
UIViewController *viewController4 = [[[FourthViewController alloc] initWithNibName:@"FourthViewController" bundle:nil] autorelease];
UIViewController *viewController5  = [[[FivfthViewController alloc] initWithNibName:@"FivfthViewController" bundle:nil] autorelease];


/* Initialize navigation controllers */
UINavigationController * navigationController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];
UINavigationController * navigationController2 = [[UINavigationController alloc] initWithRootViewController:viewController2];
UINavigationController * navigationController3 = [[UINavigationController alloc] initWithRootViewController:viewController3];
UINavigationController * navigationController4 = [[UINavigationController alloc] initWithRootViewController:viewController4];
UINavigationController * navigationController5 = [[UINavigationController alloc] initWithRootViewController:viewController5];

/*  Release View Controllers */
[viewController1 release];
[viewController2 release];
[viewController3 release];
[viewController4 release];
[viewController5 release];

/* Stuff Navigation Controllers into return value */
retval = [NSArray arrayWithObjects:viewController1,viewController2,viewController3,viewController4,viewController5,nil];

/* Release Navigation Controllers */
[navigationController1 release];
[navigationController2 release];
[navigationController3 release];
[navigationController4 release];
[navigationController5 release];

return (retval);
}

You Can Try This ....

Buntylm
  • 7,345
  • 1
  • 31
  • 51
  • shouldn't it be: `retval = [NSArray arrayWithObjects: navigationController1, navigationController2, navigationController3, navigationController4, navigationController5,nil];` – viral Apr 16 '13 at 10:52
2

Yes Howard, your approach is fine. Apple says this too. I also follow same approach while working with UITabbarController with UINavigationController and it works great for me.

Nitish
  • 13,845
  • 28
  • 135
  • 263
0

You should have one UINavigationController per tab of your TabBarController. So your 2nd approach is correct. I don't think that you can reuse the same navigation controller for all the tabs.

Resh32
  • 6,500
  • 3
  • 32
  • 40
0

Yes your approach is correct.

If u have to navigate views into tab then that tab should have navigation Controller.

UINavigationController * navigationCtrl = [[UINavigationController alloc] initWithRootViewController:firstTabViewCtrl];
[arrTabs addObject:navigationCtrl];

Either wise there is no need of navigation Controller inside tab.

 [arrTabs addObject:firstTabViewCtrl];
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132