【iOS】ViewController的生命周期

发布于:2025-09-15 ⋅ 阅读:(23) ⋅ 点赞:(0)

概念

在iOS中一般有两种ViewController,一类是用于显示内容的,比如UIViewController,UITableVIewController或者自定义的VC等,另一类是容器型的控制器,如UINavigationController(内部维护一个栈,前面博客中有所讲解,用push,pop进行控制),UITabBarController(内部维护一个数组)等

内容型 VC = 页面本身(展示内容和逻辑)

容器型 VC = 用来管理多个内容 VC 的关系(栈 or 数组)

生命周期

请添加图片描述

函数

  1. init:初始化程序
  2. loadView:在UIViewController对象的view被访问且为空时调用
  3. viewDidLoad:视图加载完成后调用,进行如控件的初始化之类的操作,在这个控制器销毁之前只会调用一次该方法
  4. viewWillAppear:视图即将展现时调用
  5. viewWillLayoutSubviews:即将开始子视图位置布局
  6. viewDidLayoutSubviews:用于通知视图的位置布局已经完成
  7. viewDidAppear:视图已经出现时调用
  8. viewWillDisappear:视图即将消失
  9. viewDidDisappear:视图已经消失
  10. dealloc:视图被销毁

周期函数的调用顺序

我们具体调用控制器推出另一个控制器时的具体方法有两种,一个是push方法,另一个是present方法,他们的具体调用顺序调用我们在下面演示一下

push/pop

#import "ViewControllerA.h"
#import "ViewControllerB.h"
@interface ViewControllerA ()
@property (nonatomic, strong) UIButton *pushButton;
@end

@implementation ViewControllerA

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor];
    self.title = @"ViewController A";
    
    NSLog(@"A - %s", __func__);
    

    _pushButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_pushButton setTitle:@"Push to B" forState:UIControlStateNormal];
    _pushButton.frame = CGRectMake(self.view.frame.size.width / 2 - 100, 200, 200, 50);
    _pushButton.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_pushButton];
    [_pushButton addTarget:self action:@selector(pushViewControllerB) forControlEvents:UIControlEventTouchUpInside];
}

- (void)pushViewControllerB {
    ViewControllerB *b = [[ViewControllerB alloc] init];
    NSLog(@"-------- A push B --------");
    [self.navigationController pushViewController:b animated:YES];
}
- (void)loadView {
    [super loadView];
    NSLog(@"A - %s", __func__);
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"A - %s", __func__);
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    NSLog(@"A - %s", __func__);
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    NSLog(@"A - %s", __func__);
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"A - %s", __func__);
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    NSLog(@"A - %s", __func__);
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    NSLog(@"A - %s", __func__);
}
#import "ViewControllerB.h"

@interface ViewControllerB ()
@property (nonatomic, strong) UIButton *popButton;
@end

@implementation ViewControllerB

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    self.title = @"ViewController B";
    
    NSLog(@"B - %s", __func__);
    
    // Pop 按钮
    _popButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_popButton setTitle:@"Pop to A" forState:UIControlStateNormal];
    _popButton.frame = CGRectMake(self.view.frame.size.width / 2 - 100, 200, 200, 50);
    _popButton.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_popButton];
    [_popButton addTarget:self action:@selector(popToViewControllerA) forControlEvents:UIControlEventTouchUpInside];
}

- (void)popToViewControllerA {
    NSLog(@"-------- B pop A --------");
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)loadView {
    [super loadView];
    NSLog(@"B - %s", __func__);
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"B - %s", __func__);
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    NSLog(@"B - %s", __func__);
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    NSLog(@"B - %s", __func__);
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"B - %s", __func__);
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    NSLog(@"B - %s", __func__);
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    NSLog(@"B - %s", __func__);
}

运行结果

请添加图片描述

输出

请添加图片描述

请添加图片描述

present/dismiss

#import "ViewControllerA.h"
#import "ViewControllerB.h"

@interface ViewControllerA ()
@property (nonatomic, strong) UIButton *presentButton;
@end

@implementation ViewControllerA

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor];
    self.title = @"ViewController A";
    
    NSLog(@"A - %s", __func__);
    _presentButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_presentButton setTitle:@"Present B" forState:UIControlStateNormal];
    _presentButton.frame = CGRectMake(self.view.frame.size.width / 2 - 100, 200, 200, 50);
    _presentButton.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_presentButton];
    [_presentButton addTarget:self action:@selector(presentViewControllerB) forControlEvents:UIControlEventTouchUpInside];
}

- (void)presentViewControllerB {
    ViewControllerB *b = [[ViewControllerB alloc] init];
    NSLog(@"-------- A present B --------");
    b.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:b animated:YES completion:nil];
}
- (void)loadView {
    [super loadView];
    NSLog(@"A - %s", __func__);
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"A - %s", __func__);
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    NSLog(@"A - %s", __func__);
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    NSLog(@"A - %s", __func__);
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"A - %s", __func__);
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    NSLog(@"A - %s", __func__);
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    NSLog(@"A - %s", __func__);
}


/*
#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
#import "ViewControllerB.h"

@interface ViewControllerB ()
@property (nonatomic, strong) UIButton *dismissButton;
@end

@implementation ViewControllerB

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    self.title = @"ViewController B";
    
    NSLog(@"B - %s", __func__);
    
    _dismissButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_dismissButton setTitle:@"Dismiss to A" forState:UIControlStateNormal];
    _dismissButton.frame = CGRectMake(self.view.frame.size.width / 2 - 100, 200, 200, 50);
    _dismissButton.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_dismissButton];
    [_dismissButton addTarget:self action:@selector(dismissToViewControllerA) forControlEvents:UIControlEventTouchUpInside];
}

- (void)dismissToViewControllerA {
    NSLog(@"-------- B dismiss A --------");
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)loadView {
    [super loadView];
    NSLog(@"B - %s", __func__);
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"B - %s", __func__);
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    NSLog(@"B - %s", __func__);
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    NSLog(@"B - %s", __func__);
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"B - %s", __func__);
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    NSLog(@"B - %s", __func__);
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    NSLog(@"B - %s", __func__);
}


/*
#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

运行结果

请添加图片描述

输出

请添加图片描述
请添加图片描述