iOS Knowledge Learning _ View Controls Object Lifecycle-init, Viewdidload, Viewwillappe

Source: Internet
Author: User

iOS view Control object life cycle-init, Viewdidload, Viewwillappear, Viewdidappear, viewwilldisappear, viewdiddisappear differences and uses

Init-Initialization program

viewdidload-load View

ViewwillappearThe view of the-uiviewcontroller object is about to be added to the window;

ViewdidapperThe view of the-uiviewcontroller object has been added to the window when called;

ViewwilldisappearThe view of the-uiviewcontroller object is about to disappear, be overwritten, or be hidden;

ViewdiddisappearThe view of the-uiviewcontroller object has disappeared, is overwritten, or is hidden;

viewvillunload-when memory is too low, you need to free some unused views when you are about to release the call;

Viewdidunload-Called when the memory is too low to release some unwanted views.


The view control object is created by Alloc and Init, but the view control object does not create the corresponding view at the moment of creation, but rather waits until it is needed to be created by calling Loadview, which improves memory usage . For example, when a tag has many Uiviewcontroller objects, a view of any Uiviewcontroller object is created only if the corresponding label is selected.

For example, the following code:

[Plain] view plaincopy

  1. -(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) Nibbundleornil

  2. {

  3. self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil];

  4. if (self) {

  5. Custom initialization

  6. Uitabbaritem *TBI = [self tabbaritem];

  7. [TBI settitle:@ "CurrentTime"];

  8. [Self view] setbackgroundcolor:[uicolor Yellowcolor];

  9. }

  10. return self;

  11. }


We will set the instance variable in the Uiviewcontroller init method to yellow in Init, run the program, we can see that the background is actually yellow, but the view is loaded when we don't need to use the view. Accessing the instance variable view in the initial method of Uiviewcontroller causes the delayed load mechanism to fail, which does not appear to be very serious, but if you take into account the low memory warning , then the problem is big ...


Run the program, select Hardware in the simulator and simulate memory too low warning, we will find that the original set of yellow background is missing, because, memory is too low, the view control object will be issued a memory too low warning when the didreceivememorywarning message received, the method is implemented by default, Checks if the view control object's view is visible, if it is not visible, releases it, and the next time the view is loaded, the Init method is not executed, but only the Viewdidload method is executed, so you need to[[ SelfView ] SetBackgroundColor:[UicolorYellowcolor]];put inViewdidload, So if the view is freed because the memory is too low, the next time you need to use that view, the program defaults to theViewdidloadmethod so that the background color comes out again.

Execution order of Init, Viewdidload, Viewwillappear, Viewdidappear, Viewwilldisappear, Viewdiddisappear:

From the log view of the program:

2012-10-19 15:51:44.811inhyron[483:b903] Init

2012-10-19 15:51:54.081inhyron[483:b903] Viewdidload

2012-10-19 15:51:54.082inhyron[483:b903] Viewvillappear

2012-10-19 15:51:54.084 inhyron[483:b903] Viewdidappear

It is clear that the first execution Init , and then execute Viewdidload , and then the Viewwillappear Finally, Viewdidappear , so that the view is created, when the view disappears or is overwritten:

2012-10-19 15:54:14.557inhyron[483:b903] Viewwilldisappear

2012-10-19 15:54:14.558inhyron[483:b903] Viewdiddisappear

This way the view disappears when the memory warning is simulated:

2012-10-19 15:55:14.333inhyron[483:b903] Received simulated memory warning.

2012-10-19 15:55:14.334inhyron[483:b903] Viewwillunload

2012-10-19 15:55:14.335 Inhyron[483:b903]viewdidunload

As can be seen from log, when the view control object receives a memory warning, the program executes Viewwillunload and finally executes viewdidunload.

In this way, a set of processes will be completed, do not know whether the written details ....

Still have to see not understand, can take off my small Demo: http://download.csdn.net/detail/weasleyqi/4664799, In the currenttimeviewcontroller.m of the detailed log are written, we can take research.


iOS Knowledge Learning _ View Controls Object Lifecycle-init, Viewdidload, Viewwillappe

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.