目标:TabBar下的每一個view都有自己的NavigationBar
代碼:
tabBarController = [[UITabBarController alloc] init];
FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *firstNavigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
[firstViewController release];
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *secondNavigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
[secondViewController release];
tabBarController.viewControllers = [NSArray arrayWithObjects:firstNavigationController,secondNavigationController,nil];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
interface buider裡的方法:
将Navigation Controller作為TabBar Controller的子視圖控制器。
每個TabBar的子視圖用一個Navigation Controller。
Navigation Controller的子視圖用來實作要顯示的View:将Custom Class和Nib Name改成自己建立的ViewController,在那裡寫自己實作的代碼。
原理:TabBar的ViewControllers屬性可以添加各種View Controller,包括Navigation Controller。
Navigation Controller的子視圖指定為想要實作的view Controller。