本节学习内容
1.导航控制器视图管理概念
2.导航控制器视图切换的原理
3.导航控制器视图切换应用
导航控制器函数
navigationBar:导航栏对象
navigationItem:导航元素项对象
translucent:导航栏透明度
pushViewController:推入视图控制器
popViewController:弹出视图控制器
创建一个视图控制器命名为VCRoot
【VCRoot.m】
#import "VCRoot.h"
#import"VCSecond.h"
@interface VCRoot()
@end
@implementation VCRoot
-(void)viewDidLoad{
[super viewDidLoad];
//设置导航栏的透明度,默认透明度为YES:可透明的,NO:表示不透明
self.navigationController.navigationBar.translicent=NO;
//导航栏风格UIBarStyleBlackOpaque;
self.navigationController.navigationBar.barStle=UIBarStyleDefault;
self.title=@"根视图";
UIBarButtonItem* next=[[UIBarButtonItem alloc]initWithTitle:@"下一级"style:UIBarButtonItemStylePlain target:self acion:@selector(pressNext)];
self.navigationItem.rightBarButtonItem=next;
}
-(void)pressNext{
//创建新的视图控制器
VCSecond* vcSecond=[[VCSecond alloc]init];
//使用当前视图控制器的导航控制器对象
[self.navigationController pushViewController:VCSecond animated:YES];
}
创建二个视图控制器命名为VCSecond,VCThird
【VCSecond.m】
#import"VCSecond.h"
#import "VCThird.h"
@interface VCSecond()
@end
@implementation VCSecond
-(void)viewDidLad{
[super viewDidLoad];
self.view.backgroundColor=[UIColor greenColor];
self.title=@"视图二";
UIBarButtonItem* btnNext=[UIBarButtonItem allock]]initWithTitle:@"第三级"style:UIBarButtonItemStylePlain target:self action:@selector(pressNext)];
self.navgationItem.rightBarButtonItem=btnNext;
}
-(void)pressNext{
VCThrid* VC=[[VCThird alloc]init];
//切换到第三个视图控制器
[self.navigationController pushViewController:vc animated:YES];
}
#import "VCThird.h"
@interface VCThird()
@end
@implemention VCThird
-(void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor=[UIColor orangeColor];
self.title=@"第三级";
//当自己设定左侧按钮时,返回按钮会被械侧按钮替换
UIBarButtonItem* btnLeft=[[UIBarButtonItem alloc]initWithTitle:@"返回" style: UIBarButtonItemStylePlain target:self action:@selector(tressBack)]
//当自己设定左侧按钮时,探马回按钮会被左侧按钮替换
self .navigationItem.leftBarButtonItem=btnLeft;
UIBarButtonItem* btnR=[[UIBarButtonItem alloc]initWithTitle:@"返回根视图" style: UIBarButtonItemStylePlain target:self action:@selector(tressRight)]
self .navigationItem.lrightBarButtonItem=btnR;
}
-(void)pressRight{
//直接返回到根视图
[self.navgationController popToRootControllerAnimated:YES];
}
-(void)pressBack{
//将当前的视图控制器弹出,返顺到上一级界面
[self.navgationController popViewControllerAnimated:YES];
}
【AppDelegate.m】
#import"AppDelegate.h"
#import"VCRoot.h"
@interface AppDelegate()
@end
@implementation AppDelegate
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
self.window=[[UIwindow alloc]initFrame:[UIScreen mainScreen].bounds];
//将VCRoot赋值给导航根视图对象,同进创建导航控制器,导航控制器又作为windows根视图
self.window.rootViewController=[[UINavigationController alloc]initWithRootViewController:[[VCRoot alloc]init]];
[self.window makeKeyAndVisible];
return YES;
}