UITabBarController与UINavigationController异同点浅谈

本文介绍了如何使用UITabBarController和UINavigationController管理iOS应用中的视图控制器,包括通过TabBar实现页面间的切换,以及如何在一个TabBar控制器中嵌套Navigation控制器。以微信为例,展示了具体的代码实现。

UITabBarController与UINavigationController都可以管理多个视图控制器,并且完成视图控制器的切换,并且UITabBarController中可以嵌套UINavigationController,UINavigationController中也可以嵌套UITabBarController

UITabBarController管理多个视图控制器(被管理的视图控制器是平级关系),通过TabBar上的标签切换显示被管理的controller的页面,而UINavigationController则是有上一个页面推出下一个页面

以微信作为例子,在UITabBarController中可以嵌套UINavigationController

    //创建被tabVC管理的视图控制器
    //微信
    VchatViewController *vVC = [[VchatViewController alloc] init];
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:vVC];
    vVC.navigationItem.title = @"微信";
    //标签标题
    vVC.tabBarItem.title = @"微信";
    //设置图片
    vVC.tabBarItem.image = [UIImage imageNamed:@"08-chat.png"];
    //标签栏标记
    vVC.tabBarItem.badgeValue = @"5";
    
    //通讯录
    AddressBookViewController *addVC = [[AddressBookViewController alloc] init];
    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:addVC];
    addVC.navigationItem.title = @"通讯录";
    addVC.tabBarItem.title = @"通讯录";
    addVC.tabBarItem.image = [UIImage imageNamed:@"24-gift.png"];
    
    
    //发现
    DiscoverViewController *disVC = [[DiscoverViewController alloc] init];
    UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:disVC];
    disVC.navigationItem.title = @"发现";
    disVC.tabBarItem.title = @"发现";
    disVC.tabBarItem.image = [UIImage imageNamed:@"06-magnifying-glass.png"];
    disVC.tabBarItem.badgeValue = @"8";
    
    //我
    MeViewController *meVC = [[MeViewController alloc] init];
    UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:meVC];
    meVC.navigationItem.title = @"我";
    meVC.tabBarItem.title = @"我";
    meVC.tabBarItem.image = [UIImage imageNamed:@"29-heart.png"];
    
    //设置
    SettingViewController *setVC = [[SettingViewController alloc] init];
    UINavigationController *nav5 = [[UINavigationController alloc] initWithRootViewController:setVC];
    setVC.navigationItem.title = @"设置";
    setVC.tabBarItem.title = @"设置";
    setVC.tabBarItem.image = [UIImage imageNamed:@"19-gear.png"];
    
//    //登陆
//    LoginViewController *logVC = [[LoginViewController alloc] init];
//    logVC.tabBarItem.title = @"登陆";
//    logVC.tabBarItem.image = [UIImage imageNamed:@"38-airplane.png"];
    
    
    //设置TabBar管理的controller
    //当controller超过5个的时候,刺痛会自动增加哦一个”more“标签,管理第五个和五个以上的controller,没有出现在底部的controller会以列表的形式通过点击more展示出来
    tabVC.viewControllers = @[nav1, nav2, nav3, nav4, nav5];
    //设置tabBarController代理
    tabVC.delegate = self;
所显示的页面是

当点击某一个标签时,便会切换到标签对应的页面



UINavigationController的推出视图的方法是:这是由当前页面推出ThreeViewController控制的页面

- (void)goToNextVC:(UIButton *)btn{
        ThreeViewController *threeVC = [[ThreeViewController alloc] init];
        [self.navigationController pushViewController:threeVC animated:YES];
}

返回上一个页面

- (void)goBackPreviousVC:(UIButton *)btn1{
     [self.navigationController popViewControllerAnimated:YES];
}

以上所述只是对两者浅显的对比,我会继续研究的奋斗



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值