The life cycle of ios:viewcontroller and view

Source: Internet
Author: User

First, Introduction:

the life cycle of Viwe and Viewcontroller is the most basic knowledge, and if the order of execution of their method calls is well understood, the code's excellent style can be well mastered. This blog is very good: http://www.cnblogs.com/peaceWang/p/5346726.html, there is time I will be on this basis to improve, specific about each method is the most appropriate to perform the action.

  1. Life cycle of Controller creation

    1 .   2.  If you generate a viewcontroller from your code, you call the Init method, Loadview,viewdidload,viewwillappear,viewwilllayoutsubviews, and so on.
  2. Customizing the life cycle of view creation

    12. If the code is generated, then call the view's init, initWithFrame:, Layoutsubviews method
  3. controller view load mode:

     1   2    3  in Storyboard   4 . If not found, then the system produces a blank view 
  4. The relationship between the controller and the nib that comes with the controller:

     1   2 . Initializes the controller with the Initwithnib:bundle method, does not call Init, and then calls the Loadview method directly, Viewdidload,viewwillappear, Viewwilllayoutsubviews,viewdidlayoutsubviews,viewdidappear and so on, does not call the controller's Initwithcoder:,awakefromnib method, This is easy to make mistakes 
  5. Sometimes only a xib custom view is added to the controller view, we can directly use the Xib as the controller view, set owner, or you can assign a new view to Viewcontroller.view in Loadview

Second, the view in good code design style is as follows:

#pragmaMark-life cycle-(instancetype) init{if(self =[Super Init])    {[Self setup]; }    returnSelf ;}-(Instancetype) initWithFrame: (cgrect) frame{if(self =[Super Initwithframe:frame])    {[Self setup]; }    returnSelf ;}-(Instancetype) Initwithcoder: (Nscoder *) adecoder{if(self =[Super Initwithcoder:adecoder])    {[Self setup]; }    returnSelf ;}-(void) awakefromnib{[Super awakefromnib]; [Self setup];}-(void) setup{[self setdefalut];    [Self addsubviews]; [Self setupsubviewsconstraints];}-(void) setdefalut{}#pragmaMark-add subviews-(void) setupsubviews{}#pragmaMark-layout subviews-(void) setupsubviewsconstraints {}-(void) layoutsubviews{[Super Layoutsubviews];}#pragmaMark-event response#pragmaMark-public methods#pragmaMark-private methods#pragmaMark-getters and Setters

Third, the viewcontroller of good code design style is as follows:

#pragmaMark-life cycle-(void) viewdidload {[Super viewdidload];    [Self setupnavigation];    [Self setupdefaultvalue]; [Self setupsubviews];}-(void) Viewwillappear: (BOOL) animated{[Super viewwillappear:animated];}-(void) Viewwilldisappear: (BOOL) animated{[Super viewwilldisappear:animated];}-(void) setupnavigation{}-(void) setupdefaultvalue{}-(void) setupsubviews{}#pragmaMark-layout subviews-(void) setupsubviewsconstraints {}#pragmaMark-load data#pragmaMark-delegate methods#pragmaMark-event response#pragmaMark-public methods#pragmaMark-private methods#pragmaMark-getters and Setters

The life cycle of ios:viewcontroller and view

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.