1. UINavigationController:
补充的一些:
1.---继承于UIViewController,以栈的方式管理视图控制器,至少得有一个被管理的视图控制器,称作:根视图控制器
2.---始终显示栈顶的视图控制器里的视图
3.---push 入栈 pop 出栈
4.---navigationBar是同一个,无论你在第几个子试图控制器里隐藏 当返回的时候之前的控制器也会消失(可以在返回之前的视图时将bar还原)
5.---initWithBarButtonSystemItem:target:action: 这个UIBarButtonItem上的初始化方法:只是需要设置button的样式,其他的都一样
6.---tintColor:是UINavigationBar里,按钮的颜色
7.---navigationBar—导航条,iOS7之后默认是透明的,iOS7之前默认是不透明的。
navigationBar在透明情况,与contentView会重合一部分区域。
navigationBar在不透明情况,contentView跟在navigationBar的下面。
navigationBar竖屏下默认⾼高度44,横屏下默认⾼高度32
AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
RootViewController *rootVC = [[RootViewController alloc] init];
// 1. 初始化:创建naVC的根视图
UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
// 设置上方的导航栏是否透明(同时也会改变视图原点的位置)
naVC.navigationBar.translucent = NO; // 让导航栏变成不透明
// navigationBar是否隐藏
// naVC.navigationBar.hidden = YES;
self.window.rootViewController = naVC;
return YES;
}
RootView.m:
@implementation RootView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addViews];
}
return self;
}
- (void)addViews {
self.backgroundColor = [UIColor whiteColor];
self.button = [UIButton buttonWithType:UIButtonTypeCustom];
self.button.frame = CGRectMake(50, 50, 200, 50);
self.button.layer.cornerRadius = 20;
self.button.backgroundColor = [UIColor blueColor];
[self.button setTitle:@"button" forState:UIControlStateNormal];
[self addSubview:self.button];
self.viewLocation = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 200)];
self.viewLocation.backgroundColor = [UIColor lightGrayColor];
self.viewLocation.alpha = 0.5;
[self addSubview:self.viewLocation];
self.viewLocation.hidden = YES;
}
备注:
页面上设置按钮,进行两个页面跳转
左边“定位”按钮 每按一次,会出现一个小视图,再按一次就会消失
RootViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
[self.rootV.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
// 1. 给导航栏设置Title(是居中的)
self.title = @"首页";
// 2. 给导航栏左右两边添加button
// UIBarButtonItem控制导航栏上面的一些小控件
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"跳转" style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)];
self.navigationItem.rightBarButtonItem = rightButton;
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"定位" style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)];
self.navigationItem.leftBarButtonItem = leftButton;
}
#pragma mark-----------添加事件
- (void)buttonAction:(UIButton *)sebder {
SecondViewController *secondVC = [[SecondViewController alloc] init];
// 1. 利用栈实现跳转:
// [self.navigationController pushViewController:secondVC animated:YES];
// 页面跳转的效果:
// 限于使用模态跳转:
secondVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
/*
UIModalTransitionStyleCoverVertical // 从下往上滑入,向下滑出
UIModalTransitionStyleFlipHorizontal, // 页面翻转的效果
UIModalTransitionStyleCrossDissolve, // 没有滑动效果 类似于直接叠加的效果
UIModalTransitionStylePartialCurl // 翻书的效果 卷
*/
// 2. 模态实现跳转
[self presentViewController:secondVC animated:YES completion:nil];
}
SecondViewController.m:
之前的控制器里的loadView方法 都是这种格式:
-(void)loadView {
self.second = [[SecondView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view = self.second;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self.second.buttonBack addTarget:self action:@selector(buttonBackAction) forControlEvents:UIControlEventTouchUpInside];
self.navigationController.navigationBar.hidden = NO;
}
- (void)buttonBackAction {
#pragma mark ================ 返回某个视图控制器的三种方法:=========
#pragma mark *** 1 *** 返回上一层视图控制器
// [self.navigationController popViewControllerAnimated:YES];
#pragma mark *** 2 *** 返回根视图
// [self.navigationController popToRootViewControllerAnimated:YES];
#pragma mark *** 3 *** 返回到指定的视图:(根据navigationController里的视图数组下标,找到某个控制器 因为是个栈)
// 这个控制器是已经存在栈中的,不能重新创建一个控制器
// [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0 ] animated:YES];
// [self.navigationController popToViewController:self.navigationController.viewControllers[0] animated:YES];
#pragma mark *** 4 *** 利用模态返回视图
[self dismissViewControllerAnimated:YES completion:nil];
}
SecondView.m:
只是设置一个返回按钮
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addViews];
}
return self;
}
- (void)addViews {
self.backgroundColor = [UIColor grayColor];
self.buttonBack = [UIButton buttonWithType:UIButtonTypeCustom];
self.buttonBack.frame = CGRectMake(50, 50, 200, 50);
self.buttonBack.backgroundColor = [UIColor greenColor];
[self.buttonBack setTitle:@"BackButton" forState:UIControlStateNormal];
[self addSubview:self.buttonBack];
}
2. 界面传值:
1. 从前往后传值:
三步:
1> 在第二个控制器设置属性,用来接收第一个界面传来的值
2> 在第一个控制器的页面跳转事件中实现:将第一个页面中的值传给第二个控制器的属性(第一步设置的 属性)
3> 在第二个控制器的加载方法中:将接收到的属性赋给第二个界面中的某个控件
2. 从后往前传值:
六步:
1> 在第二个控制器里写一个协议(协议里写方法)
2> 在第二个控制器中设置一个属性delegate,遵守第一步中的协议
3> 导入协议头文件,第一个控制器遵守协议
4>第一个控制器中设置代理(在实现页面跳转的方法中设置)( secondVC.delegate = self)
5> 实现协议中的方法
6> 最最重要的一步:在第二个控制器中进行传值 (即:delegate属性调用协议中的方法,并给值)