preface
UIViewController plays a very important role in iOS development. It serves as a bridge between view and data model, and systematically presents data on the view through the management of UIViewController. As one of the most basic classes in UIKit, you can’t do complex projects without UIViewController as the base class. So it’s important to understand the entire lifecycle of UIViewController.
The life cycle of UIViewController
Let’s first look at the following functions related to the lifecycle of UIViewController:
+ (void)initialize {
NSLog(@"======== class initialization method: initialize =======\n");
}
- (instancetype)init {
self = [super init];
NSLog(@"======== instance initialization method: init =======\n");
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
NSLog(@"= = = = = = = = initialization from the archive: initWithCoder: (aDecoder NSCoder *) = = = = = = = \ n");
return self;
}
- (void)loadView {
[super loadView];
NSLog(@"======== 加载视图: loadView =======\n");
}
#pragma mark- life cycle
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"======== to load view: viewDidLoad =======\n");
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
NSLog(@"= = = = = = = = to layout child views: viewWillLayoutSubviews = = = = = = = \ n");
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
NSLog(@"= = = = = = = = have child view layout: viewDidLayoutSubviews = = = = = = = \ n");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
NSLog(@"= = = = = = = = receive memory warning: didReceiveMemoryWarning = = = = = = = \ n");
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"= = = = = = = = view will be: viewWillAppear: (BOOL) animated = = = = = = = \ n");
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"= = = = = = = = view has appeared: viewDidAppear: (BOOL) animated = = = = = = = \ n");
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSLog(@"= = = = = = = = view is going to disappear: viewWillDisappear: (BOOL) animated = = = = = = = \ n");
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
NSLog(@"= = = = = = = = view has disappeared: viewDidDisappear: (BOOL) animated = = = = = = = \ n");
}
- (void)dealloc {
NSLog(@"======== Release: dealloc =======\n");
}
Copy the code
It’s complicated to see so many functions, but when you look at them from the top down, you can see that the relationship is very clear. Let’s run to see the order of calls between them:
[77845:3751647] ======== ViewController1 Initialize ======= 2019-05-21 09:29:41.533321+0800 ThinTableVIew1[77845:3751647] ======== ViewController1 ======= 2019-05-21 09:29:41.539746+0800 ThinTableVIew1[77845:3751647] ======== ViewController1 LoadView ======= 2019-05-21 09:29:41.539975+0800 ThinTableVIew1[77845:3751647] ======== ViewController1 ViewDidLoad ======= 2019-05-21 09:29:41.540280+0800 ThinTableVIew1[77845:3751647] ======== ViewController1 ViewWillAppear :(BOOL) ======= 2019-05-21 09:29:41.581539+0800 ThinTableVIew1[77845:3751647] ======== ViewController1 will lay out the subviews: ViewWillLayoutSubviews ======= 2019-05-21 09:29:41.581755+0800 ThinTableVIew1[77845:3751647] ======== ViewController1 Already laid out subviews: ViewDidLayoutSubviews ======= 2019-05-21 09:29:42.086186+0800 ThinTableVIew1[77845:3751647] ======== ViewController1 The view already appears: ViewDidAppear :(BOOL)animated ======= 2019-05-21 09:30:11.567953+0800 ThinTableVIew1[77845:3751647] ======== ViewController1 is about to disappear: ViewWillDisappear :(BOOL)animated ======= 2019-05-21 09:30:11.568210+0800 ThinTableVIew1[77845:3751647] ======== The ViewController view will appear: ViewWillAppear :(BOOL) ======= 2019-05-21 09:30:12.074866+0800 ThinTableVIew1[77845:3751647] ======== The ViewController1 view has disappeared: ViewDidDisappear :(BOOL)animated ======= 2019-05-21 09:30:12.075103+0800 ThinTableVIew1[77845:3751647] ======== The ViewController view already appears: ViewDidAppear :(BOOL)animated ======= 2019-05-21 09:30:12.075378+0800 ThinTableVIew1[77845:3751647] ======== ViewController1 release: dealloc =======Copy the code
ViewController is the upper view and ViewController1 is the secondary view of push. We can see the entire process of ViewController1 from initialization to release.
+ (void)initialize
The: function is not called every time the object is created. It is called only when the object is initialized for the first time and not when the object is created againinitialize
Methods.init
Methods andinitCoder
Methods are similar, and the context in which knowledge is invoked is different. If initialized with code, is calledinit
Methods from niB files or archives (xib
,storyboard
) will be calledinitCoder
.initCoder
isNSCoding
Methods in the protocol,NSCoding
It’s a protocol for encoding, decoding, archiving.loadView
: Indicates the start of loadingview
The start method of theViewController
Is called only once in the life cycle of.viewDidLoad
The: is our most common method, where we initialize class member objects and variables. This method is also called only once on the layout after the class is created, whether the view is displayed or disappeared.viewWillAppear:(BOOL)animated
The: method is called when the view is about to be displayed.viewWillLayoutSubviews
The: method is called when the subview is about to be laid out.viewDidLayoutSubviews
The: method is called after the subview layout is complete.viewDidAppear:(BOOL)animated
: the method is that the view already appears.viewWillDisappear:(BOOL)animated
The method is that the view is about to disappear.viewDidDisappear:(BOOL)animated
: The view has disappeared.dealloc
:ViewController
Called when released.
conclusion
From the print above we can see the entire life cycle of the ViewController, from initialization to release. So when you exit the ViewController you can see that ViewController is going to call viewWillDisappear:(BOOL)animated and then viewWillAppear:(BOOL)anim from the upper view The ated method will then call the viewDidDisappear (BOOL)animated method of the current view.