Explain how to create a controller in IOS UI development _ios

Source: Internet
Author: User

The creation of the controller
Description: The controller has three ways to create, described below.

One, the first way to create (using code to create directly)

1. Create an empty iOS project.

2. Add a controller class for your project.

3. Create a controller directly in the proxy method.

Copy Code code as follows:

#import "YYAppDelegate.h"
#import "YYViewController.h"

@implementation Yyappdelegate

-(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];

Creating a Controller
Yyviewcontroller *controller=[[yyviewcontroller Alloc]init];
Set the background color of the controller view
Controller.view.backgroundcolor=[uicolor Browncolor];
Set this controller as the root controller for Windows
Self.window.rootviewcontroller=controller;
[Self.window makekeyandvisible];
return YES;
}


4. The controller's view is added to the window to display it.

Two, the second way of creating (created by Storyboard)

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

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

3. Pay attention to this type of error.

Copy Code code as follows:

-(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];

Yyviewcontroller *controller=[[yyviewcontroller Alloc]init];
Self.window.rootviewcontroller=controller;

[Self.window makekeyandvisible];
return YES;
}


Note: This is 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 code:

Copy Code code as follows:

#import "YYAppDelegate.h"
#import "YYViewController.h"

@implementation Yyappdelegate

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];
Self.window.backgroundColor = [Uicolor Whitecolor];

Yyviewcontroller *controller=[[yyviewcontroller Alloc]init];

1. Load Storyboard, (note: This is simply loading the storyboard with the name test, and will not create the controllers and controls in storyboard)
Uistoryboard *storyboard=[uistoryboard storyboardwithname:@ "test" bundle:nil];

2. The following method represents a controller (initial controller) in which the arrows in the storyboard are created
Yyviewcontroller *controller=[storyboard Instantiateinitialviewcontroller];

Reference
uinib *nib=[uinib nibwithnibname:@ "test" bundle:nil];
[nib Instantiatewithowner:nil Options:nil];

3. Set the Controller as window root controller
Self.window.rootviewcontroller=controller;

[Self.window makekeyandvisible];
return YES;
}


Steps:

(1) Loading storyboard

(2) Create a controller

(3) Setting the controller to the root controller of window

5. Expand

New requirements: If you have multiple controllers in a 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 in front of the controller you want to create and creating them in your code.

Implementation code:

Copy Code code as follows:

#import "YYAppDelegate.h"
#import "YYViewController.h"

@implementation Yyappdelegate

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
    self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds];
 & nbsp;  Self.window.backgroundColor = [Uicolor Whitecolor];
   
//    yyviewcontroller *controller=[[yyviewcontroller alloc]init];
   
   //1. Loading Storyboard
    uistoryboard *storyboard=[ Uistoryboard storyboardwithname:@ "test" bundle:nil];
   
   //2. Create the specified controller
    uiviewcontroller *controller=[ Storyboard Instantiateinitialviewcontroller];
   
   //3. Set the root controller for Windows to
    Self.window.rootviewcontroller=controller;
   
    [Self.window makekeyandvisible];
    return YES
}


Tip: Note the type of the specified controller you created.

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

Implementation code:

Copy Code code as follows:

#import "YYAppDelegate.h"
#import "YYViewController.h"

@implementation Yyappdelegate

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];
Self.window.backgroundColor = [Uicolor Whitecolor];

1. Loading Storyboard
Uistoryboard *storyboard=[uistoryboard storyboardwithname:@ "test" bundle:nil];

2. Create the specified controller

Uiviewcontroller *controller=[storyboard instantiateviewcontrollerwithidentifier:@ "one"];
3. Set the Controller as window root controller
Self.window.rootviewcontroller=controller;

[Self.window makekeyandvisible];
return YES;
}


Three, Third way to create (using Xib)

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

2. Create process and attention points

(1) Create code:

Copy Code code as follows:

#import "YYAppDelegate.h"
#import "YYViewController.h"

@implementation Yyappdelegate

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];
Self.window.backgroundColor = [Uicolor Whitecolor];

1. Create a controller through Xib
Yyviewcontroller *controller=[[yyviewcontroller alloc]initwithnibname:@ "two" bundle:nil];
2. Set this controller as window root controller
Self.window.rootviewcontroller=controller;

[Self.window makekeyandvisible];
return YES;
}


(2) Two error points of attention

1 cannot be loaded (is unable to load a nib named "two")

Cause: No action was made in the Xib file.

Workaround: Drag a view into the Xib

2) loaded the xib, but did not set the output (loaded the "two" nib but the view outlet is not set. ')

Cause: Xib view is not set to Yyviewcontroller view

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

Iv. imitation of the creation of a storyboard project controller

1. Create a project

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

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

Copy Code code as follows:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{

Override point for customization after application launch.
1. Create Winodw
Self.window = [[UIWindow alloc] Initwithframe:[uiscreen mainscreen].bounds];
2. Create a Controller
Uistoryboard *storyboard = [Uistoryboard storyboardwithname:@ "Main" bundle:nil];
Njviewcontroller * VC = [Storyboard Instantiateinitialviewcontroller];
3. Set the window's root controller
Self.window.rootViewController = VC;
4. Show window
[Self.window makekeyandvisible];

return YES;
}


The creation of the controller's view
one or 6 ways to create a controller view

Copy Code code as follows:

#import "NJAppDelegate.h"
#import "NJViewController.h"
/*
1. No xib of the same name
2. Create by Storyboard
3. Created with specified xib
4. Xib with the same name
5. The case of removing controll with the same name
6.loadveiw
*/
@implementation Njappdelegate

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
Create UIWindow
Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];
Self.window.backgroundColor = [Uicolor Whitecolor];

/*
1. The first way: no Xib and storyboard
(if there are no xib and storyboard, a blank view is automatically created as the controller's veiw)
Njviewcontroller *VC = [[Njviewcontroller alloc] init];
*/

/*
2. Create by Storyboard
If created through storyboard, the arrows are created to point to view as a controller veiw

If the controller's Loadview method is overridden, the view described in storyboard is not created as the controller's view, but instead creates a blank veiw as the controller's VEIW
Uistoryboard *storyboard = [Uistoryboard storyboardwithname:@ "Test" bundle:nil];
Njviewcontroller *VC = [Storyboard Instantiateinitialviewcontroller];
*/

/*
3. Created with specified xib
If you pass Xib, you will create the veiw described in Xib as the controller veiw
Njviewcontroller *VC = [[Njviewcontroller alloc] initwithnibname:@ "one" bundle:nil];
*/

/*
4. Xib with the same name
If you have a xib with the same name, you will automatically find the view as the controller described in Xib of the same name Veiw
Njviewcontroller *VC = [[Njviewcontroller alloc] init];
*/

/*
5. The xib situation of removing controller with the same name
If there is a xib with the same name that removes controller, the view of the xib is automatically found as the controller
Njviewcontroller *VC = [[Njviewcontroller alloc] init];
*/

6. Loadveiw method of rewriting controller
If you override the controller's Loadview method, you will not load the Xib and xib with the same name that created the controller with the same name, but instead create a blank veiw as the controller's VEIW
Njviewcontroller *VC = [[Njviewcontroller alloc] init];

Setting the root controller for Windows
Self.window.rootViewController = VC;
Show window
[Self.window makekeyandvisible];

return YES;
}


Six kinds of ways:

1. No xib of the same name
2. Create by Storyboard
3. Created with specified xib
4. Xib with the same name
5. The case of removing controll with the same name
6.loadveiw

Second, create the controller view priority

Apple's Official document schematic:

Third, the controller view delay loading

Description

The controller's view is deferred loading: reload when used

You can use the Isviewloaded method to determine whether a Uiviewcontroller view has been loaded

The Viewdidload method is invoked when the controller's view is loaded

Copy Code code as follows:

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
{
1. Create UIWindow
Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];
Self.window.backgroundColor = [Uicolor Whitecolor];


2. Create a Controller
Njviewcontroller *VC = [[Njviewcontroller alloc] init];

In fact, it is two-step operation, first call the Loadview method, create the controller's veiw, and then set the controller's view color is purple, that is, the color of the last one to cover off the previous color
Vc.view.backgroundColor = [Uicolor Purplecolor];

3. Set the Controller as window root controller
Self.window.rootViewController = VC;

4. Show window (veiw of controller used in this line)
[Self.window makekeyandvisible];

return YES;
}


In the Master controller file:

Copy Code code as follows:

#import "NJViewController.h"

@interface Njviewcontroller ()

@end

@implementation Njviewcontroller

Loadview is invoked when the controller needs to display the view of the controller
You can create a view to controller in the Loadview method
This method is generally used to customize the view of the controller
-(void) Loadview
{
When to call Loadveiw represents when to load the controller's VEIW
NSLog (@ "Loadview");

Self.view = [[UIView alloc] init];
Self.view.backgroundColor = [Uicolor Greencolor];
}

-(void) viewdidload
{
[Super Viewdidload];
NSLog (@ "Viewdidload");
}
@end


Related Article

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.