The IOS SDK provides a lot of native viewcontroller, which greatly improves our development efficiency, then we will talk about its life cycle according to the Viewcontroller we used in development.
(a) According to the structure and usage, all viewcontroller of iOS can be divided into two categories:
1, mainly for the display of content viewcontroller, such as Uitableviewcontroller,uiviewcontroller, this viewcontroller mainly for the user to show the content of the application, and provide user interaction.
2, for the control and display of other Viewcontroller Viewcontroller, such as Uinavigationcontroller,uitabbarcontroller, These viewcontroller are generally viewcontroller containers or controllers; they all have a property viewcontrollers (where: ①uinavigationcontroller represents a stack-like structure, Push a viewcontroller or pop once viewcontroller, so generally after a viewcontroller will generally rely on the previous Viewcontroller ; ② while Uitabbarcontroller represents an array structure, each viewcontroller is tied to each other).
(ii) Controller and view life cycle:
The view referred to here refers to the controller's view, which acts as an attribute of the controler, whose life cycle is within the controller's life cycle, meaning that your controller cannot be released before the view is released.
Figure--viewcontroller Life cycle (picture for reference network)
When you alloc and init a viewcontroller, the Viewcontroller should not have created a view. Viewcontroller view is created using the Lazyinit method, that is, the view property that you call Getter:[self view]. The getter will first determine if the view is created, and if not, it will call Loadview to create the view. Loadview will continue to call Viewdidloadwhen it finishes. One difference between Loadview and viewdidload is that there is no view when calling Loadview, and the view is called when the viewdidload is created ;
When the view is added before the other view, the viewwillappear is called, and then the viewdidappear is called;
When a view is moved out of another view, Viewwilldisappear is called, and then Viewdiddisappear is called;
When view is not in use and is disappeared and is subject to a memory warning, Viewcontroller will release the view and point it to nil.
(c) There are so many functions in the Viewcontroller life cycle that it is better to write the code in that function:
1.do not appear in Init to create the View Code , in Init should only the relevant data initialization, and these data are more critical data; Do not drop self.view in init, otherwise it will cause Viewcontroller to create the view. (because view is lazyinit);
2,Loadview only initialize view , generally used to create a more critical view such as Tableviewcontroller Tabview, Uinavigationcontroller Navgationbar, do not drop the getter of the view (before the Super Loadview), it is best not to initialize some non-critical view. If you are creating a Viewcontroller from the nib file, you must first call the Super Loadview method, but it is not recommended to overload this method;
3,Viewdidload This time the view has already ; it is best to create some additional view and controls, one thing to note is that Viewdidload will be called multiple times (Viewcontroller may load view multiple times);
4,viewwillappear This is generally before the view is added to the Superview; call before switching animation, here can do some pre-display processing, such as keyboard pop-up, some special process animation;
5,Viewdidappear is generally used for display , after switching the animation, if there is a need for the operation, you can add the relevant code here;
6,Viewdidunload This time the view of Viewcontroller is nil ; Since this generally occurs in memory warnings, you should release the view that is not displayed here. For example, if you add a label to the Viewcontroller view, and the label is a Viewcontroller attribute, then you have to set this property to nil so as not to consume unnecessary memory. This label is re-created when Viewdidload.
(The above is the Viewcontroller life cycle of the relevant introduction and understanding, but also hope that we complement each other to improve together)
The life cycle of Ios-viewcontroller