iOS学习笔记——导航栏与标签栏结合

1.直接在根视图控制器上显示标签栏和导航栏

只有一个根视图控制器,在AppDelegate.m文件中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)lanuchOptions方法中添加如下代码:

    //创建导航栏对象
    UINavigationController *pNavigation = [[UINavigationController alloc]initWithRootViewController:self.viewController];
    //创建另外一个类的对象
    LinAnotherViewController * pAnotherVC = [[LinAnotherViewController alloc]initWithNibName:nil bundle:nil];
    //创建Tabbar对象
    UITabBarController *pTabBar = [[UITabBarController alloc]init];
    //初始化数组,存储标签栏的内容
    NSArray *pArray = [NSArray arrayWithObjects:pNavigation,pAnotherVC, nil];
    //把数组中内容传递给标签栏控制器
    pTabBar.viewControllers = pArray;
    //设置根视图控制器
    self.window.rootViewController = pTabBar;

此时视图都为空,可按照之前学习的方法添加相应的控件、图片、设置导航栏、标签栏的属性等。

2.新建根视图控制器,显示切换界面后的标签栏和导航栏

当需要在页面跳转后显示标签栏和导航栏,此时应该设置新的根视图,在它上面编写创建相应的视图控制器、导航栏,再添加到标签栏控制器中。需要实现AppDelegate的委托创建新的根视图。
首先是导入新视图的名称, 特别是:LinAppDelegate.h文件
#import "LinFirstViewController.h"
#import "LinSecondViewController.h"
#import "LinThirdViewController.h"
//委托协议代理
#import "LinAppDelegate.h"
重写- (void)viewDidLoad方法:
- (void)viewDidLoad
{
    [super viewDidLoad];
    //创建视图对象和相应的导航栏对象,假设均有导航栏
    LinFirstViewController * pFirstVC = [[LinFirstViewController alloc]initWithNibName:nil bundle:nil];
    UINavigationController * pFirstNavigation = [[UINavigationController alloc]initWithRootViewController:pFirstVC];
    LinSecondViewController * pSecondVC = [[LinSecondViewController alloc]initWithNibName:nil bundle:nil];
    UINavigationController * pSecondNavigation = [[UINavigationController alloc]initWithRootViewController:pSecondVC];
    LinThirdViewController * pThirdVC = [[LinThirdViewController alloc]initWithNibName:nil bundle:nil];
    UINavigationController * pThirdNavigation = [[UINavigationController alloc]initWithRootViewController:pThirdVC];
    //初始化数组,存储导航控制器
    NSArray * array = [[NSArray alloc]initWithObjects:pFirstNavigation, pSecondNavigation, pThirdNavigation, nil];
    //初始化标签栏控制器
    UITabBarController * pTabBar = [[UITabBarController alloc]init];
    //设置标签栏中视图控制器(数组)
    pTabBar.viewControllers = array;
    //根据委托协议调用方法
    [self goInNemView:pTabBar];
}
//构造委托协议的方法,把标签控制器放在新的根视图中
- (void)goInNemView:(id)sender
{
    //获取当前程序
    UIApplication * app = [UIApplication sharedApplication];
    //创建应用程序的委托对象
    LinAppDelegate * pDelegate = app.delegate;
    //设置新的根视图控制器,用委托的方法实现代理
    pDelegate.window.rootViewController = sender;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值