1. How to use the Uinavigationcontroller navigation controller
Uinavigationcontroller can be translated as a navigation controller, which is often used in iOS.
Let's see how it's used:
The following figure shows the flow of the navigation controller. The leftmost is the root view, which slides into the screen when the user taps the general item, and the Auto-lock view slides into the screen when the user continues to tap the Auto-lock item. Accordingly, on object management, the navigation controller uses the navigation stack. The root view controller is at the bottom of the stack, and the next stack is the general view Controller and the Auto-lock view controller. You can call the pushviewcontrolleranimated: method to push the view controller into the top of the stack, or you can call the popviewcontrolleranimated: method to eject the view controller.
From Apple's official website.
2, the structure of the uinavigationcontroller composition
See, Uinavigationcontroller has Navigation bar, Navigation View, Navigation Toobar and other components.
Now let's create an example to see how to use the Uinavigationcontroller
3. Create a new project
Named Uinavigationcontrollerdemo, in order to better understand uinavigationcontroller, we choose the empty application template
4, Create a View Controller, named Rootviewcontroller: Select File--new--new File, the default check with the XIB for user interface.
Choose the correct location to create the complete, then the project three more files, respectively, RootViewController.h rootviewcontroller.m rootviewcontroller.xib file.
Open Rootviewcontroller.xib, add a button control, Change button to: Goto Secondview, prepare for jump
5. Open AppDelegate.hand add attributes to it:
[CPP]View Plaincopyprint?
- @property (Strong, nonatomic) Uinavigationcontroller *navcontroller;
@property (Strong, nonatomic) Uinavigationcontroller *navcontroller;
Add the following AppDelegate.h file code as follows:
[CPP]View Plaincopyprint?
- #import <UIKit/UIKit.h>
-
- @class viewcontroller;
-
- @interface AppDelegate : UIResponder < uiapplicationdelegate>
-
- @property (strong, nonatomic) uiwindow *window;
-
- @property ( strong, nonatomic) viewcontroller *viewcontroller;
-
- @property (strong, nonatomic) UINavigationController *navController;
-
- @end
#import <UIKit/UIKit.h> @class viewcontroller, @interface appdelegate:uiresponder <uiapplicationdelegate > @property (Strong, nonatomic) UIWindow *window; @property (Strong, nonatomic) Viewcontroller *viewcontroller;@ Property (Strong, Nonatomic) Uinavigationcontroller *navcontroller; @end
6. Create the Add Navcontroller,rootviewcontroller view in the Didfinishlaunchingwithoptions method of the APPDELEGATE.M file.
[CPP]View Plaincopyprint?
- -(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
- {
- Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];
- Rootviewcontroller *rootview = [[Rootviewcontroller alloc] init];
- Rootview.title = @"Root View";
- Self.navcontroller = [[Uinavigationcontroller alloc] init];
- [Self.navcontroller Pushviewcontroller:rootview Animated:yes];
- [Self.window AddSubview:self.navController.view];
- [Self.window makekeyandvisible];
- return YES;
- }
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{ Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]]; Rootviewcontroller *rootview = [[Rootviewcontroller alloc] init]; Rootview.title = @ "Root View"; Self.navcontroller = [[Uinavigationcontroller alloc] init]; [Self.navcontroller Pushviewcontroller:rootview Animated:yes]; [Self.window AddSubview:self.navController.view]; [Self.window makekeyandvisible]; return YES;}
Name the Rootview titie Root view to identify the direct switching relationship of the view. Add the Rootview to the Navcontroller view stack with Pushviewcontroller.
7. Now the root view is added to complete
Look at the effect:
‘
There's no navigation bar. Only title.
8. Add Uibarbuttonitem
Bar Buttonitem about Uibarbuttonitem. We add all the left and right.
Add the following code in the ROOTVIEWCONTROLLER.M:
[CPP]View Plaincopyprint?
- -(void) Viewdidload
- {
- [Super Viewdidload];
- Uibarbuttonitem *leftbutton = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemaction Target:self Action: @selector (selectleftaction:)];
- Self.navigationItem.leftBarButtonItem = LeftButton;
- Uibarbuttonitem *rightbutton = [[Uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemadd target: Self action: @selector (selectrightaction:)];
- Self.navigationItem.rightBarButtonItem = rightbutton;<p class="P1" >}</p>
-(void) viewdidload{ [Super Viewdidload]; Uibarbuttonitem *leftbutton = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemaction Target:self Action: @selector (selectleftaction:)]; Self.navigationItem.leftBarButtonItem = LeftButton; Uibarbuttonitem *rightbutton = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:uibarbuttonsystemitemadd Target:self Action: @selector (selectrightaction:)]; Self.navigationItem.rightBarButtonItem = Rightbutton;
}
This adds the Uibarbuttonitem, the effect is as follows:
Here are the highlights
Uibarbuttonitem *leftbutton = [[uibarbuttonitemalloc]initwithbarbuttonsystemitem: Uibarbuttonsystemitemactiontarget:selfAction:@selector(selectleftaction:)];
Uibarbuttonsystemitemaction style, this is the system comes with the button style, see, you do not have to experiment, you also know that you want to use the item, such as:
9. Implementation of events in response to Uibarbuttonitem
We are in action:@selector(selectleftaction:);
Action adds selectleftaction and Selectrightaction
Add code implementations in the ROOTVIEWCONTROLLER.M file:
[CPP]View Plaincopyprint?
- -(void) Selectleftaction: (ID) Sender
- {
- Uialertview *alter = [[Uialertview alloc] initwithtitle:@"hint" message:@"you clicked the navigation bar left button" Delegate:self cancelbuttontitle:@"OK" otherbuttontitles:nil, nil];
- [Alter show];
- }
- -(void) Selectrightaction: (ID) Sender
- {
- Uialertview *alter = [[Uialertview alloc] initwithtitle:@"hint" message:@"you clicked the Navigation bar right button" delegate:self cancelbuttontitle:@"OK" otherbuttontitles:nil, nil];
- [Alter show];
- }
-(void) Selectleftaction: (ID) sender{ uialertview *alter = [[Uialertview alloc] initwithtitle:@ "Prompt" message:@ " You clicked the navigation bar left button "Delegate:self cancelbuttontitle:@" OK "otherbuttontitles:nil, nil"; [Alter show];} -(void) Selectrightaction: (ID) sender{ uialertview *alter = [[Uialertview alloc] initwithtitle:@ "Prompt" message:@ " You clicked the Navigation bar right button "Delegate:self cancelbuttontitle:@" OK "otherbuttontitles:nil, nil"; [Alter show];}
So when you click on the left and right uibarbuttonitem, pop-up hints:
This article first to add Uibarbuttonitem, the next page to explain the jump and add Uisegmentedcontrol
http://blog.csdn.net/totogo2010/article/details/7681879