A detailed description of the controller

Source: Internet
Author: User

The controller is used to process, monitor the view of the event and status.

The controller and the controller view are different.

One, the controller's various creation way

1. Create with Storyboard

Load Storyboard

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

Create the controller that the storyboard arrows refer to (Initialize the "Initial controller" (the controller that the Arrow refers to))

Uiviewcontroller *VC = [Storyboard Instantiateinitialviewcontroller];

Create a controller with storyboard ID pink inside storyboard (initialize the corresponding controller with an identity)

Mjtwoviewcontroller *VC = [Storyboard instantiateviewcontrollerwithidentifier:@ "Pink"];

Self.window.rootViewController = VC;

Add: Storyboard is actually a special xib file Xib file is actually view

2. Create directly

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

3. Specify the Xib file to create (note in xib that you want file ' owner ' to right-click on the View)

Mjviewcontroller *MJ = [[Mjviewcontroller alloc] initwithnibname:@ "Mjviewcontroller" bundle:nil];

Second, the creation of the view of the controller

1.

Official Chart (less nibname judgment)

2. Delayed loading of Controller view (lazy loading)

• Reload when used • The Isviewloaded method can be used to determine if a uiviewcontroller view has been loaded · The controller's view is loaded and the Viewdidload method is called (the controller's view is created only once so viewdidload is only called once)

Third, the life cycle of the Controller view

/**

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

Since the controller view is no longer available, some data that needs to be displayed on the view is not required

Self.apps = nil;

Self.persons = nil;

//

[Self.apps release];

[Self.persons release];

}

/**

* When a memory warning is received

*/

-(void) didreceivememorywarning

{

[Super didreceivememorywarning];

}

Memory warning processing (first uploaded to the Appdelegate object applicationdidreceivememorywarning: method to the entire app memory processing and then to the controller

Didreceivememorywarning: Method off Super method automatically remove the view of the controller that is not displayed on the window nil in the MRC is also required in Viewdidunload: methods such as

Four, multi-controller management

1. Understanding the management of multiple controls

• An iOS app rarely consists of one controller, unless the app is extremely simple • We need to manage these controllers when there are multiple controllers in the app • With multiple view, you can manage 1 or more small view controllers with a large view. Manage multiple controllers with 1 controllers • For example, use a controller A to manage 3 controllers B, C, dø controller A is called "Parent controller" of Controller B, C, d. O controller B, C, D is called controller A "sub-controller" • For easy management of the controller, iOS offers 2 more special controllers 1.uinavigationcontroller2.uitabbarcontroller

Easy to manage multiple controllers for easy switching between controllers

Simple use of 2.UINavigationController

(1) Uinavigationcontroller use steps Ø Initialize Uinavigationcontrollerø settings UIWindow Rootviewcontroller for Uinavigationcontrollerø depending on the situation, add the corresponding number of sub-controllers by the push method

1. Create a navigation controller

Mjoneviewcontroller *one = [[Mjoneviewcontroller alloc] init];

Uinavigationcontroller *nav = [[Uinavigationcontroller alloc] initwithrootviewcontroller:one];

Stack for all sub-controllers

Nav.viewcontrollers

This array also holds the sub-controller

Nav.childviewcontrollers

2. Adding a child controller

Mjoneviewcontroller *one = [[Mjoneviewcontroller alloc] init];

[Nav Addchildviewcontroller:one];

[Nav Pushviewcontroller:one Animated:yes];

Nav.viewcontrollers = @[one];

3. Set as the root controller for Windows

Self.window.rootViewController = nav;

[Self.window makekeyandvisible];

(2) Uinavigationcontroller sub-controller Uinavigationcontroller Save the sub-controller as a stack

@property (nonatomic,copy) Nsarray *viewcontrollers;

@property (nonatomic,readonly) Nsarray *childviewcontrollers;

• Use the push method to push a controller into the stack

-(void) Pushviewcontroller: (Uiviewcontroller *) Viewcontroller animated: (BOOL) animated;

• Use the Pop method to remove the controller

Ø Remove the controller from the top of the stack

-(Uiviewcontroller *) popviewcontrolleranimated: (BOOL) animated;

Ø Back to the specified sub-controller

-(Nsarray *) Poptoviewcontroller: (Uiviewcontroller *) Viewcontroller animated: (BOOL) animated;

Ø Back to root controller (stack bottom controller)

-(Nsarray *) poptorootviewcontrolleranimated: (BOOL) animated;

(3) Uinavigationcontroller View structure (simple understanding of complex needs deeper digging with recursion to print out its sub-view)

(4) How to modify the contents of the navigation bar

• The contents of the navigation bar are determined by the Navigationitem property of the stack top controller Uinavigationitem has the following properties affecting the contents of the navigation bar the back button in the upper left corner

@property (Nonatomic,retain) Uibarbuttonitem *backbarbuttonitem;

Ø Title view in the middle

@property (Nonatomic,retain) UIView *titleview;

Ø Title text in the middle

@property (nonatomic,copy) NSString *title;

Ø Upper left corner of the view

@property (Nonatomic,retain) Uibarbuttonitem *leftbarbuttonitem;

Øuibarbuttonitem *rightbarbuttonitem view in the upper right corner

@property (Nonatomic,retain) Uibarbuttonitem *rightbarbuttonitem;

(5) How to manage the controller in deep mining uinavigationcontroller? (Only the navigation controller can operate push (push in) pop (jump))

When the one controller is pushed in, the one controller is placed in the stack as both the bottom of the stack and the top of the stack. Then the view of one controller is created and placed on the view of the navigation controller

When the one controller is pushed in and the other controller is put in the stack and the controller is called the top of the stack, then the view of the controller is created and placed on the view of the navigation controller, and the one controller's view is not destroyed in the rest state.

When the three controller is pushed in, the three controller is put into the stack three controller is called the top of the stack and the view of the three controller is created and placed on the view of the navigation controller, and the view of the controller is not destroyed in the rest state.

When the three controller pops out and the controller is located at the top of the stack, then the view of the controller is destroyed and the view of the three controller is destroyed.

Additional knowledge:

1.IOS7 feature is full screen window full screen Controller View also full screen display in navigation controller The color of the navigation bar can assimilate the color of the status bar, its navigation bar height or 44 is easily mistaken for 64


A detailed description of the controller

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.