IOSLearningUINavigationControllerDetails and usage(II)Page switching andSegmentedControllerNext, let's talk about the Navigation Toolbar.
1. Display Toolbar
Add the code in the-(void) viewDidLoad method of RootViewController. m so that Toobar is displayed.
[self.navigationController setToolbarHidden:NO animated:YES];
2. Add UIBarButtonItem on the ToolBar
Create several uibarbuttonitems and add them to the Toolbar as arrays.
UIBarButtonItem *one = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil]; UIBarButtonItem *two = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:nil action:nil]; UIBarButtonItem *three = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil]; UIBarButtonItem *four = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil]; UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; [self setToolbarItems:[NSArray arrayWithObjects:flexItem, one, flexItem, two, flexItem, three, flexItem, four, flexItem, nil]];
Effect:
Note: using [self. navigationController. toolbar setItems :( NSArray *) animated: <# (BOOL) #>] This method to add an item does not work. Now, when I add a Toolbar dynamically, this will take effect.
3. dynamically add Toolbar
We add a dynamic Toolbar in SecondView.
Add in SecondViewController. h
#import <UIKit/UIKit.h>@interface SecondViewController : UIViewController{ UIToolbar *toolBar;}@end
Add in SecondViewController. m
- (void)viewDidLoad{ [super viewDidLoad]; [self.navigationController setToolbarHidden:YES animated:YES]; UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(gotoThridView:)]; toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height - toolBar.frame.size.height - 44.0, self.view.frame.size.width, 44.0)]; [toolBar setBarStyle:UIBarStyleDefault]; toolBar.autoresizingMask = UIViewAutoresizingFlexibleTopMargin; [toolBar setItems:[NSArray arrayWithObject:addButton]]; [self.view addSubview:toolBar]; // Do any additional setup after loading the view from its nib.}
First, hide the Toobar displayed during RootView.
[Self. navigationController setToolbarHidden: YESanimated: YES]; then, the new Toolbar is added to the SecondView and an Item is set for Toobar.
[ToolBarsetItems: [NSArrayarrayWithObject: addButton];
BarButtonItem uses UIBarButtonSystemItemSearch. The effect is as follows:
4. Create a New ThridView and jump from SecondView
Commad + N create a ThridViewController,
This addButton jumps to ThridView
-(void)gotoThridView:(id)sender{ ThridViewController *thridView = [[ThridViewController alloc] init]; [self.navigationController pushViewController:thridView animated:YES]; thridView.title = @"Thrid View";}
Jump Second to Third:
At this point, the UINavigationController has been practicing almost.
Previous two articles:
IOSLearningUINavigationControllerExplanation and use (1) AddUIBarButtonItem
IOSLearningUINavigationControllerDetails and usage(II)Page switching andSegmentedController
Example code: https://github.com/schelling/YcDemo
Copyright Disclaimer: This article will be published at http://blog.csdn.net/totogo2010/, and you will be welcomed to enjoy the transfer and sharing. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you.