混合使用這兩個控件的好處是我們可以在NavigationBar添加更多的東西,如標題,按鈕等。讓用戶能夠獲得更多的信息。
UITabBarController的屬性ViewControllers接受以UIViewController或者UIViewController子類為元素的數組。
因為UINavigationController屬於UIViewController的子類,因此它當然就可以成為viewControllers的參數。
先來看效果:
原理和之前文章所說的基本一樣:
實現代碼:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //創建第一個視圖控制器 self.firstViewController = [[UIViewController alloc] init]; self.firstViewController.view.backgroundColor = [UIColor brownColor]; self.firstViewController.title = @"first view"; self.firstViewController.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemDownloads tag:1]; //創建第二個視圖控制器 self.secondViewController = [[UIViewController alloc] init]; self.secondViewController.view.backgroundColor = [UIColor yellowColor]; self.secondViewController.title = @"second view"; self.secondViewController.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:2]; //初始化第一個UINavigationController self.firstNavController = [[UINavigationController alloc] initWithRootViewController:self.firstViewController]; //初始化第二個UINavigationController self.secNavController = [[UINavigationController alloc] initWithRootViewController:self.secondViewController]; //初始化UITabBarController self.tabBarController = [[UITabBarController alloc] init]; //為viewControllers添加引用 self.tabBarController.viewControllers = @[self.firstNavController, self.secNavController]; self.window.rootViewController = self.tabBarController; return YES; }
在iPhone上很多應用程序幾乎都是以這種結構布局,實用性大,使用簡單。


