The file directory is as follows: basic navigation order: Root-> first-> second-> third. The firstviewcontroller serves as the rootview of the navigation stack.
1. Create navigation.
If you want to directly use the navigation as the following view at the beginning of the project, use rootviewcontroller. put the nav attribute in the H file into appdelegate. h, and then put the rootviewcontroller. copy the action code in the M file to appdelegate. in the didfinishlaunchingwitexceptions method in m. window. rootviewcontroller: Set the uinavigationcontroller type attribute nav.
In the rootviewcontroller. h file
1 #import <UIKit/UIKit.h> 2 @class FirstViewController; 3 4 @interface RootViewController : UIViewController 5 6 @property (strong, nonatomic) UINavigationController *nav; 7 8 - (IBAction)btnClick:(UIButton *)sender; 9 10 @end
In the rootviewcontroller. M file, choose any custom action:
1-(ibaction) btnclick :( uibutton *) sender {2 3 // create a viewcontroller 4 firstviewcontroller * fristview = [[[firstviewcontroller alloc] init] autorelease]; 5 6 7 // initialize uinavigationcontroller (method 1) 8 self. NAV = [[[uinavigationcontroller alloc] initwithrootviewcontroller: fristview] autorelease]; 9 10 11 // initialize uinavigationcontroller (method 2) 12 // self. NAV = [[[uinavigationcontroller alloc] init] autorelease]; 13 // [self. NAV pushviewcontroller: fristview animated: Yes]; 14 15 16 // initialization uinavigationcontroller (method 3, failed, XIB file loading failed, unknown reason) 17 // self. NAV = [[[uinavigationcontroller alloc] initwithnibname: @ "firstviewcontroller" Bundle: Nil] autorelease]; 18 19 20 // jump to the firstview page 21 [self presentviewcontroller: Self. NAV animated: Yes completion: Nil]; 22 23 24 // This method is generally used to add some widgets to the view, such as button label textfield, not suitable for page Jump 25 // [self. view addsubview: Self. nav. view]; 26 27 28}
2. Set the navigationitem attribute and toolbar and toolbaritem of viewcontroller.
(1) Navigation attribute and toolbaritem attribute of firstviewcontroller. The code is in the viewdidload method of firstviewcontroller. M.
Each viewcontroller has a navigationitem attribute (valid only when it is loaded into the navigation stack). The navigationitem attribute has five more attributes: leftbarbuttonitem, rightbarbuttonitem, and backbarbuttonitem of the uibarbuttonitem type, nsstring type title (or titleview of viewcontroller attribute) and prompt.
1-(void) viewdidload 2 {3 [Super viewdidload]; 4 // do any additional setup after loading the view from Its nib. 5 6 // The following two statements have the same effect, but it seems that navigationitem is set. title does not work. 7. Self. navigationitem. title = @ "home page 123"; 8 self. title = @ "Homepage"; 9 10 // Add more lines to the title, but this will cause the width of the navigation bar to increase by 11 // self. navigationitem. prompt = @ "prompt"; 12 13 // set rightbarbuttonitem14 uibarbuttonitem * rightbar = [[[uibarbuttonitem alloc] Destination: Custom target: Self action: @ selector (btnclick :)] autorelease]; 15 16 self. navigationitem. rightbarbuttonitem = rightbar; 17 18 // Modify the title of backbarbuttonitem. this backbarbutton is 19 on the left of the tabbar of the view displayed in the next push. // action can be set to nil. This is the default action, that is, the view20 self is returned. navigationitem. backbarbuttonitem = [[[uibarbuttonitem alloc] initwithtitle: @ "back" style: uibarbuttonitemstyleplain target: Self action: Nil] autorelease]; 21 22 23 24 // set whether the toolbar is visible (it takes effect for the view in the entire navigation stack and plays a global role) 25 self. navigationcontroller. toolbarhidden = no; 26 27 uibarbuttonitem * Three = [[uibarbuttonitem alloc] failed: Invalid target: Nil action: Nil]; 28 uibarbuttonitem * Four = [[uibarbuttonitem alloc] failed: uibarbuttonsystemitemedit target: Nil action: Nil]; 29 uibarbuttonitem * flexitem = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem: Define target: Nil action: Nil]; 30 31 // The toolbar is shared by the views in the entire navigation stack, but the items on the toolbar is 32 for each view independently. // currently, only the toolbaritem of the current view is set, toolbaritme of other views is irrelevant. 33 [self settoolbaritems: [nsarray arraywithobjects: flexitem, three, flexitem, four, flexitem, nil]; 34 35 [three release]; 36 [four release]; 37 [flexitem release]; 38 39 40}
The page effect is as follows:
(2) navigationitem and toolbaritem of secondviewcontroller
The code is in the viewdidload method.
1-(void) viewdidload 2 {3 [Super viewdidload]; 4 // do any additional setup after loading the view from Its nib. 5 6 // set the Left and Right barbutton 7 uibarbuttonitem * rightbar = [[[uibarbuttonitem alloc] Destination: uibarbuttonsystemitemaction target: Self action: @ selector (gotothird :)] uibarbuttonitem]; 8 9 uibarbuttonitem * leftbar = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem: uibarbuttonsystemitemaction target: Self action: @ selector (gotothird :)] orautelease]; 10 11 self. navigationitem. rightbarbuttonitem = rightbar; 12 self. navigationitem. leftbarbuttonitem = leftbar; 13 14 // The current view is not a rootview in the navigation stack. Therefore, when you push this view to the navigation stack, backbarbutton15 will be displayed on the left of the tabbar. // If leftbarbutton is set for the current view, the backbarbutton will be overwritten. to display the two at the same time, the following settings are required: 16 self. navigationitem. leftitemssupplementbackbutton = yes; 17 18 19 20 // set the toolbar of the current view to be visible. because it is possible to navigate from an invisible view of a toolbar to the current view21 self. navigationcontroller. toolbarhidden = no; 22 23}
The effect is as follows:
(3) navigationitem and toolbaritem of thirdviewcontroller
1-(void) viewdidload 2 {3 [Super viewdidload]; 4 self. title = @ "Page 3"; 5 // do any additional setup after loading the view from Its nib. 6 7 // hide the toolbar 8 self of the current view. navigationcontroller. toolbarhidden = yes; 9 10 // set toolbar11 uibarbuttonitem * rightbar = [[[uibarbuttonitem alloc] initwithbarbuttonsystemitem: Custom target: Self action: @ selector (gotorootview)] autorelease]; 12 13 self. navigationitem. rightbarbuttonitem = rightbar; 14 15 // nsarray * array = @ [@ "", @ "steak"]; 16 17 // set titleview, it will overwrite title18 uisegmentedcontrol * segment = [[uisegmentedcontrol alloc] initwithitems: @ [@ "", @ ""] autorelment]; 19 segment. segmentedcontrolstyle = uisegmentedcontrolsegmentcenter; 20 21 self. navigationitem. titleview = segment; 22}
The effect is as follows:
The Style and Effect of barbuttonitem are as follows: