UIBarButtonItem导航栏添加按钮
1 前言
UIBarButtonItem为导航栏按钮,在导航栏的左侧和右侧,他们具有许多种不同的形状和形式。
2 代码讲解
ZYViewController.m
[plain]
(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.");
}
- (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.
ZYUIBarButtonViewController.m:
[plain]
- (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.");
}
}
- (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.
UIBarButtonItem导航栏添加按钮的更多相关文章
- IOS 改变导航栏返回按钮的标题
IOS 改变导航栏返回按钮的标题 下午又找到了一个新的方法 这个方法不错 暂时没有发现异常的地方. 新写的App中需要使用UINavigationController对各个页面进行导航,但由于第一 ...
- 转载 iOS拦截导航栏返回按钮事件的正确方式
原文链接:http://www.jianshu.com/p/25fd027916fa 当我们使用了系统的导航栏时,默认点击返回按钮是 pop 回上一个界面.但是在有时候,我们需要在点击导航栏的返回按钮 ...
- iOS拦截导航栏返回按钮事件的正确方式
当我们使用了系统的导航栏时,默认点击返回按钮是 pop 回上一个界面.但是在有时候,我们需要在点击导航栏的返回按钮时不一定要 pop 回上一界面,比如一个视频播放界面,进入横屏后,默认点击返回按钮仍然 ...
- iOS拦截导航栏返回按钮事件的正确方式(二)
当我们使用了系统的导航栏时,默认点击返回按钮是 pop 回上一个界面.但是在有时候,我们需要在点击导航栏的返回按钮时不一定要 pop 回上一界面,比如一个视频播放界面,进入横屏后,默认点击返回按钮仍然 ...
- iOS不得姐项目--appearance的妙用,再一次设置导航栏返回按钮,导航栏左右按钮的封装(巧用分类)
一.UI_APPEARANCE_SELECTOR 彩票项目中appearance的用法一直没有搞明白,这次通过第二个项目中老师的讲解,更深一层次的了解到了很多关于appearance的作用以及使用方法 ...
- ios 修改导航栏返回按钮的图片
修改导航栏返回按钮的图片 方法1: [UINavigationBar appearance].backIndicatorTransitionMaskImage = [UIImage imageName ...
- Swift - 重写导航栏返回按钮
// 重写导航栏返回按钮方法 func configBackBtn() -> Void { // 返回按钮 let backButton = UIButton(type: .custom) // ...
- 解决 ios7.0 以后自定义导航栏左边按钮靠右的问题
解决 ios7.0 以后自定义导航栏左边按钮靠右的问题 www.111cn.net 编辑:edit02_lz 来源:转载 最近开发了一个ios的app,在ios7.0+出现自定义导航栏左边按钮出现靠右 ...
- Swift - 修改导航栏“返回”按钮文字,图标
Swift - 修改导航栏“返回”按钮文字,图标 2015-11-27 09:13发布:hangge浏览:4037 项目中常常会使用 UINavigationController 对各个页面进行导 ...
随机推荐
- Android App资源的查找过程分析
Android资源管理框架实际就是由AssetManager和Resources两个类来实现的.其中,Resources类可以根据ID来查找资源,而AssetManager类根据文件名来查找资源.事实 ...
- LinkedHasMap实现原理
转载:http://blog.csdn.net/luanlouis/article/details/43017071 Map作为键值对Entry<K,V>的的容器,对其内部 键值对Entr ...
- SQL操作(增删改查)
1.一些重要的SQL命令: SELECT - 从数据库中提取数据UPDATE - 更新数据库中的数据DELETE - 从数据库中删除数据INSERT INTO - 向数据库中插入新数据CREATE D ...
- uva 1400 - "Ray, Pass me the dishes!"
又是一道线段树区间更新的题: #include<cstdio> #include<algorithm> #include<cstring> #define ll l ...
- hdu 4714
一个树形dp的题,又是一个涉及不深的领域 = =: 不过在网上看到了大神用很巧的思路解决了这个题: 大神的思路就是: 从树的底部往上看:如果一棵子树拥有两个及以上的叶子节点,可以将这棵子树与大树分离 ...
- 找到个好的讲PYTHON FILE IO的文档,收藏
现在我感觉快入门了哈, 这两天,可以用PYTHON写一点自己想要实现的东东了. 但文件,IO,编码,邮件,始终有点续不完全. 这个文档,我看行.. http://www.dabeaz.com/pyth ...
- Android的Handler总结
一.Handler的定义: 主要接受子线程发送的数据, 并用此数据配合主线程更新UI. 解释: 当应用程序启动时,Android首先会开启一个主线程 (也就是U ...
- 被忽视的eMMC——A1 SD Bench闪存测试
一直以来,大家对手机的配置方面都比较关注CPU和GPU的架构.频率.核心数等,却经常忽略了手机闪存的速度.实际上手机的闪存素质对手机日常操作的响应.载入速度同样起到举足轻重的影响,今天给大家介绍的则是 ...
- MySQL查询原理及其慢查询优化案例分享(转)
MySQL凭借着出色的性能.低廉的成本.丰富的资源,已经成为绝大多数互联网公司的首选关系型数据库.虽然性能出色,但所谓“好马配好鞍”,如何能够更 好的使用它,已经成为开发工程师的必修课,我们经常会从职 ...
- 实现ImageView中两张图片重叠显示
第一种XML配置 使用layer-list标签 <layer-list xmlns:android="http://schemas.android.com/apk/res/androi ...