A simple introduction to multiple controllers and navigation controllers

Source: Internet
Author: User

A simple introduction to multiple controllers and navigation controllers

One, 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 there are multiple view, you can use a large view to manage 1 or more small view, the controller is the same, with 1 controllers to manage the other multiple controllers

For example, use a controller A to manage 3 controllers B, C, D. Controller A is referred to as the "parent controller" of Controller B, C, D, and the "sub-controller" of Controller B, C, D, which is called Controller A.

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

Uinavigationcontroller

Uitabbarcontroller

Second, navigation Controller

With Uinavigationcontroller, you can easily manage multiple controllers and easily switch between controllers, typically the system's own "settings" app

Third, the use of uinavigationcontroller steps

(1) Initialize Uinavigationcontroller

(2) Set UIWindow Rootviewcontroller to Uinavigationcontroller

(3) Depending on the situation, add the corresponding number of sub-controllers by the push method

 1 #import "YYAppDelegate.h" 2 #import "YYOneViewController.h" 3 4 @implementation yyappdelegate 5 6-(BOOL) application :(uiapplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchoptions 7 {8 Self.window = [[UIWind Ow alloc] initwithframe:[[uiscreen mainscreen] bounds]]; 9 Self.window.backgroundColor = [Uicolor whitecolor];10 11//1. Create a navigation controller. Uinavigationcontroller *nav=[[u     Inavigationcontroller alloc]init];13//2. Set the navigation controller to the window root view self.window.rootviewcontroller=nav;15 16 17     3. Add a sub-controller to the navigation Controller 18//Create some controllers Uiviewcontroller *c1=[[uiviewcontroller alloc]init];20//Set C1 This controller's view color 21 C1.view.backgroundcolor=[uicolor redcolor];22 uiviewcontroller *c2=[[uiviewcontroller alloc]init];24 C2.vie W.backgroundcolor=[uicolor purplecolor];25 uiviewcontroller *c3=[[uiviewcontroller alloc]init];27 c3.view.b Ackgroundcolor=[uicolor browncolor];28 29//Add these controllers to the navigation controller [NAV PushviewcontroLLER:C1 animated:yes];31 [nav pushviewcontroller:c2 animated:yes];32 [nav pushviewcontroller:c3 animated:YES];33 [Self.window makekeyandvisible];35 return yes;36}

Running the emulator, you can see a primitive with three sub-controllers managing the page.

But now we can only have an interface, we do not need to create three controller at a time to wait here.

Requirements: Create three sub-controllers, each with a button on the view's interface, click to jump to the next screen.

Implementation (complete three pages with a simple jump via buttons):

Note: The creation of the first sub-controller is written in the proxy method.

YYAPPDELEGATE.M File Code

  9 #import "YYAppDelegate.h" #import "YYOneViewController.h" @implementation YYAppDelegate13-(BOOL) Applicatio N: (uiapplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions15 {Self.window = [[UIWi]     Ndow alloc] Initwithframe:[[uiscreen mainscreen] bounds]];17 self.window.backgroundColor = [Uicolor whitecolor];18 19//1. Create a navigation controller Uinavigationcontroller *nav=[[uinavigationcontroller alloc]init];21//2. Set the navigation controller as the root view of window Self.window.rootviewcontroller=nav;23 24 25//3. Add a sub-controller to the navigation controller, Yyoneviewcontroller *one=[[yyoneviewcon     Troller alloc]init];27 [nav pushviewcontroller:one animated:yes];28 [Self.window makekeyandvisible];30 Return YES;31 32 33////create some controller//Uiviewcontroller *c1=[[uiviewcontroller alloc]init];35////Set C1 this control View color of the system//C1.view.backgroundcolor=[uicolor redcolor];37/////Uiviewcontroller *c2=[[uiviewcontroller Alloc ]init];39//C2.VIEW.BAckgroundcolor=[uicolor purplecolor];40/////Uiviewcontroller *c3=[[uiviewcontroller alloc]init];42//C3.vie  W.backgroundcolor=[uicolor browncolor];43//44////Add these controllers to the navigation Controller.//[Nav pushviewcontroller:c1 animated:yes];46 [Nav pushviewcontroller:c2 animated:yes];47//[Nav pushviewcontroller:c3 animated:yes];48}

Create three child control classes and their corresponding xib files

YYONEVIEWCONTROLLER.M file

8  9 #import "YYOneViewController.h" #import "YYTwoViewController.h" @interface Yyoneviewcontroller () 13/**  jump to the second interface  */16-(ibaction) Jump2two: (ID) sender;17 @end19 @implementation YYOneViewController21 22 23- (ibaction) Jump2two: (ID) Sender {     //1. Create a second sub-controller     yytwoviewcontroller *two=[[yytwoviewcontroller alloc] Init];26     //2. The controller is added to the navigation controller.     What is the way to get a navigation controller?      as long as the current controller is a sub-controller of the navigation controller, this property can be obtained directly to the navigation controller where the current controller is located in     [Self.navigationcontroller Pushviewcontroller: Animated:yes];31}32 @end

YYTWOVIEWCONTROLLER.M file

8  9 #import "YYTwoViewController.h" #import "YYThreeViewController.h" @interface Yytwoviewcontroller () 12-( ibaction) Jump2three: (ID) sender;13 @end15 @implementation YYTwoViewController17 18//Jump to third sub-controller-(Ibaction) Jump2three: (ID) Sender {     //1. Create a third child controller     Yythreeviewcontroller *three=[[yythreeviewcontroller alloc]init ];22     //2. Adding a child controller to the navigation controller     [Self.navigationcontroller pushviewcontroller:three animated:yes];24     25}26 @end

Tip: As long as the current controller is a child controller of the navigation controller, you can get directly to the navigation controller where the current controller is located through the Self.navigationcontroller property

Project file structure and run effect:

A simple introduction to multiple controllers and navigation controllers

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.