2012-09-05 13 views
5

Appleによれば、UINavigationControllerUITabBarControllerをコードを使用して組み合わせることができます。この設定でタブバーごとに個別のUINavigationControllerを使用する必要があります

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; 

、唯一VC4UINavigationControllerを持っていますが、私は、VC1~VC3をしたい場合UINavigationControllerを持って何?、私のような行う必要があります。..

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; 

はこれです正しいアプローチ?

+0

これはあなたが望むものであるかどうかによって決まります...私はアプローチが間違っているとは言いませんが、おそらくそれはあなたが期待するかもしれない結果を与えないかもしれません。あなたの目標は何ですか? – Saphrosit

+0

複数のビューを1つのTabでナビゲートする必要がある場合は、navigationControllerを使用する必要があります。単一のViewControllerをTabごとに表示する場合は、navControllerを使用しないでください。それはあなたの要求が何であるかによって異なります。 –

答えて

1
- (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); 
} 

あなたはこれを試すことができます....

+0

はそうであってはなりません: 'retval = [NSArray arrayWithObjects:navigationController1、navigationController2、navigationController3、navigationController4、navigationController5、nil];' – viral

0

あなたTabBarControllerのタブごとにUINavigationControllerを持っている必要があります。したがって、2番目のアプローチは正しいです。私はあなたがすべてのタブのために同じナビゲーションコントローラを再利用できるとは思わない。

0

はい、あなたのアプローチは正しいです。

ビューをタブに移動する必要がある場合、そのタブにはナビゲーションコントローラが必要です。

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

ナビゲーション内のタブはコントローラの必要はありません。

[arrTabs addObject:firstTabViewCtrl]; 
2

はいHoward、あなたのアプローチは問題ありません。 Appleもこれを示しています。私はまたUITabbarControllerUINavigationControllerと一緒に働いている間も同じアプローチに従います。

関連する問題