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;
}