Uiviewcontroller (view controller) presumably everyone will not be unfamiliar, often used in development. This time, talk about its life cycle.
1, the life cycle of the view
Said to be the life cycle of the viewcontroller, actually refers to the view life cycle that it controls. Whenever the state of a view changes, the view controller automatically invokes a series of methods to respond to changes.
With these methods, we can track the entire lifecycle of the view. Each method is listed as follows in order of execution:
(1) Init: Initialization program
(2) Loadview: View initialization
This method should not be invoked directly, but by the system automatically. It loads or creates a view and assigns it to the view property of the Uiviewcontroller.
When overriding the Loadview method, do not invoke the method of the parent class.
(3) Viewdidload: View loading complete, but not yet displayed on the screen
We can rewrite this method and do some other initialization work on the view. For example, you can remove some views, modify constraints, load data, and so on.
(3) Viewwillappear: Called when the view is about to be displayed on the screen
In this way, we can change the current screen orientation or the style of the status bar.
(4) Viewdidapper: Called when the view is displayed on the screen
In this method, we can do some changes to the display effect on the view.
(5) Viewwilldisappear: Called when the view is about to disappear, be overwritten, or hidden
(6) Viewdiddisappear: Call When view has disappeared, overwritten, or hidden
(7) Viewvillunload: When the memory is too low, you need to release some of the views that do not need to use, will be called when released
(8) Viewdidunload: Called when the memory is too low to release some unwanted views.
Note: Since IOS6, both Viewwillunload and viewdidunload have been abolished. When the system emits a memory warning, it automatically clears the view and does not need to be dealt with specifically.
The system also calls the Didreceivememorywarning method to notify the view controller that we can do some work here to free up some additional resources. (Usually, the most resource-less view has been cleaned up by the system.) )
2, conversion of view state
In practice, views are typically not executed at once by the process listed above, and may be converted between visible and invisible states. For example, the first view is visible, then we jump to another viewcontroller, when the original view becomes invisible. Back we jump back, then this view is also visible.
When the visibility of the view changes, the method corresponding to the view controller also responds. The following figure is visible:
In particular, it is noted that the two states of appearing and disappearing can be transformed into each other.
3, test sample description
(1) Viewcontroller is the Home view controller, and we print all of the lifecycle-related functions inside it.
(2) At the same time Viewcontroller added a "Jump" button, click to jump to another view controller (Anotherviewcontroller).
(3) There is a "back" button in the Anotherviewcontroller, and the click will return to the previous page.
4, testing the code
(1) Viewcontroller.swift
Import Uikit
Class Viewcontroller:uiviewcontroller {
View Initialization
Override Func Loadview () {
Super.loadview ()
Print ("Loadview")
}
View load Complete
Override Func Viewdidload () {
Super.viewdidload ()
Print ("Viewdidload")
Create a Jump button
Let Button:uibutton = UIButton (type:. System)
Button.frame=cgrectmake (10, 50, 100, 30)
Button.settitle ("Jump", forstate:. Normal)
Button.addtarget (self,action: #selector (Jump), forcontrolevents:. Touchupinside)
Self.view.addSubview (button);
}
When the view will appear
Override func Viewwillappear (Animated:bool) {
Print ("Viewwillappear")
}
Execution of view display after completion
Override func Viewdidappear (Animated:bool) {
Print ("Viewdidappear")
}
When the view is going to disappear, execute
Override func Viewwilldisappear (Animated:bool) {
Print ("Viewwilldisappear")
}
Execution when view has disappeared
Override func Viewdiddisappear (Animated:bool) {
Print ("Viewdiddisappear")
}
Execute when memory warning is received
Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
Jump to another view
Func jump () {
Print (click the button to start the jump!) ")
Let ANOTHERVC = Anotherviewcontroller ()
Presentviewcontroller (ANOTHERVC, Animated:true, Completion:nil)
}
}
(2) Anotherviewcontroller.swift
Import Uikit
Class Anotherviewcontroller:uiviewcontroller {
Override Func Viewdidload () {
Super.viewdidload ()
Create a return button
Let Button:uibutton = UIButton (type:. System)
Button.frame=cgrectmake (10, 150, 100, 30)
Button.settitle ("Back", Forstate:.) Normal)
Button.addtarget (self,action: #selector (back), forControlEvents:. Touchupinside)
Self.view.addSubview (button);
}
Back to Previous view
Func back () {
Print (click the button and start back!) ")
Self.dismissviewcontrolleranimated (True, Completion:nil)
}
Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}
5, run the test
We jump from Viewcontroller to Anotherviewcontroller, and then back to Viewcontroller from Anotherviewcontroller. The entire console prints out