[iOS]-ViewController生命周期

我们使用视图的显示顺序以及打印的情况来表示函数调用顺序,即View Controller的生命周期

函数介绍:

▪viewDidLoad:在视图加载后被调用,如果是在代码中创建的视图加载器,他将会在loadView方法后被调用,如果是从nib视图页面输出,他将会在视图设置好后被调用。只会被调用一次,之后进入这个ViewController不调用次函数。
▪viewLoad:每次访问controller的view(如self.view)且view为nil,loadView方法就会被调用。用于创建Controller的View
▪viewWillAppear:视图将要显示
▪viewWillLayouSubviews:控制器的view将要布局子控件(在这个方法里,部署需要改变重新刷新view的代码,功能类似view的layoutSubViews这个方法,需要注意的是,这个方法里一般都是需要重置的view的frame,宽度和高度的获取,因此view的frame一般都写在这个方法里)
▪viewDidLayoutSubviews:控制器的view布局子控件完成
▪viewDidAppear:视图已经显示
▪viewWillDisappear:视图将要消失
▪viewDidDisappear:视图已经消失,在Controller被切换时调用,第二个视图出现后第一个视图消失(但是非全屏显示的模态视图作为新视图显示的时候原视图并不会消失)

代码测试:

我们通过在一个视图上添加多个UIView和一个用于跳转视图界面的Button来进行测试

▪ViewController的viewDidLoad中代码如下:

	//我们在viewDidLoad中添加一个Button
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [testButton setTitle:@"点击跳转" forState:UIControlStateNormal];
    [testButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    testButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2, [UIScreen mainScreen].bounds.size.height / 4, [UIScreen mainScreen].bounds.size.width / 5, 50);
    
    [testButton addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:testButton];

接下来是一些测试视图周期的函数:
▪viewWillAppear(我们将其设置为灰色):

- (void) viewWillAppear:(BOOL)animated {
    _grayView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height / 3, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 1.5)];
    [self.view addSubview:_grayView];
    _grayView.backgroundColor = [UIColor grayColor];
    NSLog(@"grayViewWillAppear");
}

▪viewDidAppear(我们将其设置为蓝色):

- (void) viewDidAppear:(BOOL)animated {
    _blueView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height / 3, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 1.5)];
    [self.view addSubview:_blueView];
    _blueView.backgroundColor = [UIColor blueColor];
    NSLog(@"blueViewDidAppear");   
}

▪viewWillDisappear(我们将其设置为红色):

- (void) viewWillDisappear:(BOOL)animated {
    _redView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height / 3, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 1.5)];
    _redView.backgroundColor = [UIColor redColor];
    NSLog(@"redViewWillDisappear");
}

▪viewWillLayoutSubviews(我们将其设置为黑色):

- (void) viewWillLayoutSubviews {
    _blackView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height / 3, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 1.5)];
    [self.view addSubview:_blackView];
    _blackView.backgroundColor = [UIColor blackColor];
    NSLog(@"blackViewWillLayoutSubviews");
}

▪viewDidLayoutSubviews(我们将其设置为黄色):

- (void) viewDidLayoutSubviews {
    _yellowView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height / 3, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height / 1.5)];
    [self.view addSubview:_yellowView];
    _yellowView.backgroundColor = [UIColor yellowColor];
    NSLog(@"yellowViewDidLayoutSubviews");
}

▪按钮事件函数:

- (void) pressButton {
    NSLog(@"-------------pressButton-------------");
    SecondViewController *second = [[SecondViewController alloc] init];
    second.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:second animated:NO completion:nil];
}

代码刚开始执行时的结果(未跳转界面):
请添加图片描述
层级图如下:请添加图片描述
跳转到第二个界面之后
我们在第二个界面中也同样设置一个按钮用于返回原视图
另外设置四个函数,函数中输出相应的话来显示视图2和视图1切换是的函数执行情况:
▪SecondViewController的viewDidLoad中的代码如下:
(设置一个返回按钮)

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    [testButton setTitle:@"点击返回" forState:UIControlStateNormal];
    
    [testButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    testButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2, [UIScreen mainScreen].bounds.size.height / 2, [UIScreen mainScreen].bounds.size.width / 5, 50);
    
    [testButton addTarget:self action:@selector(pressButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:testButton];
}

▪四个函数如下:

- (void) viewDidDisappear:(BOOL)animated {
    NSLog(@"viewSecondDidDisappear");
}

- (void) viewWillDisappear:(BOOL)animated {
    NSLog(@"viewSecondWillDisappear");
}

- (void) viewDidAppear:(BOOL)animated {
    NSLog(@"viewSecondDidAppear");
}

- (void) viewWillAppear:(BOOL)animated {
    NSLog(@"viewSecondWillAppear");
}

▪按钮点击事件函数:

- (void) pressButton {
    NSLog(@"---------------pressBack---------------");
    [self dismissViewControllerAnimated:NO completion:nil];
}

点击后的运行结果:
请添加图片描述
层级图如下:
请添加图片描述
说明在视图2加载并完全显示出来以后,视图1才完全消失

点击返回按钮后的运行结果:
请添加图片描述
层级图如下:
请添加图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值