Create an iOS learning controller and an ios Controller

Source: Internet
Author: User

Create an iOS learning controller and an ios Controller

This blog is a summary blog, which summarizes various methods for creating controllers and some operations that require attention.

1. Create a controller through storyboard

As I mentioned in my previous blog, when the Main Interface is not selected, we can only create a UIWindow using code instead of using the Main. storyboard created by the system.

Creating a controller through storyboard is also not selected in the Main Interface, but we also use the Main. storyboard created by the system to create the root controller of the window in different ways.

  • Step 1: initialize a UIWindow object in the-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions method of the AppDelegate. m file. The Code is as follows:
// Create window self. window = [[UIWindow alloc] initWithFrame: [UIScreen mainScreen]. bounds];
  • Step 2: Create a UIStoryboard object and initialize it as Main. storyboard

Initialization Method:

// Parameter 1: storyboard file name, without the suffix // parameter 2: indicates [NSBundle mainBundle] + (UIStoryboard *) storyboardWithName :( NSString *) when set to nil *) name bundle :( nullable NSBundle *) storyboardBundleOrNil;

Instance code:

// Load the storyboard file name. The file name is initialized as Main without a suffix. storyboard, whose name is Main // nil: [NSBundle mainBundle] UIStoryboard * storyboard = [UIStoryboard storyboardWithName: @ "Main" bundle: nil];
  • Step 3: Create a controller through storyboard

① Specify the Controller pointed by the arrow as the window root Controller

Method:

// Load the Controller pointing to by the arrow-(nullable _ kindof UIViewController *) instantiateInitialViewController;

Instance code:

// Create a controller through storyboard // instantiateInitialViewController: load the Controller UIViewController * vc = [storyboard instantiateInitialViewController] pointed by the arrow;

Instance diagram:

For example, we set the controller of the pink background to the Controller of the default Main. storyboard. When we use the above Code, the program runs as follows: for example, a pink window.

② Create a root controller using the Storyboard ID

Method:

// The parameter is the Storyboard ID string-(_ kindof UIViewController *) instantiateViewControllerWithIdentifier :( NSString *) identifier;

Instance code:

// Create the Controller UIViewController * vc = [Storyboard instantiateViewControllerWithIdentifier: @ "green"] through the storyboard ID;

As shown in figure ①, we set the Storyboard ID of the controller of the green background to "green", but the Main. the default controller of storyboard is still a controller with a pink background. Using the above instance code, the running result is a green window as shown in Figure

For more information, see the previous blog.

2. Create a controller through xib

When you create a controller through xib but the Main Interface is not selected, you must first initialize a UIWindow object in the corresponding method in the AppDelegate. m file.

Method for creating xib:

① When you create a View Controller class, select Also create XIB file and an xib corresponding to the Controller class will be created, for example:

The xib name created in this method is the same as the corresponding class name.

② Create an xib with a name that can be customized

Settings after creation: (for example)

(1) Select the file owner Option

(2) bind a controller

(3) bind the xib View

The method for creating a root controller through xib is the initialization method of the controller. The instance code is as follows:

// Create the Controller ViewController * vc = [[ViewController alloc] initWithNibName: @ "ViewController" bundle: nil] Through xib;

Special syntax for loading xib:

// Special Syntax 1: UIViewController * vc = [[ViewController alloc] initWithNibName: nil bundle: nil]; // special syntax 2: UIViewController * vc = [[ViewController alloc] init];

The underlying implementation of loading the xib View with the above special Syntax:

If the xib that describes the Controller View has the same name as the controller class

Only the init method of the controller calls initWithNibName: bundle:

As long as the controller is initialized through initWithNibName: bundle: And the nibName is nil, the following steps are performed:

(1) first, find whether there is any xib with the same name as the Controller class but no Controller. If there is one, it will be loaded (XMGView. xib)

(2) If no, find whether there is any xib with the same name as the controller class name. If yes, it will be loaded (XMGViewController. xib)

(3) If none of them are found, create an empty view,

 

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.