2012-09-05 2 views
5

Apple에 따르면 UINavigationControllerUITabBarController을 코드를 사용하여 결합 할 수 있습니다.Tab Bar 당 개별 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

하나의 Tab에서 여러보기를 탐색해야하는 경우 navigationController를 사용해야합니다. 하나의 ViewController를 Tab마다 표시하려면 navigationController를 사용하지 마십시오. 그것은 모두 귀하의 요구 사항에 달려 있습니다. –

답변

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가 있어야합니다. 따라서 두 번째 방법이 옳습니다. 나는 모든 탭에 대해 동일한 탐색 컨트롤러를 재사용 할 수 있다고 생각하지 않습니다.

0

예. 올바른 방법입니다.

보기를 탭으로 이동해야한다면 해당 탭에 탐색 컨트롤러가 있어야합니다.

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

내비게이션 컨트롤러가 필요하지 않습니다.

[arrTabs addObject:firstTabViewCtrl]; 
2

예 하워드, 접근 방식이 좋습니다. Apple에도이 내용이 나와 있습니다. 나는 또한 UITabbarControllerUINavigationController으로 일하는 동안 같은 접근법을 따른다.

관련 문제