`
prevention
  • 浏览: 70717 次
社区版块
存档分类
最新评论

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

 
阅读更多

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

如何把 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

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics