iOS - UIViewController生命周期(storyboard/Xib/纯代码)(下)

简介: iOS - UIViewController生命周期(storyboard/Xib/纯代码)
CodeInitViewController.m

//
//  CodeInitViewController.m
//  ControllerLifeCycle
//
//  Created by Ternence on 2021/4/29.
//
#import "CodeInitViewController.h"
#import "XibInitViewController.h"
@interface CodeInitViewController ()
@end
@implementation CodeInitViewController
+ (void)load {
    [super load];
    NSLog(@"CodeVC------%s", __func__);
}
+ (void)initialize {
    [super initialize];
    NSLog(@"CodeVC------%s", __func__);
}
+ (instancetype)alloc {
    NSLog(@"CodeVC------%s", __func__);
    return  [super alloc];
}
- (nullable instancetype)initWithCoder:(NSCoder *)coder {
    NSLog(@"CodeVC------%s", __func__);
    return [super initWithCoder:coder];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    NSLog(@"CodeVC------%s", __func__);
    return [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
}
- (instancetype)init {
    NSLog(@"CodeVC------%s", __func__);
    return [super init];
}
- (void)loadView {
    [super loadView];
    NSLog(@"CodeVC------%s", __func__);
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"CodeVC------%s", __func__);
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"CodeVC------%s", __func__);
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    NSLog(@"CodeVC------%s", __func__);
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    NSLog(@"CodeVC------%s", __func__);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"CodeVC------%s", __func__);
    self.view.backgroundColor = [UIColor redColor];
}
- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    NSLog(@"CodeVC------%s", __func__);
}
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    NSLog(@"CodeVC------%s", __func__);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    NSLog(@"CodeVC------%s", __func__);
}
- (void)dealloc {
    NSLog(@"CodeVC------%s", __func__);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    XibInitViewController *vc = [[ XibInitViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
@end


二. 方法说明


load   程序启动后,在main函数调用之前,系统会加载所有load方法,提前进行一些资源包配置或hook
initialize  当前类或其子类未被初始化过时会首次调用,若以后当前类或其子类再次初始化不会再调用,一般提前为初始化做一些工作
alloc  系统为当前类分配内存时调用,在C语言中就是malloc这一步
initWithCoder 通过storyboard方式实例化的vc,需要经过反序列化,这个方法会被调用,其他不会调用
initWithNibName:bundle:   通过Xib或init方法实例化的vc,这个方法都会被调用,其实init方法最终都会走该方法
init  通过纯代码实例化的vc会调用,最终会走initWithNibName:bundle
loadView  实例化vc后,可以加载一些系统常规view
viewDidLoad  一般加载自定义view或初始化熟悉,视图加载完毕后会调用
viewWillDisappear:  视图即将出现调用
viewDidAppear: 视图已经出现会调用
viewWillDisappear:  视图即将消失调用
viewDidDisappear:  视图已经消失调用
viewWillLayoutSubviews  视图加载完毕后将要布局
viewDidLayoutSubviews  视图加载完毕后布局也完成了
didReceiveMemoryWarning  加载视图时,内存消耗太大,出现内存警告会调用  
dealloc  实例化被销毁,运行内存的回收会调用


三. Debug log


  1. 程序启动,加载storyboard实例化BoardInitViewController

2021-04-29 18:33:07.317496+0800 ControllerLifeCycle[6270:5044336] XibVC------+[XibInitViewController load]
2021-04-29 18:33:07.319472+0800 ControllerLifeCycle[6270:5044336] boardVC------+[BoardInitViewController load]
2021-04-29 18:33:07.319605+0800 ControllerLifeCycle[6270:5044336] CodeVC------+[CodeInitViewController load]
2021-04-29 18:33:07.369674+0800 ControllerLifeCycle[6270:5044336] boardVC------+[BoardInitViewController initialize]
2021-04-29 18:33:07.369789+0800 ControllerLifeCycle[6270:5044336] boardVC------+[BoardInitViewController alloc]
2021-04-29 18:33:07.369918+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController initWithCoder:]
2021-04-29 18:33:07.410110+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController loadView]
2021-04-29 18:33:07.410246+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController viewDidLoad]
2021-04-29 18:33:07.410867+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController viewWillAppear:]
2021-04-29 18:33:07.412541+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController viewWillLayoutSubviews]
2021-04-29 18:33:07.412663+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController viewDidLayoutSubviews]
2021-04-29 18:33:07.418158+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController viewDidAppear:]


  1. BoardInitViewController中push到纯代码实例化的CodeInitViewController

2021-04-29 18:34:45.194133+0800 ControllerLifeCycle[6270:5044336] CodeVC------+[CodeInitViewController initialize]
2021-04-29 18:34:45.194269+0800 ControllerLifeCycle[6270:5044336] CodeVC------+[CodeInitViewController alloc]
2021-04-29 18:34:45.194399+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController init]
2021-04-29 18:34:45.194518+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController initWithNibName:bundle:]
2021-04-29 18:34:45.200030+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController loadView]
2021-04-29 18:34:45.200188+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewDidLoad]
2021-04-29 18:34:45.200374+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController viewWillDisappear:]
2021-04-29 18:34:45.200515+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewWillAppear:]
2021-04-29 18:34:45.220143+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewWillLayoutSubviews]
2021-04-29 18:34:45.220331+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewDidLayoutSubviews]
2021-04-29 18:34:45.744908+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController viewDidDisappear:]
2021-04-29 18:34:45.745100+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewDidAppear:]


  1. CodeInitViewController中push到Xib实例化的XibInitViewController

2021-04-29 18:35:29.448921+0800 ControllerLifeCycle[6270:5044336] XibVC------+[XibInitViewController initialize]
2021-04-29 18:35:29.449069+0800 ControllerLifeCycle[6270:5044336] XibVC------+[XibInitViewController alloc]
2021-04-29 18:35:29.449190+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController init]
2021-04-29 18:35:29.449288+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController initWithNibName:bundle:]
2021-04-29 18:35:29.450580+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController loadView]
2021-04-29 18:35:29.450757+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController viewDidLoad]
2021-04-29 18:35:29.450920+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewWillDisappear:]
2021-04-29 18:35:29.451058+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController viewWillAppear:]
2021-04-29 18:35:29.460843+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController viewWillLayoutSubviews]
2021-04-29 18:35:29.460981+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController viewDidLayoutSubviews]
2021-04-29 18:35:29.963047+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewDidDisappear:]
2021-04-29 18:35:29.963345+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController viewDidAppear:]


  1. XibInitViewControllerPop到CodeInitViewController

2021-04-29 18:36:28.460722+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController viewWillDisappear:]
2021-04-29 18:36:28.460881+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewWillAppear:]
2021-04-29 18:36:28.971655+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController viewDidDisappear:]
2021-04-29 18:36:28.971818+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewDidAppear:]
2021-04-29 18:36:28.972008+0800 ControllerLifeCycle[6270:5044336] XibVC-------[XibInitViewController dealloc]


  1. CodeInitViewControllerPop到BoardInitViewController

2021-04-29 18:37:09.095856+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewWillDisappear:]
2021-04-29 18:37:09.095988+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController viewWillAppear:]
2021-04-29 18:37:09.599896+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController viewDidDisappear:]
2021-04-29 18:37:09.600184+0800 ControllerLifeCycle[6270:5044336] boardVC-------[BoardInitViewController viewDidAppear:]
2021-04-29 18:37:09.600453+0800 ControllerLifeCycle[6270:5044336] CodeVC-------[CodeInitViewController dealloc]


四. 总结


  1. main函数加载前,先加载所有controller的+load方法,其中顺序是:Xib-> Storyboard->Code


  1. 控制器初始化流程是:初始化A->加载视图A->将要显示A->将要布局A->布局A完成->已经显示A


  1. 控制器A Push B生命流程:初始化B->加载B->A将要消失->B将要显示->B将要布局->B已经布局->A已经消失->B已经显示


  1. 只有通过storyboard创建的vc才会调用initWithCoder,不会调用initWithNibName:bundle:


  1. 调用了init方法的vc(xib或纯代码),最终会调用initWithNibName:bundle:


相关文章
|
7月前
|
iOS开发
iOS开发解释 App 生命周期,包括各个阶段的调用顺序。
iOS开发解释 App 生命周期,包括各个阶段的调用顺序。
85 1
|
程序员 iOS开发
iOS App的生命周期
iOS App的生命周期
341 0
|
Swift iOS开发
IOS使用Swift加载Xib文件
IOS使用Swift加载Xib文件
327 0
|
iOS开发
IOS使用纯代码布局替换掉默认的storyboard
IOS使用纯代码布局替换掉默认的storyboard
109 0
|
存储 自然语言处理 API
iOS 多语言快捷设置Xib设置
iOS 多语言快捷设置Xib设置
iOS 多语言快捷设置Xib设置
|
iOS开发
iOS小技能:自动布局实现兄弟控件N等分且宽高比例是1:N(xib 上实现)
本文为 iOS视图约束专题的第三篇:xib上使用自动布局教程
183 0
|
Go iOS开发
iOS使用xib自定义uiview
iOS使用xib自定义uiview
372 0
iOS使用xib自定义uiview
|
数据可视化 程序员 iOS开发
iOS开发:用XIB拖控件关联时报错:“Could not insert new outlet connection…”解决方法
在iOS开发过程中,尤其是iOS开发初期,会遇到各种各样的错误,有些错误是开发者的不熟悉或者疏忽大意造成的,还有些是无厘头的错误,可以通过重启Xcode或者重启电脑就可解决。
281 0
iOS开发:用XIB拖控件关联时报错:“Could not insert new outlet connection…”解决方法
IOS_ViewController生命周期内各个函数的调用次序
IOS_ViewController生命周期内各个函数的调用次序
55 0
|
iOS开发
iOS生命周期篇
iOS生命周期篇
239 0