Uikit Framework (5) Controller creation

Source: Internet
Author: User

In Uikit, Uiviewcontroller and its subclasses describe the view controller, which is a page

This article describes the three main ways to create a controller


    • Pure Code Creation

That is: Alloc+init way, such as:

Amviewcontroller * VC = [[Amviewcontroller alloc] init];


    • Load from Storyboard

A. Creating a Uistoryboard object

+ (Uistoryboard *) Storyboardwithname: (NSString *) name bundle: (nsbundle*) Storyboardbundleornil

Name parameter: Storyboard file name with no extension

Storyboardbundleornil parameter: Pass nil can

B. Getting the Controller object from a Uistoryboard object

-(ID) instantiateinitialviewcontroller//Remove unique initial controller-(ID) Instantiateviewcontrollerwithidentifier: (NSString *) Identifier//by ID

Such as:

Uistoryboard * Stoyrboard = [Uistoryboard storyboardwithname:@ "main" bundle:nil];//amviewcontroller *VC = [Stoyrboard Instantiateinitialviewcontroller]; Amviewcontroller *VC = [Stoyrboard instantiateviewcontrollerwithidentifier:@ "Redviewcontroller"];


Controller ID: Multiple controllers can be designed in the storyboard file, each controller can manage an ID

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7C/9C/wKiom1bTqCaA396EAABTRm5h0X8778.png "title=" screen shot 2016-02-29 a.m. 10.06.34.png "alt=" Wkiom1btqcaa396eaabtrm5h0x8778.png "/>


    • Load from Xib

Xib Design controller View, there are the following two ways:

Method One:

The controller can be placed in the xib, such as:

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7C/9C/wKiom1bTqHmwEvH8AAAlBbSCbtY333.png "title=" screen shot 2016-02-29 a.m. 10.10.07.png "alt=" Wkiom1btqhmwevh8aaalbbscbty333.png "/>

NSBundle loadnibnamed is still used in the code: created from NIB

Design of the controller, you can also set the associated subclass type

-(Nsarray *) loadnibnamed: (NSString *) name owner: (ID) Owner options: (Nsdictionary *) options

Name parameter: The file name of the passed Xib file (without the suffix name)

The remaining parameters are nil using the default values

Return value: An array of all objects in the Xib file, typically a xib file that only designs one object


Such as:

Amviewcontroller * VC = [[[NSBundle Mainbundle] loadnibnamed:@ "xxx"] lastobject];


Method Two:

A view is still placed in the Xib, the file ' s owner in Xib is selected, and the class is set to the controller subclass

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7C/9C/wKiom1bTqpHSqwInAAAwul7-ldg677.png "title=" screen shot 2016-02-29 a.m. 10.19.04.png "alt=" wkiom1btqphsqwinaaawul7-ldg677.png "/> 650" this.width=650; "src=" Http://s3.51cto . Com/wyfs02/m02/7c/9b/wkiol1btqu2b8o36aaa5qg3_pvc464.png "title=" screenshot 2016-02-29 a.m. 10.18.37.png "Alt=" Wkiol1btqu2b8o36aaa5qg3_pvc464.png "/>

Right-click File ' s Owner,view property to connect to the View object in Xib

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/7C/9B/wKioL1bTqsyywinFAAB-d6aX16Y221.png "title=" screen shot 2016-02-29 a.m. 10.17.09.png "alt=" Wkiol1btqsyywinfaab-d6ax16y221.png "/>

The code uses the Uiviewcontroller init method to create its view from the Xib

-(Instancetype) Initwithnibname: (NSString *) nibname Bundle: (NSBundle *) nibbundle

Nibname parameter: Xib file name, not including suffix name

Nibbundle parameter: Pass nil can

Such as:

Amviewcontroller * VC = [[Amviewcontroller alloc] initwithnibname:@ "View" bundle:nil];











This article is from the "Teacheran" blog, make sure to keep this source http://annmeng.blog.51cto.com/3321237/1745898

Uikit Framework (5) Controller creation

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.