First, some properties and basic use of the navigation controller
1. Four ways to add the controller to the navigation controller
(1)
1. Create a navigation controller
Uinavigationcontroller *nav=[[uinavigationcontrolleralloc]init];
2. Set the navigation controller as the root view of window
Self.window.rootviewcontroller=nav;
3. Add
Yyoneviewcontroller *one = [[Yyoneviewcontroller alloc] init];
[Nav Pushviewcontroller:one Animated:yes];
(2)
1. Create a navigation controller
Uinavigationcontroller *nav=[[uinavigationcontrolleralloc]init];
2. Set the navigation controller as the root view of window
Self.window.rootviewcontroller=nav;
3. Add
Yyoneviewcontroller *one = [[Yyoneviewcontroller alloc] init];
[Nav Addchildviewcontroller:one];
(3)
1. Create a navigation controller
Uinavigationcontroller *nav=[[uinavigationcontrolleralloc]init];
2. Set the navigation controller as the root view of window
Self.window.rootviewcontroller=nav;
3. Add
Yyoneviewcontroller *one = [[Yyoneviewcontroller alloc] init];
[Email protected] [one]; (added to the stack of the navigation controller)
The navigation controller has two properties Viewcontrollers and childviewcontrollers, the array type, is specifically used to install the sub-controller stack
Description: nav.viewcontrollers;== nav.childviewcontrollers; Note that the property is read-only and therefore cannot be written as follows. Nav.childviewcontrollers = @[one];
(4) The most common method
Yyoneviewcontroller *one=[[yyoneviewcontroller Alloc]init];
Uinavigationcontroller *nav=[[uinavigationcontroller Alloc]initwithrootviewcontroller:one];
2. The title of the current sub-controller interface navigation bar and the settings that correspond to the returned caption
[Email protected] "first interface";
self.navigationitem.backbarbuttonitem=[[uibarbuttonitemalloc]initwithtitle:@ "return one" style: Uibarbuttonitemstyleplain Target:nilaction:nil];
3. Add a button to the navigation bar
Description: You can add one, or you can add multiple (arrays)
Add a button to the left of the navigation bar (a button that adds a camera icon) that will cover the back
Self.navigationitem.leftbarbuttonitem=[[uibarbuttonitem Alloc]initwithbarbuttonsystemitem: Uibarbuttonsystemitemcamera Target:nil Action:nil];
4. Interface Jump
Jump to the second interface (currently a third, remove the controller at the top of the current stack) [Self.navigationControllerpopViewControllerAnimated:YES];
Remove all controllers except the stack base controller [Self.navigationControllerpopToRootViewControllerAnimated:YES];
Whenever a controller in the stack is passed in, it jumps to the specified controller [self.navigationcontroller poptoviewcontroller:<# (Uiviewcontroller *) #> animated: <# (BOOL) #>];
Second, code example
YYAPPDELEGATE.M file
1//2//YYAPPDELEGATE.M 3//01-Navigation Controller Use 1 4//5//Created by Apple on 14-6-4. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYAppDelegate.h" #import "YYOneViewController.h" one @implementation YYAppDelegate13 14//The application starts up Call-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) LAUNCHOPTIONS16 {Self.window = [[UIWindow alloc] Initwithframe:[[uiscreen mainscreen] bounds]];18 self.window.backgroundColor = [Uicolor whitecolor];19 20 21//3. Adding a sub-controller to the navigation Controller 22//The first is also the most commonly used one.//Yyoneviewcontroller *one=[[yyonev Iewcontroller Alloc]init];//Uinavigationcontroller *nav=[[uinavigationcontroller Alloc]initwithrootviewcontroller:one]; 25 26//1. Create a navigation controller Uinavigationcontroller *nav=[[uinavigationcontroller alloc]init];28//2. Set the navigation controller to win Dow Root View self.window.rootviewcontroller=nav;30 31//second Yyoneviewcontroller *one = [[Yyoneviewcontroll Er alloc] init];33 [nav pushviewcontroller:one animated:yes];34 35//third type//[Nav Addchildviewcontroller: ONE];37//fourth (added to the navigation controller stack)//[Email protected][one];39 40/////nav.viewcontrollers;== NAV.CHILDVIEWCONTROLLERS;42//Note that the property is read-only and therefore cannot be written as follows//nav.childviewcontrollers = @[one];44 45 46 [Self.window makekeyandvisible];47 return yes;48}49 @end
YYONEVIEWCONTROLLER.M file
1//2//YYONEVIEWCONTROLLER.M 3//01-Navigation Controller Use 1 4//5//Created by Apple on 14-6-4.6//Copyright (c) 2014 Year Itcase. All rights reserved. 7//8 9 #import "YYOneViewController.h" #import "YYTwoViewController.h" @interface Yyoneviewcontroller () 13/**14 Jump to the second interface */16-(ibaction) Jump2two: (ID) sender;17 @end19 @implementation YYOneViewController21-(ibaction ) Jump2two: (ID) Sender {24//1. Creating a second sub-controller Yytwoviewcontroller *two=[[yytwoviewcontroller alloc]init];26 27 2. Add the controller to the navigation Controller 28//Is there any way to get the navigation controller? 29//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:two animated : yes];31}32-(void) viewDidLoad34 {[Super viewdidload];36//control the contents of the navigation bar corresponding to the current controller [email protected] " The first interface "; 38//Modify the contents of the Back button display Self.navigationitem.backbarbuttonitem=[[uibarbuttonitem alloc]initwithtitle:@" return one "style : Uibarbuttonitemstyleplain target:nil action:nil];40}41 @end
YYTWOVIEWCONTROLLER.M file
1//2//YYTWOVIEWCONTROLLER.M 3//01-Navigation Controller Use 1 4//5//Created by Apple on 14-6-4. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYTwoViewController.h" #import "YYThreeViewController.h" @interface Yytwoviewcontroller (IBA ction) Jump2three: (ID) sender;13 @end15 @implementation YYTwoViewController17 18//Jump to third sub-controller-(Ibaction) Jump2three: (ID) Sender {20//1. Create a third child controller Yythreeviewcontroller *three=[[yythreeviewcontroller alloc]init];22 2. Add the child controller to the navigation controller. [Self.navigationcontroller pushviewcontroller:three animated:yes];24}25-(void) VIEWDIDLOAD27 [Super viewdidload];29//Add button to navigation bar 30//Add button to the left of navigation bar (button to add a camera icon), will cover back//Self.navigationi Tem.leftbarbuttonitem=[[uibarbuttonitem Alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemcamera Target:nil ACTION:NIL];32 33//Add multiple buttons to the right of the navigation bar 34//Create two buttons Uibarbuttonitem *a=[[uibarbuttonitem Alloc]initwithbarbutto Nsystemitem:uibarbuttonsystemiteMstop target:nil action:nil];36 uibarbuttonitem *b=[[uibarbuttonitem alloc]initwithbarbuttonsystemitem: Uibarbuttonsystemitemreply target:nil action:nil];37 uibarbuttonitem *c=[[uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemcamera Target:nil action:nil];38 [email protected][a,b,c]; 39 40//Set the return of the corresponding navigation bar (the return of the third Interface navigation bar) Self.navigationitem.backbarbuttonitem=[[uibarbuttonitem Alloc]initwithtitle : @ "Back" style:uibarbuttonitemstylebordered target:nil action:nil];42}43 @end
YYTHREEVIEWCONTROLLER.M file
1//2//YYTHREEVIEWCONTROLLER.M 3//01-Navigation Controller Use 1 4//5//Created by Apple on 14-6-4.6//Copyright (c) 201 4 years itcase. All rights reserved. 7//8 9 #import "YYThreeViewController.h" #import "YYTwoViewController.h" @interface Yythreeviewcontroller () 13/ /Return to the Second Controller page (ibaction) Jump2two: (ID) sender;15//Return to the first Controller page-(ibaction) Jump2root: (ID) sender;17 @end19 20 @ Implementation YYThreeViewController21-(Ibaction) Jump2two: (ID) Sender {24//jump to the second interface (removing the controller at the top of the current stack) [self.na Vigationcontroller popviewcontrolleranimated:yes];26}27-(ibaction) Jump2root: (ID) Sender {29//Remove all controllers except the stack base controller 3 0 [Self.navigationcontroller poptorootviewcontrolleranimated:yes];31 32//If one of the controllers in the stack is passed in, it jumps to the specified controller 33/ /cannot do this, not added to the navigation controller Yytwoviewcontroller *two = [[Yytwoviewcontroller alloc] init];34//[self.navigationcontroller PopToVie wcontroller:<# (Uiviewcontroller *) #> animated:<# (BOOL) #>];35}36 @end
Implementation results:
Third, the navigation controller through the stack to manage the sub-controller
:
Description
The navigation controller manages the sub-controller in the form of a stack (advanced back-out)
The view that appears on the navigation controller is always the view of the top controller (that is, the view that is currently displayed), such as the current third interface Click to return, will destroy the current controller (move out of the stack, and its view will be destroyed, and then show the second controller and its view)
A navigation controller has only one navigation bar, which means that all self-controllers have a single navigation bar.
Iv. Supplementary
In the proxy method, print all the child controls below the current window and save them through an XML file, as shown below.
The application gets the focus (which represents the ability to interact with the user)-(void) Applicationdidbecomeactive: (uiapplication *) application{NSLog (@ " Applicationdidbecomeactive "); Uinavigationcontroller *nav = (Uinavigationcontroller *) Self.window.rootViewController; Uinavigationbar *bar = nav.navigationbar;//NSLog (@ "%@", Nsstringfromcgrect (Bar.frame)); NSString *str = [self DigView:self.window]; [Str writetofile:@ "/users/apple/desktop/ios6.xml" atomically:yes]; }/** * Returns all hierarchies of incoming VEIW * * @param view needs to get the hierarchy of view * * @return string */-(NSString *) Digview: (UIView *) view{if ([VI EW Iskindofclass:[uitableviewcell class]]) return @ ""; 1. Initialize nsmutablestring *xml = [nsmutablestring string]; 2. label start [XML appendformat:@ "<%@ frame=\"%@\ "", View.class, Nsstringfromcgrect (View.frame)]; if (! Cgpointequaltopoint (View.bounds.origin, Cgpointzero)) {[XML appendformat:@ "bounds=\"%@\ "", Nsstringfromcgrect (Vie W.bounds)]; } if ([View Iskindofclass:[uiscrollview class]]) { Uiscrollview *scroll = (Uiscrollview *) view; if (! Uiedgeinsetsequaltoedgeinsets (Uiedgeinsetszero, Scroll.contentinset)) {[XML appendformat:@ "contentInset=\"%@\ "", Nsstringfromuiedgeinsets (Scroll.contentinset)]; }}//3. Determine if you want to end if (View.subviews.count = = 0) {[XML appendstring:@ "/>"]; return XML; } else {[XML appendstring:@ >]; }//4. Traverse all child controls for (UIView *child in view.subviews) {NSString *childxml = [self digview:child]; [XML Appendstring:childxml]; }//5. End of Tag [XML appendformat:@ "</%@>", View.class]; return XML;}
iOS Development UI chapter-Navigation controller properties and basic usage