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.
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.
- [Java] view plain copy
Li class= "li3" > [[mjviewcontroller alloc] initwithnibname:@ "Mjviewcontroller"  BUNDLE:NIL];  
-
- If the Xib file name is not clearly transmitted, the Xib file with the same name as Uiviewcontroller is loaded [Java] view plain copy
- [[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
[Java] View plain copy
- 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:
[Java] View plain copy
- 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 in the 3rd it is mentioned: 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:
[Java] View plain copy
- -(void) Loadview {
- Self.view = [[[UIWebView alloc] Initwithframe:[uiscreen mainscreen].applicationframe] autorelease];
- }
You don't need to call [Super Loadview], you call it and you don't make an error, it just makes some unnecessary overhead.
To summarize, the Apple design method is for our custom Uiviewcontroller view .
Viewdidload
1. When is it called?
But do you create a view of Uiviewcontroller by xib files or by rewriting Loadview, and after the view is created, the Viewdidload method is eventually called
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:
[Java] View plain copy
- -(void) viewdidload
- [Super viewdidload];
- //Add a button
- UIButton *button = [UIButton buttonwithtype:uibuttontypecontactadd];
- [Button addtarget:self action:@selector(click) forcontrolevents:uicontroleventtouchupinside];
- [Self.view Addsubview:button];
Viewdidunload
1. When is it called?
IOS The memory of the 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
[Java] View plain copy
- -(void) viewdidunload {
- [Super viewdidunload];
- Self.name = nil;
- Self.pwd = nil;
- }
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.
Relationship of three 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
The relationship between Loadview, Viewdidload and Viewdidunload