iOS Development UI article-a brief 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 @implementationyyappdelegate5 6-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions7 {8Self.window =[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds];9Self.window.backgroundColor =[Uicolor Whitecolor];Ten One //1. Create a navigation controller AUinavigationcontroller *nav=[[Uinavigationcontroller alloc]init]; - //2. Set the navigation controller as the root view of window -Self.window.rootviewcontroller=nav; the - - //3. Adding a child controller to the navigation controller - //Create some controllers +Uiviewcontroller *c1=[[Uiviewcontroller alloc]init]; - //Set C1 view color for this controller +C1.view.backgroundcolor=[Uicolor Redcolor]; A atUiviewcontroller *c2=[[Uiviewcontroller alloc]init]; -C2.view.backgroundcolor=[Uicolor Purplecolor]; - -Uiviewcontroller *c3=[[Uiviewcontroller alloc]init]; -C3.view.backgroundcolor=[Uicolor Browncolor]; - in //add these controllers to the navigation controller - [nav pushviewcontroller:c1 animated:yes]; to [nav pushviewcontroller:c2 animated:yes]; + [nav pushviewcontroller:c3 animated:yes]; - the [Self.window makekeyandvisible]; * returnYES; $}
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
1 //2 //YYAPPDELEGATE.M3 //01-Use of the navigation controller 14 //5 //Created by Apple on 14-6-4.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 #import "YYAppDelegate.h"Ten #import "YYOneViewController.h" One A @implementationyyappdelegate - --(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) Launchoptions the { -Self.window =[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; -Self.window.backgroundColor =[Uicolor Whitecolor]; - + //1. Create a navigation controller -Uinavigationcontroller *nav=[[Uinavigationcontroller alloc]init]; + //2. Set the navigation controller as the root view of window ASelf.window.rootviewcontroller=nav; at - - //3. Adding a child controller to the navigation controller -Yyoneviewcontroller *one=[[Yyoneviewcontroller alloc]init]; - [nav pushviewcontroller:one animated:yes]; - in [Self.window makekeyandvisible]; - returnYES; to + - // //Create some controllers the //Uiviewcontroller *c1=[[uiviewcontroller Alloc]init]; * // //Set C1 view color for this controller $ //C1.view.backgroundcolor=[uicolor Redcolor];Panax Notoginseng // - //Uiviewcontroller *c2=[[uiviewcontroller Alloc]init]; the //C2.view.backgroundcolor=[uicolor Purplecolor]; + // A //Uiviewcontroller *c3=[[uiviewcontroller Alloc]init]; the //C3.view.backgroundcolor=[uicolor Browncolor]; + // - ////Add these controllers to the navigation controller $ //[nav pushviewcontroller:c1 animated:yes]; $ //[nav pushviewcontroller:c2 animated:yes]; - //[nav pushviewcontroller:c3 animated:yes]; -}
Create three child control classes and their corresponding xib files
YYONEVIEWCONTROLLER.M file
1 //2 //YYONEVIEWCONTROLLER.M3 //01-Use of the navigation controller 14 //5 //Created by Apple on 14-6-4.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 #import "YYOneViewController.h"Ten #import "YYTwoViewController.h" One A @interfaceYyoneviewcontroller () - /** - jump to the second interface the */ --(Ibaction) Jump2two: (ID) sender; - - @end + - @implementationYyoneviewcontroller + A at-(Ibaction) Jump2two: (ID) Sender { - //1. Create a second sub-controller -Yytwoviewcontroller *two=[[Yytwoviewcontroller alloc]init]; - - //2. Add the controller to the navigation controller - //is there any way to get the navigation controller? in //as long as the current controller is a child controller of the navigation controller, it is possible to get directly to the navigation controller where the current controller is located - [Self.navigationcontroller pushviewcontroller:two animated:yes]; to } + @end
YYTWOVIEWCONTROLLER.M file
1 //2 //YYTWOVIEWCONTROLLER.M3 //01-Use of the navigation controller 14 //5 //Created by Apple on 14-6-4.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 #import "YYTwoViewController.h"Ten #import "YYThreeViewController.h" One @interfaceYytwoviewcontroller () A-(Ibaction) Jump2three: (ID) sender; - - @end the - @implementationYytwoviewcontroller - - //Jump to third sub-controller +-(Ibaction) Jump2three: (ID) Sender { - //1. Create a third child controller +Yythreeviewcontroller *three=[[Yythreeviewcontroller alloc]init]; A //2. Add the child controller to the navigation controller at [Self.navigationcontroller pushviewcontroller:three animated:yes]; - - } - @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:
iOS Development UI article-a brief introduction to multiple controllers and navigation controllers