IOS开发(10)UIBarButtonItem导航栏添加按钮
来源:互联网 发布:记忆力训练软件 编辑:程序博客网 时间:2024/05/21 14:53
1 前言
UIBarButtonItem为导航栏按钮,在导航栏的左侧和右侧,他们具有许多种不同的形状和形式。
2 代码讲解
ZYViewController.m
- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; self.title = @"First"; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(perFormAdd:)];//为导航栏添加右侧按钮 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(perFormAdd:)];//为导航栏左侧添加系统自定义按钮}-(void)perFormAdd:(id)paramSender{ NSLog(@"Action method got called.");}运行结果:
当点击左边和右边的按钮的时候,控制台显示:
2013-04-23 21:40:58.982 UIBarButtonItemTest[660:c07] Action method got called.
2013-04-23 21:41:02.598 UIBarButtonItemTest[660:c07] Action method got called.
- (void)viewDidLoad{ [super viewDidLoad];// Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor]; self.title = @"Second"; UISwitch *simpleSwitch = [[UISwitch alloc] init];//实例化一个选择开关 simpleSwitch.on = YES;//开关设置为开启状态 [simpleSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];//添加事件 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:simpleSwitch];//将开关控件赋给导航栏右按钮}-(void)switchChanged:(UISwitch *)paramSender{ if ([paramSender isOn]) {//如果开关状态为开启 NSLog(@"Switch is on."); }else{ NSLog(@"Switch is off."); }}
运行结果:
当拨动开关控制台显示:
2013-04-23 21:46:46.692 UIBarButtonItemTest[727:c07] Switch is off.
2013-04-23 21:46:47.493 UIBarButtonItemTest[727:c07] Switch is on.
3 结语
以上就是导航栏按钮的实例,希望对大家有所帮助。
Demo实例下载地址:http://download.csdn.net/detail/u010013695/5292886
- IOS开发(10)UIBarButtonItem导航栏添加按钮
- (iOS开发)导航栏navigationBar中间添加按钮
- IOS开发UIBarButtonItem-添加自定义Left或者Right按钮
- IOS-9-UIBarButtonItem导航条-添加自定义Left或者Right按钮
- 自定义导航按钮UIBarButtonItem
- 自定义导航按钮UIBarButtonItem
- 自定义导航按钮UIBarButtonItem
- 自定义导航按钮UIBarButtonItem
- 导航栏中添加多个UIBarButtonItem
- iOS之自定义导航按钮UIBarButtonItem的样式
- ios导航栏添加多个按钮
- ios----导航栏添加多个按钮
- xamarin.iOS 导航栏添加按钮
- IOS开发:导航栏添加多个按钮以及在左边添加logo
- UIBarButtonItem导航条按钮封装
- ios导航条添加按钮
- ios导航条添加按钮
- iOS添加左右导航按钮
- 部分索引
- 迈向高端软件开发的数学必经之路
- Android高手进阶教程(五)之----Android 中LayoutInflater的使用!
- 静态初始化块和实例化块
- ios NSThread多线程
- IOS开发(10)UIBarButtonItem导航栏添加按钮
- 获取手机中安装的所有应用程序
- hdu3339 spfa()+dp 如果让我说:我只能说,实力决定一切
- iOS多线程编程之NSThread的使用
- jquery复习
- git diff
- 第六周项目6
- 在红黑树的基础上实现区间树的创建,左旋,右旋,插入和查找
- svn1