The 3 methods described in the heading are Uiviewcontroller methods that are closely related to the life cycle of the Uiviewcontroller view attribute. Then I'll explain how they work and how they relate to each other.
First, Loadview
1. When is it called?
Each time you access a Uiviewcontroller view (such as Controller.view, Self.view) and the view is Nil,loadview the method is called.
2. What is the role?
The Loadview method is used to create a uiviewcontroller view
3. What is the default implementation?
The default implementation is what [Super Loadview] does.
1> It first looks for the Xib file associated with Uiviewcontroller, and creates Uiviewcontroller view by loading the Xib file.
- If the Xib file name is specified in the initialization uiviewcontroller, the corresponding xib file is loaded according to the Xib file name passed in
[[Mjviewcontroller alloc] initwithnibname:@ "Mjviewcontroller" bundle:nil];
- If the Xib file name is not clearly transmitted, the Xib file with the same name as Uiviewcontroller will be loaded
[[Mjviewcontroller alloc] init]; Load Mjviewcontroller.xib
2> if the associated xib file is not found, a blank uiview is created and then assigned to the Uiviewcontroller view property, roughly as follows
Self.view = [[[UIView alloc] Initwithframe:[uiscreen mainscreen].applicationframe] autorelease];
The value of the Applicationframe is: {{x = 0, y = +}, {width = +, height = 460}}
[Super Loadview] the contents of 1> and 2> are roughly completed.
4. How do I use this method correctly?
We all know that Uiviewcontroller view can be created by xib files, but in some cases xib is not so flexible, so sometimes we want to create uiview through code, such as:
Self.view = [[[UIWebView alloc] Initwithframe:[uiscreen mainscreen].applicationframe] autorelease];
If you want to create a Uiviewcontroller view by code, rewrite the Loadview method, and do not need to call [Super Loadview], because it is mentioned in the 3rd: if there is no Xib file, [Super Loadview] A blank UIView is created by default. Since we are going to customize UIView through code, there is no need to create a blank uiview in advance to save unnecessary overhead. The right approach should be this:
1-(void) Loadview {2 self.view = [[[UIWebView alloc] Initwithframe:[uiscreen Mainscreen].applicationframe] AUTORELEASE];3}
There is no need to call [Super Loadview], and you can call it without error, just cause some unnecessary overhead.
To summarize, Apple's design method is to give us a custom Uiviewcontroller view.
Second, Viewdidload
1. When is it called?
Whether you create a Uiviewcontroller view by xib a file or by overriding the Loadview method, the Viewdidload method is eventually called after the view is created.
2. What is the role?
In general, we will do the initialization on the interface here, such as adding some sub-views to the view, loading the model data from the database or the network into the child view. For example:
1-(void) viewDidLoad2 {3 [super viewdidload];4 5 ///Add a button 6 UIButton *button = [UIButton Buttonwithtype:uibuttontypecontactadd];7 [button addtarget:self action: @selector (click) forcontrolevents: Uicontroleventtouchupinside];8 [Self.view addsubview:button];9}
Third, Viewdidunload
1. When is it called?
The memory of the iOS device is extremely limited, and if the application consumes too much memory, the system will issue a memory warning to the application. Uiviewcontroller will receive didreceivememorywarning message. The default implementation of the Didreceivememorywarning method is that if the current view of Uiviewcontroller is not in the view hierarchy of the application (view Hierarchy), that is, the superview of the view is nil, The view is released and the Viewdidunload method is called
2. What is the role?
It says that when a memory warning is issued and the view is released, the Viewdidunload method is called, so it is generally freeing up resources to release the resources associated with the interface elements, assigning the relevant instances to nil
1-(void) Viewdidunload {2 [super viewdidunload];3 self.name = nil;4 self.pwd = nil;5}
3.dealloc is also used to release resources, what's the relationship with Viewdidunload?
When a memory warning is issued to call the Viewdidunload method, only the view is freed and Uiviewcontroller is not released, so the Dealloc method is not called. That is, the Viewdidunload and Dealloc methods do not have any relationship, the Dealloc method will only be called when Uiviewcontroller is released.
the relationship of 四、三个 methods
1. When you first access the Uiviewcontroller view, the view is nil, and then the Loadview method is called to create the view
The Viewdidload method is called when the 2.view is created to initialize the interface element
3. When a memory warning occurs, the system may release Uiviewcontroller view, assign the view to nil, and call the Viewdidunload method
4. When accessing Uiviewcontroller view again, the view has been assigned nil in 3, so the Loadview method is called to recreate the view
After the 5.view is recreated, the Viewdidload method is called to initialize the interface element
Loadview, Viewdidload and viewdidunload of iOS development