【iOS】—— ViewController生命周期

ViewController生命周期

首先来看下ViewController在运行过程中的函数介绍:

  • loadView:加载view。这个方法中,要正式加载View了,控制器 view 是通过懒加载的方式进行加载的,即用到的时候再加载。在 view 加载过程中首先会调用 loadView 方法,在这个方法中主要完成一些关键 view 的初始化工作。
  • viewWillAppear: 视图将要显示
  • viewWillLayoutSubviews:控制器的view将要布局子控件
  • viewDidLayoutSubviews: 控制器的view布局子控件完成
  • viewDidAppear: 视图已经显示
  • viewWillDisappear: 视图将要消失
  • viewDidDisappear: 视图已经消失

过程图可看如下:
请添加图片描述

我们来看一个demo,设计思想为从第一个界面切换到第二个界面,再从第二个界面切换回第一个界面,全程当这些函数运行的时候打印出函数名:

第一个界面:

//
//  FirstViewController.m
//  ViewController生命周期
//
//  Created by 翟旭博 on 2022/9/16.
//

#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()

@end

@implementation FirstViewController

- (void)loadView {
    [super loadView];
    NSLog(@"loadView1");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    NSLog(@"viewDidLoad1");
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.view addSubview:button];
    button.tintColor = [UIColor blackColor];
    button.frame = CGRectMake(100, 100, 100, 100);
    [button addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"go" forState:UIControlStateNormal];
    

}

- (void)press {
    NSLog(@"first back------------------------");
    SecondViewController *vc = [[SecondViewController alloc] init];
    vc.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    NSLog(@"viewWillLayoutSubviews1");
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    NSLog(@"viewDidLayoutSubviews1");
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"viewWillAppear1");
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"viewDidAppear1");
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    NSLog(@"viewWillDisappear1");
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    NSLog(@"viewDidDisappear1");
}

- (void)dealloc {
    NSLog(@"dealloc1");
}

第二个界面:

//
//  SecondViewController.m
//  ViewController生命周期
//
//  Created by 翟旭博 on 2022/9/16.
//

#import "SecondViewController.h"
#import "FirstViewController.h"
@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)loadView {
    [super loadView];
    NSLog(@"loadView2");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    NSLog(@"viewDidLoad2");
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.view addSubview:button];
    button.tintColor = [UIColor blackColor];
    button.frame = CGRectMake(100, 100, 100, 100);
    [button addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"back" forState:UIControlStateNormal];
    

}

- (void)press {
    NSLog(@"second back------------------------");
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    NSLog(@"viewWillLayoutSubviews2");
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    NSLog(@"viewDidLayoutSubviews2");
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"viewWillAppear2");
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"viewDidAppear2");
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    NSLog(@"viewWillDisappear2");
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    NSLog(@"viewDidDisappear2");
}

- (void)dealloc {
    NSLog(@"dealloc2");
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

当app启动进入第一个界面:

请添加图片描述
此时输出结果:
请添加图片描述

app进入第二个界面:

请添加图片描述
此时输出结果:
请添加图片描述

app返回第一个界面:
请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值