---
title: "iOS Dev (15) TabBarController + NavigationController基础示例"
author: "prevention"
date: "2014-01-05T00:55:00Z"
tags: []
slug: "ios-dev-15-tabbarcontroller-navigationcontroller"
draft: false
lastmod: "2014-02-03T16:56:21Z"
id: 2010880
url: "https://prevention.iteye.com/blog/2010880"
---

# iOS Dev (15) TabBarController + NavigationController基础示例

- 作者：CSDN 大锐哥
- 博客：[http://blog.csdn.net/prevention](http://blog.csdn.net/prevention)

## 如何把 TabBarController 和 NavigationController 组织到一起？

整个 APP 的 root 是 TabBarController。TabBarController 有一个数组类型的属性，叫 viewControllers。我们在 TabBar 这个数组属性中，装填一些 ViewController。

在一般的应用中（比如微信、微博、QQ、Twitter、Instagram 等），TabBarController 中套的都是 NavigationController。这些 NavigationController 就是装填在 TabBarController 的 viewControllers 属性中的。每个 NavigationController 可以用 initWithRootViewController 来指定一个 ViewController，可能是自定义的，可能是系统提供的，这里可以做你自己想做的东西。

说了一堆废话，SHOW ME THE CODE!

```
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIScreen mainScreen] bounds];

    LeftViewController *leftVC = [[LeftViewController alloc] init];
    RightViewController *rightVC = [[RightViewController alloc] init];

    leftVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemContacts tag:0];
    rightVC.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:1];

    UINavigationController *leftVC = [[UINavigationController alloc] initWithRootViewController:leftVC];
    UINavigationController *rightVC = [[UINavigationController alloc] initWithRootViewController:rightVC];

    UITabBarController *tabBar = [[UITabBarController alloc] init];
    tabBar.navigationControllers = [[NSArray arrayWithObjects:leftVC, rightVC, nil]];

    self.window.rootViewController = tabbar;
    [self.window makeKeyAndVisible];
    return YES;
}
```

当然喽，上面的 LeftViewController 和 RightViewController 都是你自定义的。不过，你也可以直接用 UIViewController。

## 再啰嗦两句

先有两个 ViewController 吧，然后每个 ViewControler 的 TabBarItem 都得指定一下，毕竟得有一个触发点，而这一切都被系统给封装好了，只剩下一句话需要你写。

然后在把这两个 ViewController 指定给一个 TabBarController 的 viewControllers 属性。

\-

转载请注明来自：[http://blog.csdn.net/prevention](http://blog.csdn.net/prevention)