The life cycle of the iOS view controller

Source: Internet
Author: User

Original blog, reproduced please indicate the source

Http://blog.csdn.net/hello_hwc?viewmode=contents

The so-called life cycle, which is the sequence of calls to several functions, is used to create a viewcontroller with storyboard as an example

Then we test the following code

////viewcontroller.m////Created by Huangwenchen on 14/12/26.//Copyright (c) 2014 Huan Gwenchen. All rights reserved.//#import "ViewController.h" @interface Viewcontroller () @end @implementation viewcontroller-(ID)    Initwithcoder: (Nscoder *) adecoder{if (self = [Super Initwithcoder:adecoder]) {NSLog (@ "initwithcoder called"); } return self;}    -(void) viewdidload {[Super viewdidload];    NSLog (@ "viewdidload called");    Additional setup after loading the view, typically from a nib.    }-(void) Viewwillappear: (BOOL) animated{[Super viewwillappear:animated]; NSLog (@ "viewwillappear called");}    -(void) Viewdidappear: (BOOL) animated{[Super viewdidappear:animated]; NSLog (@ "viewdidappear called");} -(void) awakefromnib{NSLog (@ "awakefromnib called");} -(void) viewwilllayoutsubviews{NSLog (@ "viewwilllayoutsubviews called");} -(void) viewdidlayoutsubviews{NSLog (@ "viewdidlayoutsubviews called");} @end 
Then, run on the emulator, and the output is

2014-12-29 19:42:47.904 kvcforcsdn[2370:126741] initwithcoder called2014-12-29 19:42:47.907 KVCForCSDN[2370:126741] awakefromnib called2014-12-29 19:42:47.927 kvcforcsdn[2370:126741] viewdidload called2014-12-29 19:42:47.927 kvcforcsdn[2370:126741] viewwillappear called2014-12-29 19:42:47.961 kvcforcsdn[2370:126741] ViewWillLayoutSubviews called2014-12-29 19:42:47.963 kvcforcsdn[2370:126741] viewdidlayoutsubviews called2014-12-29 19:42:47.970 KVCForCSDN [2370:126741] Viewdidappear called

Therefore, the call order is

1 init function (init;initwithframe;initwithcoder; etc)--initialization

2 awakefromnib--'s work here before Loadview.

3 viewdidload--Note that this function will only be called once in a Viewcontroller life cycle

4 Viewwillappear--The view will appear and will be called every time the view disappears

5 viewwilllayoutsubviews--Brief pair trying to lay out

6 viewdidlayoutsubivews--Complete Child attempt layout

7 viewdidappear--View will appear on the screen

---The above code does not contain part

8 Viewwilldisappear--view is going to disappear.

9 Viewdiddisappear--view has disappeared.

These nine are the usual sequence of function calls in the view controller life cycle, and be sure to know where the code should be placed in the View controller life cycle

BTY:

Didreceivememorywarning

This function is also generally considered within the life cycle, when memory is low, it should be properly freed, or iOS will force the current app to close.

The life cycle of the iOS view controller

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.