• 【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");
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    第二个界面:

    //
    //  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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85

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

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

    app进入第二个界面:

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

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

  • 相关阅读:
    leetcode(力扣) 454. 四数相加 II (优化暴力算法)
    小红书商品详情API接口(商品详情页面数据接口)
    spring容器ioc和di
    MySQL主/从-主/主集群安装部署
    大厂程序员爆料,现在紧缺这号人,简历准备好了么?
    非人脸场景AE模块调试方法及其合理性分析
    promise详细的适用
    uni微信小程序隐私协议
    11.22IG客户情绪报告: 黄金、原油、澳元、日元、欧元、英镑
    【Python函数式编程】——高阶函数(Higher-order function)
  • 原文地址:https://blog.csdn.net/m0_62386635/article/details/126887894