iOS Development UI chapter-Navigation controller properties and basic usage
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)
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
9 #import "YYAppDelegate.h" #import "YYOneViewController.h" one @implementation YYAppDelegate13 14//The application is started and will call 15- (BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) LAUNCHOPTIONS16 {17 Self.window = [[UIWindow alloc] Initwithframe:[[uiscreen mainscreen] bounds]];18 self.window.backgroundColor = [Uicolo R whitecolor];19 20 21//3. Adding a sub-controller to the navigation Controller 22//The first and most commonly used one is the//Yyoneviewcontroller *ONE=[[YYONEVIEWCONTR Oller alloc]init];24//Uinavigationcontroller *nav=[[uinavigationcontroller Alloc]initwithrootviewcontroller:one]; 25 26//1. Create a navigation controller Uinavigationcontroller *nav=[[uinavigationcontroller alloc]init];28//2. Setting the navigation controller to wind ow root View self.window.rootviewcontroller=nav;30 31//second Yyoneviewcontroller *one = [[Yyoneviewcontrolle R Alloc] init];33 [nav pushviewcontroller:one animated:yes];34 35//Third Kind//[Nav Addchildviewcontroller:o NE];37//fourth (added to the stack of navigation controllers) 38//[email protected][one];39 40//NAV controller stack//nav.viewcontrollers;== nav.childviewcontrollers;42 Note that this property is read-only and therefore cannot be written as follows//nav.childviewcontrollers = @[one];44 [Self.window makekeyandvisib le];47 return yes;48}49 @end
YYONEVIEWCONTROLLER.M file
9 #import "YYOneViewController.h" #import "YYTwoViewController.h" @interface Yyoneviewcontroller ()/**14 Jump to the second interface */16-(ibaction) Jump2two: (ID) sender;17 @end19 @implementation YYOneViewController21-(ibacti ON) Jump2two: (ID) Sender { //1. Create a second sub-controller yytwoviewcontroller *two=[[yytwoviewcontroller alloc]init];26 //2. The handle controller is added to the navigation controller. How can I 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: Two animated:yes];31}32-(void) viewDidLoad34 { [super viewdidload];36 //Control the contents of the navigation bar corresponding to the current controller [ Email protected] "first interface"; edit//Modify the contents of the return button Self.navigationitem.backbarbuttonitem=[[uibarbuttonitem alloc]initwithtitle:@ "Return one" Style:uibarbuttonitemstyleplain target:nil action:nil];40}41 @end
YYTWOVIEWCONTROLLER.M file
9 #import "YYTwoViewController.h" #import "YYThreeViewController.h" @interface Yytwoviewcontroller ()-(ibaction ) Jump2three: (ID) sender;13 @end15 @implementation YYTwoViewController17 18//Jump to the third sub-controller (ibaction) Jump2three: ( ID) Sender {20//1. Create a third child controller Yythreeviewcontroller *three=[[yythreeviewcontroller alloc]init];22//2. Add a child controller to Nav controller [Self.navigationcontroller pushviewcontroller:three animated:yes];24}25-(void) VIEWDIDLOAD27 {[sup ER viewdidload];29//Add a button to the navigation bar 30//Add a button to the left of the navigation bar (a button to add a camera icon), will cover the return to//Self.navigationitem.leftbarbuttonitem=[[ui Barbuttonitem Alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemcamera Target:nil action:nil];32 33//For navigation bar on the right Add multiple buttons 34//Create two buttons Uibarbuttonitem *a=[[uibarbuttonitem alloc]initwithbarbuttonsystemitem:uibarbuttonsystemitemst Op 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) x self.navigationItem.ba Ckbarbuttonitem=[[uibarbuttonitem alloc]initwithtitle:@ "Back" style:uibarbuttonitemstylebordered target:nil action: nil];42}43 @end
YYTHREEVIEWCONTROLLER.M file
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 { x//Jump to the second interface (remove the controller at the top of the current stack) [ Self.navigationcontroller popviewcontrolleranimated:yes];26}27-(ibaction) Jump2root: (ID) Sender { // Remove all controllers except the stack bottom controller. [self.navigationcontroller poptorootviewcontrolleranimated:yes];31 // As soon as one of the controllers in the stack is passed in, it jumps to the specified controller. //cannot do so, not added to the navigation controller Yytwoviewcontroller *two = [[Yytwoviewcontroller alloc] init]; //[self.navigationcontroller poptoviewcontroller:<# (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)
View that appears on the navigation controller is always the stack top controller
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;}
Note: In iOS7 and previous versions, the individual controls, including the sub-controller interface frame, are different.
Navigation controller properties and basic usage