Brief discussion on Uiviewcontroller life cycle

Source: Internet
Author: User

Uiviewcontroller is the carrier and controller of the top-level view in iOS, the interaction between user and program interface is controlled by Uiviewcontroller, Uiviewcontroller manages the UIView life cycle and the load and release of resources.

We created a simple demo to test its life cycle, create two new Viewcontroller, a page A, and a B page. A page has a button to jump to page B, and NSLog is added to each method in each Viewcontroller to print its status. Intercept the AVIEWCONTROLLER.M section code as follows,

  #pragma  mark --  life cycle test---(instancetype) init{    if (self =  [super init]) {        self.title = @ "a page";     }    nslog (@ "init :%@", self.title);     return  self;} -(void )  loadview{    [super loadview];    nslog (@ " loadview :%@ ", self.title);} -(void) Viewdidload {    [super viewdidload];    nslog (@ " viewdidload :%@ ", self.title);     [self setupui];} -(void)  viewwillappear: (BOOL) animated{    [ super viewwillappear:animated];     nslog (@ "viewwillappear :%@", Self.title);} -(void)  viewdidappear: (BOOL) animated{    [super viewdidappear:animated];     nslog (@ "viewdidappear :%@", self.title);} -(void)  viewwilldisappear: (BOOL) animated{    [super viewwilldisappear:animated];     nslog (@ "viewwilldisappear :%@", Self.title);} -(void)  viewdiddisappear: (BOOL) animated{    [super viewdiddisappear:animated];     nslog (@ "viewdiddisappear :%@", Self.title);} -(void) didreceivememorywarning {    [super didreceivememorywarning];     nslog (@ "didreceivememorywarning :%@", self);} -(void)  dealloc{    nslog (@ "dealloc: %@", Self.title);}

Then start running the test

Case 1: start the app for the first time

After clicking the Start button, go to page A, as

650) this.width=650; "src=" http://km.oa.com/files/photos/pictures/201508/1438517195_100_w375_h689.jpg "width=" 263 "height=" 483 "alt=" 1438517195_100_w375_h689.jpg "/>

This prints out the following information

650) this.width=650; "src=" Http://km.oa.com/files/photos/pictures/201508/1438516707_2_w468_h79.jpg "alt=" 1438516707_2_w468_h79.jpg "/>

Analysis :

Visible, the following methods are executed

1. (alloc) Init

assigning addresses and initializing Viewcontroller objects

2, Loadview

Each viewcontroller will have a view property, which is a uiview type. The Loadview method is used to load this view, usually this step does not need to intervene, the parent class has been implemented, if you want to override this method, either call the parent class of the method, or customize a UIView object and assign the value to the current Viewcontroller view property. This view is equivalent to a canvas, and the rest of the custom controls are generally added to it.

3, Viewdidload

As the name implies, the view mentioned above will call this method when it is loaded, and some code for custom controls can be written here.

4, Viewwillappear

The view will be called before the screen is displayed.

5, Viewdidappear

The view has been called after screen rendering is complete.

Case 2: Click on the button, from the a page push to page b

As shown

650) this.width=650; "src=" http://km.oa.com/files/photos/pictures/201508/1438517442_94_w375_h689.jpg "width=" 246 " height= "452" alt= "1438517442_94_w375_h689.jpg"/>

The console printing information is

650) this.width=650; "src=" Http://km.oa.com/files/photos/pictures/201508/1438517548_50_w489_h92.jpg "alt=" 1438517548_50_w489_h92.jpg "/>

Analysis:

1, b page Init

2, b page Loadview

3, b page viewdidload

4, a page viewwilldisappeare

The first three points have been analyzed in #case # has not been analyzed. The 4th Viewwilldisappeare is called when a page is about to disappear on the screen, a new page appears and there will always be an old page disappear

5, b page viewwillappear

6, a page viewdiddisappear

Called after the page has disappeared from the screen.

7, Viewdidappear

Case 3: Click Back in the top left corner, close the B page and return to page a

The console prints the following information

650) this.width=650; "src=" Http://km.oa.com/files/photos/pictures/201508/1438518098_17_w505_h73.jpg "alt=" 1438518098_17_w505_h73.jpg "/>

Analysis:

The first four methods have been analyzed above, it is not cumbersome, the fifth method is Dealloc, is executed when the view is destroyed, because the B page is returned to the a page after the page is no longer required (in a page never know where the next switch page is), and #case, unlike the Because from a page push to B page, a page still has the value of existence (because also need to return to a page, if destroyed a there is no way to return), so a page is not called Dealloc method. In this method, it is generally done to release memory operations.

Case 4: When you push from page A to page B, the analog memory is low and the console prints the following information

650) this.width=650; "src=" Http://km.oa.com/files/photos/pictures/201508/1438518418_77_w517_h49.jpg "alt=" 1438518418_77_w517_h49.jpg "/>

Analysis:

The printing information shows that when the memory is low, the didreceivememorywarning method is triggered, and each Viewcontroller method is called.

Summarize:

As the above example shows, a page that is created will go through a few steps.

Step 1: (alloc) init

Step 2:loadview

Step 3:viewdidview

Step 4:viewwillappear

Step 5:viewdidappear

A page to be removed will go through the following steps

Step 1:viewwilldisappear

Step 2:viewdiddisappear

Step 3:dealloc (If this page is no longer needed)

is a summary of the above analysis

650) this.width=650; "src=" Http://km.oa.com/files/photos/pictures/201508/1438521037_82_w878_h846.png "style=" float:left;width:790px;height:760px; "Width=" 790 "height=" 760 "border=" 0 "hspace=" 0 "vspace=" 0 "title=" "alt=" 1438521037_82_w878_h846.png "/> Extensions:

1, when in the AVIEWCONTROLLER.M init, Loadview, Viewdidview or Viewwillappear method inside add sleep (5) statement, will be found to wait about 5 seconds after the launch of the app to display a page, visible, if the page load slow and so on, There may be too many time-consuming operations in these methods.

2, when the Viewdidappear method in the AVIEWCONTROLLER.M to add the Sleep (5) statement, you will find that although a page has been rendered, but the inside of the control can not be manipulated, such as button click No response. It can be seen that some pages are stuck, the response is slow, and the problem may be too much time consuming in this method.


The basic life cycle is probably the case, if there is a mistake or need additional explanation, please correct me.


This article is from the "spiritual Brother Notes" blog, please make sure to keep this source http://kinghacker.blog.51cto.com/7767090/1681064

Brief discussion on Uiviewcontroller life cycle

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.