Life cycle
We set up a simple model to test the life cycle: Create a new two Viewcontroller, one is the main view controller (Viewcontroller, hereinafter referred to as MAINVC), and the other is the sub Viewcontroller, Hereinafter referred to as SUBVC), click on a button in MAINVC, switch to SUBVC in modal mode, then click another button in the SUBVC to close SUBVC and return to MAINVC. We print out each of the two controllers, each of which performs the following steps:
Case 1. Run the app for the first time:
Main Loadview
Main Viewdidload
Main Viewwillappear
Main Viewdidappear
Case 2. Click button in MAINVC to switch to SUBVC in modal mode:
Sub Loadview
Sub Viewdidload
Main Viewwilldisappear
Sub Viewwillappear
Sub Viewdidappear
Main Viewdiddisappear
Case 3. In SUBVC, click button to close SUBVC and return to MAINVC.
Sub Viewwilldisappear
Main Viewwillappear
Main Viewdidappear
Sub Viewdiddisappear
Sub Dealloc
The order in which the code executes when a view controller is created and displayed on the screen:
Step 1: Alloc creating objects, allocating space
Step 2: Init (initwithnibname) initialization of the object
Step 3:loadview loading views from the nib , usually this step does not need to interfere. Unless you're not using the xib file to create a view
Step 4:viewdidload loading complete, custom data can be customized and other controls are created dynamically
Step 5: Theviewwillappear view will appear before the screen, and this view will be displayed on the screen immediately.
Step 6: Viewdidappear View has been rendered on the screen completed
The order of execution when a view controller is removed from the screen and destroyed:
Step 1: viewwilldisappear View will be removed from the screen before execution
Step 2: The Viewdiddisappear view has been removed from the screen and the user cannot see the view.
Step 3: Dealloc view is destroyed
Here we need to talk about the difference between Loadview and viewdidload: When Loadview, there is no view, and Viewdidload, the view has been created. Detailed load loops:
Step 1: The View property of the program request Viewcontroller
Step 2: If the view is in memory, it is loaded directly; if it does not exist, call the Loadview method
The step 3:loadview method performs the following methods:
- If this method is overloaded, you must create the necessary uiview and pass a non-nil value to the Viewcontroller view property.
- If this method is not overloaded, Viewcontroller will attempt to load the view from the nib file by default using its own Nibname and Nibbundle properties. If the nib file is not found, it tries to find a nib file that matches the Viewcontroller class name.
- If there is no nib file available, then it creates an empty uiview as its view.
Finally, consider an important situation: out of memory warnings. When the program receives a memory warning, it invokes each Viewcontroller didreceivememorywarning method, and we need to make a corresponding release of the resources temporarily unnecessary in the program, which is usually overridden by the But remember to call the super when rewriting the method.
During ios3.0-ios6.0, thedidreceivememorywarning method determines whether the current Viewcontroller view is displayed on the window, or if it is not displayed on the window, Didreceivememorywarning automatically destroys the Viewcontroller view with all its child view, and then calls the View controller's Viewdidunload method. But starting with iOS6.0, Viewdidunload and viewwillunload have been abolished, and the system will not release the view when it receives low-memory, but only release the controller's resource.
A common way to handle memory warnings:
-(void) didreceivememorywarning{ [Super didreceivememorywarning]; float ver = [[[Uidevice Currentdevice] systemversion] floatvalue]; if (ver >= 6.0f) { if (self.isviewloaded &&!self.view.window) { Self.view = nil;//ensure Next Reload } }}
The above code first obtains the current iOS system version number, if it is iOS6.0 or above, further determine whether the view is loaded into memory, and whether it is the current view, both conditions are satisfied (already loaded into memory && not the current view), Set Self.view to nil so that the viewcontroller is guaranteed to be called again, Loadview and Viewdidload are called again.
We simulate a memory warning in the Xcode debugger to monitor the status of this switch:
Case 4. When you have switched to SUBVC, simulate a memory warning and return MAINVC, do not process didreceivememorywarning.
Received memory warning.
Main didreceivememorywarning
Sub Didreceivememorywarning
Sub Viewwilldisappear
Main Viewwillappear
Main Viewdidappear
Sub Viewdiddisappear
Sub Dealloc
Case 5. When you have switched to SUBVC, simulate a memory warning and return MAINVC to handle the didreceivememorywarning.
Received memory warning.
Main didreceivememorywarning
Sub Didreceivememorywarning
Main Loadview
Main Viewdidload
Sub Viewwilldisappear
Main Viewwillappear
Main Viewdidappear
Sub Viewdiddisappear
Sub Dealloc
It is clear that when didreceivememorywarning is processed , the Loadview and Viewdidload methods of the non-current view are re-executed.
The life cycle of Uiviewcontroller