相关函数
init
: 初始化ViewController。viewDidLoad
:在我之前学习中最多使用的方法,在创建类后无论视图展示还是消失,该方法只调用一次在布局中,类成员对象和变量的初始化都会放在这个方法中。viewWilAppear
:如同英语含义一样,在视图即将展示的时候调用该方法。viewDidAppear
:在视图已经展示的时候调用该方法。viewWillLayoutSubviews
,viewDidLayoutSubviews
:这两个方法用于处理视图布局的变化,一般来说在这里处理视图布局的相关操作,will在重新布局前被调用,did在重新布局后被调用。viewWillDisappear
:在视图即将消失前被调用。viewDidDisappear
:在视图消失后被调用。traitCollectionDidChange
:当视图控制器的特征集合发生变化的时候(例如横竖屏切换或者分屏时使用),这里笔者并没有使用过,后期对其了解透彻后再进行补充。didReceiveMemoryWarning
:当系统可用内存不足时会调用这个函数,可以在这个函数中释放一些不必要的对象和可重建的资源,但是如果在这个方法中不写代码,系统不会自动释放内存,只会释放一些系统级别的内存资源(不甚理解)。
执行顺序
下面我通过两个视图控制器跳转来展示执行的顺序。
VCViewController:
#import "ViewController.h"
#import "VCTwo.h"
@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor redColor];UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 200, 250, 100)];[btn setTitle:@"跳转下一页" forState:UIControlStateNormal];[btn addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview: btn];NSLog(@"viewDidLoad");
}-(void) press
{NSLog(@"A->B");VCTwo* vc2 = [[VCTwo alloc] init];vc2.modalPresentationStyle = UIModalPresentationFullScreen;[self presentViewController:vc2 animated:YES completion:nil];}- (void)viewWillAppear:(BOOL)animated
{NSLog(@"viewWillAppear:A");
}- (void)viewDidAppear:(BOOL)animated
{NSLog(@"viewDidAppear:A");
}- (void)viewWillLayoutSubviews
{NSLog(@"viewWillLaoutSubviews");
}- (void)viewDidLayoutSubviews
{NSLog(@"viewDidLayoutSubviews");
}- (void)viewWillDisappear:(BOOL)animated
{NSLog(@"viewWillDisappear:A");
}- (void)viewDidDisappear:(BOOL)animated
{NSLog(@"viewDidDisappear:A");
}@end
VCTwo:
#import "VCTwo.h"@interface VCTwo ()@end@implementation VCTwo- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor greenColor];UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 200, 250, 100)];[btn setTitle:@"跳转上一页" forState:UIControlStateNormal];[btn addTarget:self action:@selector(press) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview: btn];NSLog(@"viewDidLoad");
}-(void) press
{NSLog(@"B->A");[self dismissViewControllerAnimated:YES completion:nil];
}- (void)viewWillAppear:(BOOL)animated
{NSLog(@"viewWillAppear:B");
}- (void)viewDidAppear:(BOOL)animated
{NSLog(@"viewDidAppear:B");
}- (void)viewWillLayoutSubviews
{NSLog(@"viewWillLaoutSubviews");
}- (void)viewDidLayoutSubviews
{NSLog(@"viewDidLayoutSubviews");
}- (void)viewWillDisappear:(BOOL)animated
{NSLog(@"viewWillDisappear:B");
}- (void)viewDidDisappear:(BOOL)animated
{NSLog(@"viewDidDisappear:B");
}@end
打印结果:
viewDidLoad
viewWillAppear:A
viewWillLaoutSubviews
viewDidLayoutSubviews
viewDidAppear:A
A->B
viewDidLoad
viewWillDisappear:A
viewWillAppear:B
viewWillLaoutSubviews
viewDidLayoutSubviews
viewDidAppear:B
viewDidDisappear:A
B->A
viewWillDisappear:B
viewWillAppear:A
viewDidAppear:A
viewDidDisappear:B
由打印的结果可以看出这些方法执行的顺序:
viewDidLoad
->viewWillAppear
->viewDidAppear
->viewWillLaoutSubviews
->viewDidLaoutSubviews
->viewWillDisappear
->viewDidDisappear
在最后消失的时候,先准备加载新的视图,旧的视图才开始准备消失,在新的视图出现后,旧视图才消失。
GIF动图:
loadView
该方法在控制器的view为空的时候被调用,loadView是使用代码生成视图的时候,当视图第一次载入的时候调用的方法,用于使用代码来实现控件。当我们找不到view时,系统就会一直调用loadView方法来寻找view,就会陷入一个循环。
参考文档:ios开发中ViewDidLoad和loadView的区别