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.
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.
Customizing the life cycle of view creation
12. If the code is generated, then call the view's init, initWithFrame:, Layoutsubviews method
-
controller view load mode:
1 2 3 in Storyboard 4 . If not found, then the system produces a blank view
-
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
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