Viewcontroller is a vital part of app development, whether it's the presentation of pages and the interaction between data, Viewcontroller provides a framework to manage and build apps. There are two ways to build apps in iOS-Viewcontroller management such as Navigationcontroller and Uitabbarcontroller, The other is the custom Viewcontroller we often work with or the usual uitableviewcontroller to show the data we need. Here is a picture of Apple's official website to enhance understanding:
life cycle (lifecircle)
The life cycle of a view can actually be understood as Load-present-hidden (load-show-hide) three stages, which should look like this in the order in which the methods are executed from the Viewcontroller:
Loadview→viewdidload→viewwillappear→viewdidappear→viewwilldisappear→viewdiddisappear→dealloc
Loadview: Not used in general, unless you need to override the settings view;
Viewdidload/dealloc: The view is loaded after the completion of the settings and the view is destroyed when the call;
Viewwillappear/viewwilldisappear: The view is about to be rendered and the view is about to disappear;
Viewdidappear/viewdiddisappear: The view is displayed at the time of the screen and the view disappears completely in the screen when the call, the default does not do any action;
If you look at the life cycle, there is a very similar picture on the Internet, specific to the following, from the Apple official website:
You can rewrite the code to see the effect:
-(void) viewdidload { [super viewdidload]; Additional setup after loading the view, typically from a nib. NSLog (@ "Viewdidload-keso");} -(void) Viewwillappear: (BOOL) animated{ NSLog (@ "viewwillappear-flyelephant");} -(void) Viewdidappear: (BOOL) animated{ NSLog (@ "Viewdidappear");} -(void) Viewwilldisappear: (BOOL) animated{ NSLog (@ "Viewwilldisappear");} -(void) Viewdiddisappear: (BOOL) animated{ NSLog (@ "viewdiddisappear-http://www.cnblogs.com/xiaofeixiang/");}
Viewwilllayoutsubviews and viewdidlayoutsubviews If you use automatic layout, you need to modify the constraints in the automatic layout, you can operate in Viewwilllayoutsubviews, Recently encountered one is the text text box too many words when the text cannot be pinned, you can operate in Viewwilldidsubviews:
-(void) viewdidlayoutsubviews { [Self.mytextview setcontentoffset:cgpointzero animated:no];}
The invocation of several methods in the life cycle actually need to look at their own operations, in the right place to do the right thing, will be more effective, the specific need to see their familiarity ~
Viewcontroller Switching
The purpose of the switch is to put the right content on the user's phone screen, showing different content is our goal, the general situation there will be three ways, the line (Segue), Modal (modal), Push (need and Navigationcontroller with use), Custom segue, normally will generally involve the first two, if there is a special need to study the third kind of customization, push way before the article, this article mainly talk about the modal code implementation of two ways, if the non-code everything control select modal can;
Presentviewcontroller mode, the first load is a xib is no problem, the code is as follows:
Mainviewcontroller *maincontroller=[[mainviewcontroller Alloc]init]; Maincontroller.modaltransitionstyle=uimodaltransitionstylepartialcurl; [Self Presentviewcontroller:maincontroller animated:yes completion:nil];
uimodaltransitionstylecoververtical- The default, from down, to go down; uimodaltransitionstylecrossdissolve- gradients appear and disappear;
uimodaltransitionstylefliphorizontal- horizontal flip; uimodaltransitionstylepartialcurl-The effect of flipping books;
Firstviewcontroller corresponding view is not xib in storyboard, storyboard view is called Presentviewcontroller If you still use the above initialization method is black screen, in fact, replaced by storyboard instantiateviewcontrollerwithidentifier can ~
Firstviewcontroller *firstcontroller=[self.storyboard instantiateviewcontrollerwithidentifier:@ " Firstviewcontroller "]; [Self Presentviewcontroller:firstcontroller animated:yes completion:nil];
For a long time did not write articles, compared shallow, if there is improper, a lot of corrections ~
iOS Development-viewcontroller life cycle and switching