1、程序框架框架
一、程序结构函数
二、storyboard工具
一个navigation controller 仅保留最基础部分,其余删除
测试
根视图设置为view controller:flex
另外两个视图:ui
2、主要代码spa
一、ViewController.m中的主要代码.net
1)- (void)viewDidLoad方法code
- (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; //自定义从下一个视图左上角,“返回”根视图的按钮 UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"后退" style:UIBarButtonItemStyleDone target:nil action:nil]; self.navigationItem.backBarButtonItem=backButton; //自定义根视图下方的工具条,可进一步绑定各个按钮的处理函数;只对当前视图有效,切换到其余视图时为空白 [self.navigationController setToolbarHidden:NO animated:YES]; 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]]; }
2)导航条左侧按钮响应函数orm
-(void)selectLeftAction:(id)sender { UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了导航栏左按钮" delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil]; [alter show]; }
3)导航条右侧按钮响应函数
-(void)selectRightAction:(id)sender { VCSecondView *vcSecondView; vcSecondView =[self.storyboard instantiateViewControllerWithIdentifier: @"second_view"]; [self.navigationController pushViewController:vcSecondView animated:YES]; }
二、VCSecondView.m中的主要代码
1)- (void)viewDidLoad
- (void)viewDidLoad { [super viewDidLoad]; //自定义在导航条显示的对第二个视图对描述; //UINavigationController的title能够用别的view替代,好比用UIButton UILable等,此处使用uibutton UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect]; [button setTitle: @"自定义title" forState: UIControlStateNormal]; [button sizeToFit]; self.navigationItem.titleView = button; //导航栏右侧按钮 UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(selectRightAction:)]; self.navigationItem.rightBarButtonItem = rightButton; //自定义从下一个视图左上角,“返回”本视图的按钮 UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"后退" style:UIBarButtonItemStyleDone target:nil action:nil]; self.navigationItem.backBarButtonItem=backButton; }
2)导航条右侧按钮响应函数
-(void)selectRightAction:(id)sender { VCThirdView *vcThirdView; vcThirdView =[self.storyboard instantiateViewControllerWithIdentifier: @"third_view"]; [self.navigationController pushViewController:vcThirdView animated:YES]; }
二、VCThirdView.m中的主要代码
均是默认代码
3、运行测试
一、初始界面
二、单击根视图左侧按钮
三、单击根视图右侧按钮
四、单击第二个视图右侧按钮
4、另外两个功能:不经常使用
在第二个视图中操做
一、分割功能:UISegmentedControl
1)- (void)viewDidLoad添加以下代码:
- (void)viewDidLoad { 。。。 NSArray *array = [NSArray arrayWithObjects:@"分割a",@"分割b", nil]; UISegmentedControl *segmentedController = [[UISegmentedControl alloc] initWithItems:array]; segmentedController.segmentedControlStyle = UISegmentedControlSegmentCenter; [segmentedController addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged]; self.navigationItem.titleView = segmentedController; 。。。 }
2)分割按钮响应函数:
-(void)segmentAction:(id)sender { switch ([sender selectedSegmentIndex]) { case 0: { UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了分割a" delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil]; [alter show]; } break; case 1: { UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了分割b" delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil]; [alter show]; } break; default: break; } }
3)运行结果:
二、动态加载自定义的toolbar
1)VCSecondView.h
#import <UIKit/UIKit.h> @interface VCSecondView : UIViewController { UIToolbar *toolBar; } @end
2)- (void)viewDidLoad中添加以下代码:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:nil/*@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];
3)运行结果:切换到第三个视图过程当中右上角有黑色阴影
5、其余
导航栏按钮图标类型: