iOS Learning note-viewcontroller/life cycle

Source: Internet
Author: User

Viewcontroller is an important part of the iOS application and is an important bridge between application data and views, Viewcontroller managing many views in your app. Many native Viewcontroller are available in the iOS SDK to support standard user interfaces such as Table View Controllers (Uitableviewcontroller), navigation Controllers (Uinavigationcontroller), tab bar Controllers (Uitabbarcontroller) and ipad-specific uisplitviewcontroller.

By structure, you can divide all viewcontroller of iOS into two categories:
1, mainly used to display the content of the Viewcontroller, this viewcontroller is mainly used for users to display content and interaction with users, such as Uitableviewcontroller,uiviewcontroller.
2, for the control and display of other Viewcontroller Viewcontroller. This kind of viewcontroller is generally a viewcontroller container. such as Uinavigationcontroller,uitabbarcontroller. They all have a property: Viewcontrollers. Where Uinavigationcontroller represents a stack-type structure, push a viewcontroller or pop once, so the latter viewcontroller generally depend on the previous viewcontroller. While Uitabbarcontroller represents an array structure, each viewcontroller is tied.

Viewcontroller use

Uiviewcontroller can be created in two ways, 1, xib mode, 2, code mode

1) xib mode

Command+n new file, select Cocoa Touch uiviewcontroller subclass,subclass of Uiviewcontroller, tick with XIB for user interface. The definition Viewcontroller name is Mainviewcontroller and eventually generates MainViewController.h MAINVIEWCONTROLLER.M mainviewcontroller.xib three files. In the APPDELEGATE.M file,

the Xib file is loaded in the Didfinishlaunchingwithoptions method.  

    Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];    Override point for customization after application launch.    Self.window.backgroundColor = [Uicolor whitecolor];        Mainuiviewcontroller *vc=[[mainuiviewcontroller alloc]initwithnibname:@ "Mainuiviewcontroller" bundle:nil];    SELF.WINDOW.ROOTVIEWCONTROLLER=VC;        [Self.window makekeyandvisible];    return YES;


After loading the Xib file, drag several controls in the Xib file to see the effect.

2) Code creation

Command+n the new file Uiviewcontroller file, which is used in the Didfinishlaunchingwithoptions method of the APPDELEGATE.M file. Use code to create a view in the Uiviewcontroller Loadview method.

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{    Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];    Override point for customization after application launch.    Self.window.backgroundColor = [Uicolor whitecolor];    mainuiviewcontroller *vc=[[mainuiviewcontroller alloc]initwithnibname:@ "Mainuiviewcontroller" Bundle:nil] ;//    SELF.WINDOW.ROOTVIEWCONTROLLER=VC;        Rootviewcontroller *rc=[[rootviewcontroller alloc] init];    SELF.WINDOW.ROOTVIEWCONTROLLER=RC;        [Self.window makekeyandvisible];    return YES;}

CREATE view

-(void) loadview{    [Super Loadview];        UIView *view=[[uiview Alloc]initwithframe:[uiscreen Mainscreen].applicationframe];    view.alpha=0.5;    View.backgroundcolor=[uicolor Cyancolor];    Self.view=view;}

the life cycle of Viewcontroller

The life cycle of the iOS app is written in the front, which is written in the life cycle of the Viewcontroller, which is more like the life cycle of the Android activity (see the final figure of the article). The Viewcontroller life cycle experiences initialization, loading views, destroying views, and ending life.

1) Init method

Initializes the viewcontroller itself.

2) Loadview method

Viewcontroller calls this method when the view needs to be displayed and it is nil.

If the code maintains the view, you need to override this method, and use Xib to maintain the view without rewriting it.

3) Viewdidload method

There is no view when you continue to execute Viewdidload,loadview after executing Loadview, and the view is created when Viewdidload is finished.

4) Viewdidunload method

This method is called when the system is in a tight memory, and didreceivememorywarning is the only way to free up unused memory before iphone OS 3.0, but OS 3.0 and later viewdidunload methods are a better way.

In this method, all Iboutlet (both property and instance variables) are set to nil (the System release view has already been removed).

In this method, other view-related objects are freed, other objects created at run time (but not system-required), objects created in viewdidload, cached data, and so on.

It is generally assumed that viewdidunload is a viewdidload mirror, because Viewdidload is also re-executed when view is re-requested.

5) Dealloc

Frees additional resources or memory.

Life cycle Diagram of Viewcontroller

Viewcontroller loading the view process, see (Loadview)

1) Loadview

Viewcontroller Uninstall View procedure See (Unloadview)

2) Unloadview

/** * @author Zhang Xingye * http://blog.csdn.net/xyz_lmn * iOS starter Group: 83702688 * Android Development Advanced Group: 241395671 * My Sina Weibo:@ Zhang Xingye tbow */

Reference:

https://developer.apple.com/library/ios/#featuredarticles/viewcontrollerpgforiphoneos/introduction/ Introduction.html#//apple_ref/doc/uid/tp40007457-ch1-sw1

Http://xcodev.com/341.html

http://iostrack.com/post/2012-07-20/40029700941

iOS Learning note-viewcontroller/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.