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