Creation of the Controller

Source: Internet
Author: User

Creation of the Controller

Description: There are three ways to create a controller, described below.

One, the first creation method (directly created using code)

1. Create an empty iOS project.

2. Add a controller class for the project.

3. Create a controller directly in the proxy method.

1 #import "YYAppDelegate.h" 2 #import "YYViewController.h" 3  4 @implementation yyappdelegate 5  6-(BOOL) Applicat Ion: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchoptions 7 {8     Self.window = [ [UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; 9     //Override point for customization after application launch.10     self.window.backgroundColor = [Uicolor WHITECOLOR];11     //Create controller     yyviewcontroller *controller=[[yyviewcontroller alloc]init];14     // Set the background color of the controller view     controller.view.backgroundcolor=[uicolor browncolor];16     //Set the Controller as window's root controller     Self.window.rootviewcontroller=controller;18     [Self.window makekeyandvisible];19     return YES;20}

4. The view of the controller is added to the window to display.

Two, the second way of creation (created through storyboard)

1. Add a Storyboard file named Test and drag a view controller on the interface.

2. Set the controller to associate with the Yyviewcontroller class in the program and set the color of the controller view to differentiate.

3. Be aware of this erroneous notation.

1-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions 2 {3     Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; 4     //Override point for Customiza tion after application launch. 5     self.window.backgroundColor = [Uicolor Whitecolor]; 6      7     yyviewcontroller *controller=[[ Yyviewcontroller Alloc]init]; 8     Self.window.rootviewcontroller=controller; 9     [Self.window makekeyandvisible];11     return YES; 12}

Note: This is just not possible, because the program does not load storyboard, so there is no way to create the controller we want. What to do? Explicitly tell the storyboard to load.

4. Create the Code:

 1 #import "YYAppDelegate.h" 2 #import "YYViewController.h" 3 4 @implementation yyappdelegate 5 6-(BOOL) application: (U IApplication *) Application didfinishlaunchingwithoptions: (nsdictionary *) launchoptions 7 {8 Self.window = [[UIWindow Alloc] Initwithframe:[[uiscreen mainscreen] bounds]; 9 Self.window.backgroundColor = [Uicolor whitecolor];10 one//Yyviewcontroller *controller=[[yyviewcontroller A lloc]init];12 13//1. Load Storyboard, (note: This is simply loading storyboard with the name test and does not create controllers and controls in storyboard) Uistoryboard *story Board=[uistoryboard storyboardwithname:@ "Test" bundle:nil];15 16//2. The following method represents the controller (initial controller) that the arrow points to in the Create storyboard Yviewcontroller *controller=[storyboard instantiateinitialviewcontroller];18 19//reference//uinib *nib=[UINib NIB     withnibname:@ "Test" bundle:nil];21//[nib Instantiatewithowner:nil options:nil];22 23//3. Set the controller to the window's root controller 24 self.window.rootviewcontroller=controller;25 [Self.window makekeyandvisible];Return yes;28} 

Steps:

(1) Load Storyboard

(2) Create a controller

(3) Set the controller to the window's root controller

5. Expand

New requirement: If you have multiple controllers in one storyboard, how do you specify which particular controller to create?

Here are two ways to consider:

(1) Change the initial controller by dragging the arrows to the controller you want to create and creating them in your code.

Implementation code:

1 #import "YYAppDelegate.h" 2 #import "YYViewController.h" 3  4 @implementation yyappdelegate 5  6-(BOOL) Applicat Ion: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchoptions 7 {8     Self.window = [ [UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; 9     self.window.backgroundColor = [Uicolor whitecolor];10     //    Yyviewcontroller *controller=[[ Yyviewcontroller alloc]init];12     //1. Loading Storyboard14     Uistoryboard *storyboard=[uistoryboard storyboardwithname:@ "Test" bundle:nil];15     //2. Create the specified controller     uiviewcontroller *controller=[ Storyboard instantiateinitialviewcontroller];18     //3. Set the Controller as window's root controller     Self.window.rootviewcontroller=controller;21     [Self.window makekeyandvisible];23     return YES;24}

Tip: Note the type of the specified controller that is created.

(2) Create a specified controller by setting a unique identifier

Implementation code:

1 #import "YYAppDelegate.h" 2 #import "YYViewController.h" 3  4 @implementation yyappdelegate 5  6-(BOOL) Applicat Ion: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchoptions 7 {8     Self.window = [ [UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; 9     self.window.backgroundColor = [Uicolor whitecolor];10     //1. Loading storyboard12     Uistoryboard * Storyboard=[uistoryboard storyboardwithname:@ "Test" bundle:nil];13     //2. Create the specified controller     Uiviewcontroller *controller=[storyboard instantiateviewcontrollerwithidentifier:@ "One"];17     //3. Set the Controller as window's root controller     self.window.rootviewcontroller=controller;19     [Self.window Makekeyandvisible];21     return yes;22}

Iii. third method of creation (using Xib)

1. Create a new Xib file, named Two.xib.

2. Creating processes and points of attention

(1) Create code:

1 #import "YYAppDelegate.h" 2 #import "YYViewController.h" 3  4 @implementation yyappdelegate 5  6-(BOOL) Applicat Ion: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchoptions 7 {8     Self.window = [ [UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; 9     self.window.backgroundColor = [Uicolor whitecolor];10     one     //1. Creating a controller via Xib Yyviewcontroller     * Controller=[[yyviewcontroller alloc]initwithnibname:@ "Two" bundle:nil];13     //2. Set this controller as window's root controller     self.window.rootviewcontroller=controller;15     [Self.window makekeyandvisible];17     return YES;18}

(2) Two error points of attention

1) Cannot load (wasunable to load a nib named "")

Cause: No action was made in the Xib file.

Workaround: Drag a view into the Xib

2) The Xib is loaded, but the output is not set (loaded the "both" nib but the view outlet is not set. '

Cause: Xib view is not set to Yyviewcontroller view

Workaround: Set file ' owner, which can be understood to set the file to whom it belongs, and connect to file's owner and view. The connection is because there may be multiple view in a xib, which is already wired by default in storyboard.

Iv. simulation of the creation of a project controller with storyboard

1. Create a project

2. In the configuration file, delete the name of the main storyboard (storyboard not found).

3. Code (Storyboard does what the following code does)

1-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions 2 {3     4     //Override point for customization after application launch. 5     //1. Create Winodw 6     Self.window = [[Uiwindo] W alloc] Initwithframe:[uiscreen mainscreen].bounds]; 7     //2. Create Controller 8     uistoryboard *storyboard = [Uistoryboard storyboardwithname:@ "Main" Bundle:nil]; 9     Njviewcontroller * VC = [Storyboard instantiateinitialviewcontroller];10     //3. Set the window's root controller     one by one Self.window.rootViewController = vc;12     //4. Display Window13     [Self.window makekeyandvisible];14     Return yes;16}

Creation of the Controller

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.