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
{
    

    
[[UIWindow alloc] initWithFrame:[[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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值