导航控制器

导航控制器负责控制导航栏(navigationBar),导航栏上的按钮叫UINavigationItem(导航元素项)。它还控制着一个视图控制器,即导航栏下面的东西。
导航控制器基础
#import "SceneDelegate.h"
#import "VCRoot.h"
@interface SceneDelegate ()
@end
@implementation SceneDelegate
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
//创建一个根视图控制器
VCRoot* root = [[VCRoot alloc] init];
//创建导航控制器
//导航控制器主要用来管理多个视图控制器的切换
//层级的方式来管理多个视图控制器
//创建控制器时,一定要有一个根视图控制器
//P1:就是作为导航控制器的根视图控制器
UINavigationController* rev = [[UINavigationController alloc] initWithRootViewController:root];
//将window的根视图设置为导航控制器
self.window.rootViewController = rev;
[self.window makeKeyAndVisible];
}
新建一个VCRoot类
#import "VCRoot.h"
@interface VCRoot ()
@end
@implementation VCRoot
- (void)viewDidLoad {
[super viewDidLoad];
//设置导航栏的透明度
//默认透明度为YES:可透明的
self.navigationController.navigationBar.translucent = NO;
self.view.backgroundColor = [UIColor greenColor];
//设置导航栏的标题文字
self.title = @"娃哈哈";
//设置导航元素项目的标题
//如果没有设置元素项目的标题,系统会使用self.title作为标题;反之,优先为navigationItem.title
self.navigationItem.title = @"娃哈哈1";
//向左侧按钮中添加文字,这里是根据title文字来创建
//P1:栏按钮项的标题
//P2:按钮的样式
//P3:接受动作的目标对象
UIBarButtonItem* leftBtn = [[UIBarButtonItem alloc] initWithTitle:@"旺仔牛奶" style:UIBarButtonItemStyleDone target:self action:@selector(pressLeft)];
self.navigationItem.leftBarButtonItem = leftBtn;
//右侧按钮中的文字是不可变的
//这里按钮是制定了系统提供的风格样式
//P1:按钮中展现的东西,注意,这里无论按钮中展现的是什么内容(无论图案或者文字),都是不可改变的
UIBarButtonItem* rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(pressRight)];
//向右侧添加自定义按钮
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 50, 40)];
label.text = @"矿泉水";
//将文字调至中间位置
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor blackColor];
//UIView的子类都可以被添加
UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:label];
//数组展现顺序从右至左
NSArray* array = [NSArray arrayWithObjects:item, rightBtn, nil];
//将右侧按钮数组赋值
self.navigationItem.rightBarButtonItems = array;
//self.navigationItem.rightBarButtonItem = rightBtn;
}
-(void) pressLeft
{
NSLog(@"按下了左侧按钮");
}
-(void) pressRight
{
NSLog(@"按下了右侧按钮");
}
效果图:

导航控制器切换
navigationBar:导航栏对象
navigationItem:导航元素项对象
translucent:导航栏透明度
pushViewController:推入视图控制器
popViewController:推出视图控制器
首先创建三个视图
根视图VCRoot.m:
#import "VCRoot.h"
#import "VCTwo.h"
@interface VCRoot ()
@end
@implementation VCRoot
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor greenColor];
//设置导航栏的透明度,默认为YES:可透明的;NO:不可透明的
self.navigationController.navigationBar.translucent = NO;
self.title = @"哦哦哦";
//设置导航栏的风格颜色,默认为Default
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
//为根视图的导航控制器设置右侧按钮
UIBarButtonItem* rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"下一页" style:UIBarButtonItemStylePlain target:self action:@selector(pressRight)];
self.navigationItem.rightBarButtonItem = rightBtn;
}
-(void) pressRight
{
//创建新的视图控制器
VCTwo* vcTwo = [[VCTwo alloc] init];
//使用当前视图控制器的导航控制器对象
[self.navigationController pushViewController:vcTwo animated:YES];
}
第二个视图VCTwo.h:
#import

最低0.47元/天 解锁文章
2775

被折叠的 条评论
为什么被折叠?



