iOS Development controller creation and loading (life cycle)

Source: Internet
Author: User

1 , how to create a controller

There are several common ways to create controllers:

(1) Create with storyboard

(2) Create directly

Mjviewcontroller *MJ = [[Mjviewcontroller alloc] init];

(3) Specify the Xib file to create

Mjviewcontroller *MJ = [[Mjviewcontroller alloc] Initwithnibname:

@ "Mjviewcontroller" bundle:nil];

Note that after you create the XID, you also set which view in the XID is the Controllerview view (and use storyboard to create the controller without setting it because the system has been set up automatically) by:

First set the file's owner to the controller, such as:

Indicates which class (Controller) The XID file is serviced for. any view, such as UIView , XID , Storyboard and so on) set the class , which means that the view is serviced for that class, that is, manipulating the controls in the view from that class, or initializing the view, and so on.

Then set the view of the controller, the method is the right key file's owner Drag the view option to the relevant view, such as:

If you select the following option when creating a class that inherits Viewcontroller, you can omit this step (the XID file is automatically created and the above steps are automatically completed):

When you create a controller through XID, you can set it to Rootviewcontroller in the following three ways if the new controller name and the name of the XID file are the same:

Way One:

Self.window.rootViewController =

[[Mjviewcontroller alloc] initwithnibname:@ "Mjviewcontroller" bundle:nil];

Way two:

Self.window.rootViewController = [[Mjviewcontroller alloc] Initwithnibname:nil Bundle:nil];

This sets the Initwithnibname parameter to nil, which is equivalent to method three , The first is to find the file named Mjview.xid as the initialization of the XID file, if not found Mjview.xid file, will go to find the file named Mjviewcontroller.xid as the initialization of the XID file.

Way three:

Self.window.rootViewController = [[Mjviewcontroller alloc] init];

"Remarks" Apple recommends creating a view of the controller in the Viewload method of the controller.

2 , through Storyboard Create controller details

(1) Load the storyboard file first (test is the file name of storyboard)

Uistoryboard *storyboard = [Uistoryboard storyboardwithname:@ "Test" bundle:nil];

(2) Then initialize the controller in the storyboard

Initialize the "Initial controller" (the controller that the Arrow refers to)

Mjviewcontroller *MJ = [Storyboard Instantiateinitialviewcontroller];

Initialize the corresponding controller with an identity

Mjviewcontroller *MJ =

[Storyboard instantiateviewcontrollerwithidentifier:@ "MJ"];

For example, create a new storyboard file (. Storyboard), drag inside a viewcontroller, how to display the Viewcontroller in this storyboard file to UIWindow?

First, to load this storyboard:

Uistoryboard *storyboard =

[Uistoryboard storyboardwithname:@ "the Bundle:nil];

Then using this storyboard to create the Viewcontroller, now we are creating a new class Mjtwoviewcontroller, which inherits from Viewcontrolle, and then:

Mjtwoviewcontroller *VC = [Storyboard Instantiateinitialviewcontroller];

Finally, the controller is set to Rootviewcontroller:

Self.window.rootViewController = VC;

If you have more than one viewcontroller in storyboard, you can load the specified Viewcontroller using the following methods:

Mjtwoviewcontroller *VC =

[Storyboard instantiateviewcontrollerwithidentifier:@ "MyID"];

If we want to set events for the controls in storyboard Viewcontroller, such as button click events, we only need to drag the line into the Mjtwoviewcontroller and implement the appropriate method.

3 , Controller View the Load

The view of the controller is lazily loaded: it is loaded again when it is used.

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

The controller's view is loaded and the Viewdidload method is called.

3. Multi-Controller

An iOS app rarely consists of a single controller, unless the app is extremely simple. When there are multiple controllers in the app, we need to manage these controllers. When you have multiple view, you can use a large view to manage 1 or more small view. The same is true for controllers, which use 1 of controllers to manage multiple controllers.

For example, use a controller A to manage 3 controllers B, C, D: Controller A is called the "parent controller" of Controller B, C, D. Controllers B, C, d are referred to as controller A's "sub-controllers".

To facilitate the management of the Controller, iOS offers 2 more special controllers:

Uinavigationcontroller

Uitabbarcontroller

5 , the life cycle of the controller

/**

* View loading complete

*/

-(void) viewdidload

{

[Super Viewdidload];

NSLog (@ "Mjoneviewcontroller-viewdidload");

}

/**

* View will be displayed on the window

*

*/

-(void) Viewwillappear: (BOOL) animated

{

[Super viewwillappear:animated];

NSLog (@ "Mjoneviewcontroller-viewwillappear");

}

/**

* View display is complete ( shown to Window )

*/

-(void) Viewdidappear: (BOOL) animated

{

[Super viewdidappear:animated];

NSLog (@ "Mjoneviewcontroller-viewdidappear");

}

/**

* View is about to be removed from the window ( will not be visible )

*

*/

-(void) Viewwilldisappear: (BOOL) animated

{

[Super viewwilldisappear:animated];

NSLog (@ "Mjoneviewcontroller-viewwilldisappear");

}

/**

* View completely removed from window ( completely invisible )

*

*/

-(void) Viewdiddisappear: (BOOL) animated

{

[Super viewdiddisappear:animated];

NSLog (@ "Mjoneviewcontroller-viewdiddisappear");

}

/**

* Call When view is about to be destroyed

*/

-(void) viewwillunload

{

[Super Viewwillunload];

}

/**

* Call When view is finished destroying

*/

-(void) viewdidunload

{

[Super Viewdidunload];

}

/**

* When a memory warning is received

*/

-(void) didreceivememorywarning

{

[Super didreceivememorywarning];

}

There is also a method in the Notes controller:

-(void) Loadview;

This method is called when the view in the controller is loaded, usually by rewriting this method to change the view of the controller.

iOS Development controller creation and loading (life cycle)

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.