Relationship between loadview, viewdidload, and viewdidunload

Source: Internet
Author: User

The three methods mentioned in the title are all uiviewcontroller methods, which are closely related to the life cycle of the view attribute of uiviewcontroller. Next, I will describe their functions and their relationships.

I. loadview

1. When will it be called?

The loadview method is called every time you access the view of uiviewcontroller (such as controller. View and self. View) and the view is nil.

 

2. What is the function? 

The loadview method is used to create a view of uiviewcontroller.

 

3. What is the default implementation?

The default implementation is what is done in [Super loadview.

1>It first searches for the XIB file associated with uiviewcontroller and creates the view of uiviewcontroller by loading the XIB file,

If the XIB file name is specified in uiviewcontroller initialization, the corresponding XIB file will be loaded Based on the imported XIB file name.

[[Viewcontroller alloc] initwithnibname: @ "viewcontroller" Bundle: Nil];

If the XIB file name is not explicitly uploaded, The XIB file with the same name as uiviewcontroller will be loaded.

[[Viewcontroller alloc] init]; // load viewcontroller. XIB

2>If the associated XIB file is not found, a blank uiview is created and assigned to the view attribute of uiviewcontroller, which is roughly as follows:

Self. view = [[[uiview alloc] initwithframe: [uiscreen mainscreen]. applicationframe] autorelease];

Note: The applicationframe value is: {x = 0, y = 20}, {width = 320, Height = 460 }}

In [Super loadview], the content described in 1> and 2> above is roughly completed.

 

4. How can I use this method correctly?

We all know that the view of uiviewcontroller can be created through the XIB file, but in some cases, XIB is not so flexible, so sometimes we want to create a uiview through code, such:

Self. view = [[[uiwebview alloc] initwithframe: [uiscreen mainscreen]. applicationframe] autorelease];

If you want to use code to create a view of uiviewcontroller, You need to override the loadview method and do not need to call [superloadview], because as mentioned in point 3rd: If there is no XIB file, [Super loadview] A blank uiview is created by default. Since we need to customize the uiview through code, there is no need to create a blank uiview in advance to save unnecessary overhead. The correct method should be as follows:

-(Void) loadview

{

Self. view = [[[uiwebview alloc] initwithframe: [uiscreenmainscreen]. applicationframe] autorelease];

}

You do not need to call [Super loadview], and you will not make an error when calling it, but it causes unnecessary overhead.

To sum up, Apple designed this method to customize the view of uiviewcontroller.

 

Ii. viewdidload

 

1. When will it be called?

Whether you use the XIB file or override loadview to create the view of uiviewcontroller, The viewdidload method will be called after the view is created.

 

2. What is the function?

In general, we will perform Initialization on the interface here, such as adding some sub-views to the view, loading model data from the database or network, and assembling it into the sub-view. For example:

-(Void) viewdidload

{

[Super viewdidload];

// Add a button

Uibutton * button = [uibuttonbuttonwithtype: uibuttontypecontactadd];

[Button addtarget: Self action: @ selector (click) forcontrolevents: uicontroleventtouchupinside];

[Self. View addsubview: button];

}

3. viewdidunload

 

1. When will it be called? 

The memory of iOS devices is extremely limited. If the application occupies too much memory, the system will issue a memory warning to the application. Uiviewcontroller receives the didreceivememorywarning message. The default implementation of the didreceivememorywarning method is: if the current uiviewcontroller view is not in the viewhierarchy of the application, that is, when the superview of the view is nil, the view will be released, and call the viewdidunload method.

 

2. What is the function?

As mentioned above, the viewdidunload method is called when a memory warning is issued and the view is released. Therefore, resources related to interface elements are usually released, assign all related instances to nil.

-(Void) viewdidunload

{

[Super viewdidunload];

Self. Name = nil;

Self. Pwd = nil;

}

 

3. dealloc is also used to release resources. What is the relationship with viewdidunload?

When the viewdidunload method is called with a memory warning, the view is released and the uiviewcontroller is not released. Therefore, the dealloc method is not called. That is, the viewdidunload and dealloc methods do not have any relationship. The dealloc method is called only when the uiviewcontroller is released.

 

Iv. Relationship between the three methods

 

1.When you access the view of uiviewcontroller for the first time, the view is nil, and then the loadview method is called to create the view.

 

2.After a view is created, the viewdidload method is called to initialize the interface elements.

 

3.When the memory is warned, the system may release the view of uiviewcontroller, assign the view value to nil, and call the viewdidunload method.

 

4.When you access the view of uiviewcontroller again, the view has been assigned a value of nil in 3, so the loadview method is called to recreate the view.

 

5.After the view is re-created, the viewdidload method is called to initialize the interface elements.

Relationship between loadview, viewdidload, and viewdidunload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.