navigationcontroller

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

一、程序框架

1、程序结构

2、storyboard

一个navigation controller 仅保留最基础部分,其他删除

根视图设置为view controller:

另外两个视图:

二、主要代码

1、ViewController.m中的主要代码

1)- (void)viewDidLoad方法

- (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)导航条左侧按钮响应函数

-(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];

}

2、VCSecondView.m中的主要代码

1)- (void)viewDidLoad

- (void)viewDidLoad

{

[super viewDidLoad];//自定义在导航条显示的对第二个视图对描述;//UINavigationController的title可以用别的view替代,比如用UIButton UILable等,此处使用uibuttonUIButton *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];

}

2、VCThirdView.m中的主要代码

均是默认代码

三、运行测试

1、初始界面

2、单击根视图左侧按钮

3、单击根视图右侧按钮

4、单击第二个视图右侧按钮

四、另外两个功能:不常用

在第二个视图中操作

1、分割功能: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]) {case0:

{

UIAlertView*alter = [[UIAlertView alloc] initWithTitle:@"提示"message:@"你点击了分割a"delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil, nil];

[alter show];

}

break;case1:

{

UIAlertView*alter = [[UIAlertView alloc] initWithTitle:@"提示"message:@"你点击了分割b"delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil, nil];

[alter show];

}

break;default:

break;

}

}

3)运行结果:

2、动态加载自定义的toolbar

1)VCSecondView.h

#import<UIKit/UIKit.h>@interfaceVCSecondView : 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)运行结果:切换到第三个视图过程中右上角有黑色阴影

五、其他

导航栏按钮图标类型:

参考:http://blog.csdn.net/totogo2010/article/details/7681879

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航