iOS controller life cycle

Source: Internet
Author: User

    • The life cycle of the controller in iOS

In general, there are three ways to create a controller.

1. Create directly from code

2. Create with Storyboard

3. Through Xib, when the controller is created, pass in a xib file as the view of this controller.

    • Create directly from code

  Creating this kind of general by code, we print the call order can be found

  

The corresponding code call order is Loadview, Viewdidload, Viewwillappear, Viewwilllayoutsubviews, Viewdidlayoutsubviews-&G T Viewdidappear

  

// 1.- (void) Loadview;//This is where subclasses should create their custom viewHierarchyifthey aren'T using a nib. Should never be called directly.// 2.- (void) Viewdidload;//called after the view has been loaded. For view controllers created in code, this is After-loadview. For view controllers unarchived from a nib, which is after the view is set.//3.- (void) Viewwillappear: (BOOL) animated;//Called when the view was about to made visible. Default does nothing//4.- (void) viewwilllayoutsubviews Ns_available_ios (5_0);//called just after the view controller's view ' s Layoutsubviews method is invoked. Subclasses can implement as necessary. The default is a NOP.//5.- (void) Viewdidlayoutsubviews Ns_available_ios (5_0);

After the controller is created, the other controller pops up (the current controller disappears) and executes the code sequence viewwilldisappear, Viewwilllayoutsubviews, Viewdidlayoutsubviews, Viewdiddisappear

  

  

//1.- (void) Viewwilldisappear: (BOOL) animated;//called when the view is dismissed, covered or otherwise hidden. Default does nothing//2.- (void) viewwilllayoutsubviews Ns_available_ios (5_0);//called just after the view controller's view ' s Layoutsubviews method is invoked. Subclasses can implement as necessary. The default is a NOP.//3.- (void) viewdidlayoutsubviews Ns_available_ios (5_0);//4.- (void) Viewdiddisappear: (BOOL) animated;//called after the view is dismissed, covered or otherwise hidden. Default does nothing

    • Creating a controller from storyboard

  

(Classclass])]; [Self Presentviewcontroller:touch animated:yes completion:nil];

For convenience, while reducing hand errors, we use the class name here to reuse the ID

by printing to see

  

You can see that the code is called in order awakefromnib, Loadview, Viewwillappear, Viewwilllayoutsubviews, viewdidlayoutsubviews Viewdidappear

//1.- (void) awakefromnib Ns_requires_super;//2.- (void) Loadview;//This is where subclasses should create their custom view hierarchy if they aren ' t using a nib. Should never be called directly.//3.- (void) Viewdidload;//called after the view has been loaded. For view controllers created in code, this is After-loadview. For view controllers unarchived from a nib, which is after the view is set.//4.- (void) Viewwillappear: (BOOL) animated;//Called when the view was about to made visible. Default does nothing//5.- (void) viewwilllayoutsubviews Ns_available_ios (5_0);//called just after the view controller's view ' s Layoutsubviews method is invoked. Subclasses can implement as necessary. The default is a NOP.- (void) viewdidlayoutsubviews Ns_available_ios (5_0);//6.- (void) Viewdidappear: (BOOL) animated;//called when the view had been fully transitioned onto the screen. Default does nothing

  It is important to note that we are the controller that is loaded by storyboard, so we create the view according to the storyboard description.

  

    • To create a load controller with Xib

First create a xib, shortcut command + N to create.

  

  

After Xib, we also need to set up the file

  

      This is set to the controller we created

  

All that is required has been set up, and at this time we can code it.

class ]) Bundle:nil]; [Self Presentviewcontroller:touch animated:yes completion:nil];

Let's observe the life cycle of the controller.

The code level is

//1.- (void) Loadview;//This is where subclasses should create their custom view hierarchy if they aren ' t using a nib. Should never be called directly.//2.- (void) Viewdidload;//called after the view has been loaded. For view controllers created in code, this is After-loadview. For view controllers unarchived from a nib, which is after the view is set.//3.- (void) Viewwillappear: (BOOL) animated;//Called when the view was about to made visible. Default does nothing//4. May be called multiple times- (void) viewwilllayoutsubviews Ns_available_ios (5_0);//called just after the view controller's view ' s Layoutsubviews method is invoked. Subclasses can implement as necessary. The default is a NOP.- (void) viewdidlayoutsubviews Ns_available_ios (5_0);//5.- (void) Viewdidappear: (BOOL) animated;//called when the view had been fully transitioned onto the screen. Default does nothing

  Description

  When loading XIB, we can also not specify the XIB name so that Loadview will eventually load the XIB with the same name as the controller (the naming specification is important)

Dviewcontroller *touch = [[Dviewcontroller alloc] init];[ Self Presentviewcontroller:touch animated:yes completion:nil];

Also, if we delete dviewcontroller.xib and create a dview.xib when we do not specify the Xib name above, it can also be loaded (the naming convention is important).

We can know that the process of loading the xib is, if the name is specified, then load the xib of the specified name, if no name is specified will be loaded with the controller with the same name Xib, if not, will find the controller name of the same but without controller xib to load.

    • A present B Controller returns, life cycle of two controllers

  1. Create a Controller

  

Process:

A:loadviewa:viewdidloada:viewwillappeara:viewwilllayout ... A:viewdidappear

2. A present to B Controller

  

Process:

a:loadviewa:viewdidloada:viewwillappeara:viewwilllayout     ... // may be called multiple times a:viewdidappear--------------Present B Controller--------------B:loadviewb:viewdidloada:viewwilldisappearb : Viewwillappearb:viewlayout ....    // may be called multiple times a;viewlayout    ... // may be called multiple times b:viewdidappear    // multiple calls, which may be reversed with the following call order A:viewdiddisappear

iOS controller 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.