Original link: http://www.jianshu.com/p/85c98a9e93eb Lau (Jane book author)
First, the UIView life cycle of the approximate process:
Loadview load view/nib to memory-->viewdidload functions to further initialize these view--> out of memory, call viewdidunload function to release views--> when you need to use view and go back to the first step
Loadview:
Never call this function actively.
Viewcontroller calls this function when the view is requested and the current view value is nil. If you create a view manually, you should reload the function, and remember not to invoke [super Loadview] when overloaded.
Viewdidload:
This function is primarily to allow you to further initialize your views, usually responsible for the data initialization of the view and its sub-view after it is loaded into memory, that is, the initialization of the data part
Viewdidunload: In the absence of program memory, this function is called by the controller to release temporarily useless view and view related objects. Since the controller usually holds a reference to the view and the associated object, you must use this function to discard ownership of these objects for memory recycling, but do not release data that is difficult to reconstruct
Viewwillappear: Called when the view is about to be visible, no action is taken by default.
Viewdidappear: Called when the view has completely transitioned to the screen
Viewwilldisappear: The view is dismissed when called, overwritten or otherwise hidden, no action is taken by default
Viewdiddisappear: The view is dismissed after it is called, overwritten, or otherwise hidden. No action is taken by default
Didreceivememorywarning: The system calls this method when the program memory alarm
Ii. life cycle of controller and view
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 Viewdidload when it finishes. One difference between Loadview and viewdidload is that there is no view at Loadview. And the viewdidload when the view and created well.
When view is added before other view, Viewwillappear is called, and then 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.
Third, the code organization (how to design good Viewcontroller)
There are so many functions in the Viewcontroller life cycle, an important question is what code should be written somewhere.
1. Do not show code to create view in init. Good design, 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 swap out the view getter (before you drop 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 Super's Loadview method, but it is not recommended to overload this method.
3, Viewdidload This time view has been, the most suitable to create some additional view and control.
4. Viewwillappear this is typically called before the view is added to the Superview before switching animations. Here you can do some pre-display processing. For example, keyboard popup, some special process animation (such as the status bar and Navigationbar color).
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 Viewcontroller view 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.
7. Next look at how the view in Viewcontroller is unloaded:
When the system issues a memory warning, the Didreceivememoerywarning method is called, and if there is currently a view that can be released, the system calls the Viewwillunload method to release the view, and then calls the Viewdidunload method when it is finished. The view is uninstalled. The variable that originally points to view is set to nil, and the action is to call Self.mybutton = Nil in the Viewdidunload method;
Summary: Loadview and viewdidload The difference is that the loadview when the view has not been generated, viewdidload, the view has been generated, Loadview will only be called once, Viewdidload may be called multiple times (view may be loaded multiple times), and Viewwillappear will be called before the view is added to another view, and then Viewdidappear will be called. When view is removed from another view, Viewwilldisappear is called and Viewdiddisappear is called after removal. When view is no longer in use, the Viewcontroller will release the view and point it to nil when the memory warning is received.
The process of implementing each method in the Viewcontroller life cycle is as follows:init->loadview->viewdidload->viewwillapper->viewdidapper-> Viewwilldisapper->viewdiddisapper->viewwillunload->viewdidunload->dealloc