You can create controllers in several ways.

Source: Internet
Author: User

You can create controllers in several ways.

How to Create a controller:

<Method 1> Create directly:

XZOneViewController * one = [[XZOneViewController alloc] init];
One. view. backgroundColor = [UIColor redColor];
Self. window. rootViewController = one;

 

<Method 2> Create on storyboard:

1. Actively load the storyboard (load the already described items from the existing storyboard)

// 1.0 create a storyboard file Two. storyboard first, but do not set it in Main Interface to start loading main storyboard (remove main ).

 

// 1.1 load the storyboard file without creating the controller in the storyboard.

// Nil indicates mainBundle, and operations on the storyboard object mean operations on the Two. storyboard file.
UIStoryboard * storyboard = [UIStoryboard storyboardWithName: @ "Two" bundle: nil];

 

// 1.2 create a controller in the storyboard

// Case 1 of 1.2:

// UIViewController * vc = [storyboard instantiateInitialViewController]; // The returned result is the Controller indicated by the arrow.

// Case 2 of 1.2:
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier: @ "pink"]; // This controller is a controller with no arrows pointing to, and identifier is pink

Self. window. rootViewController = vc;

// 1:

 

2. Set storyboard as the Main storyboard of the entire program. After the program is started, the Main. storyboard will be loaded:

The code for Automatically Generating and loading storyboard is:

// 1. Create a window
Self. window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen]. bounds];

// 2. Load the storyboard and create the initialization controller (Controller pointed by the arrow)
UIStoryboard * storyboard = [UIStoryboard storyboardWithName: @ "Main" bundle: nil];
Self. window. rootViewController = [storyboard instantiateInitialViewController];

// 3. display window
[Self. window makeKeyAndVisible];

 

Loading the main storyboard will accomplish three things:

1> Create a window

2> Create the Initial View Controller pointed by the arrow.

3> set the Controller as the root controller of the window. The view of the controller is displayed on the window.

 

Conclusion: 1. the storyboard does not take the initiative to load it;

2. As long as the main window is set, storyboard code is automatically generated and loaded.

<Method 3> specify the xib file to create it:

XZViewController * viewVc = [[XZViewController alloc] initWithNibName: @ "xzVc" bundle: nil]; // an xzVc has been created before. xib file. Remember to drag the view of xib to the corresponding controller (file 'owner ).

 

 

======================== For MJ videos

 

 

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.