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导航栏添加按钮的更多相关文章

  1. IOS 改变导航栏返回按钮的标题

    IOS 改变导航栏返回按钮的标题   下午又找到了一个新的方法 这个方法不错 暂时没有发现异常的地方. 新写的App中需要使用UINavigationController对各个页面进行导航,但由于第一 ...

  2. 转载 iOS拦截导航栏返回按钮事件的正确方式

    原文链接:http://www.jianshu.com/p/25fd027916fa 当我们使用了系统的导航栏时,默认点击返回按钮是 pop 回上一个界面.但是在有时候,我们需要在点击导航栏的返回按钮 ...

  3. iOS拦截导航栏返回按钮事件的正确方式

    当我们使用了系统的导航栏时,默认点击返回按钮是 pop 回上一个界面.但是在有时候,我们需要在点击导航栏的返回按钮时不一定要 pop 回上一界面,比如一个视频播放界面,进入横屏后,默认点击返回按钮仍然 ...

  4. iOS拦截导航栏返回按钮事件的正确方式(二)

    当我们使用了系统的导航栏时,默认点击返回按钮是 pop 回上一个界面.但是在有时候,我们需要在点击导航栏的返回按钮时不一定要 pop 回上一界面,比如一个视频播放界面,进入横屏后,默认点击返回按钮仍然 ...

  5. iOS不得姐项目--appearance的妙用,再一次设置导航栏返回按钮,导航栏左右按钮的封装(巧用分类)

    一.UI_APPEARANCE_SELECTOR 彩票项目中appearance的用法一直没有搞明白,这次通过第二个项目中老师的讲解,更深一层次的了解到了很多关于appearance的作用以及使用方法 ...

  6. ios 修改导航栏返回按钮的图片

    修改导航栏返回按钮的图片 方法1: [UINavigationBar appearance].backIndicatorTransitionMaskImage = [UIImage imageName ...

  7. Swift - 重写导航栏返回按钮

    // 重写导航栏返回按钮方法 func configBackBtn() -> Void { // 返回按钮 let backButton = UIButton(type: .custom) // ...

  8. 解决 ios7.0 以后自定义导航栏左边按钮靠右的问题

    解决 ios7.0 以后自定义导航栏左边按钮靠右的问题 www.111cn.net 编辑:edit02_lz 来源:转载 最近开发了一个ios的app,在ios7.0+出现自定义导航栏左边按钮出现靠右 ...

  9. Swift - 修改导航栏“返回”按钮文字,图标

    Swift - 修改导航栏“返回”按钮文字,图标 2015-11-27 09:13发布:hangge浏览:4037   项目中常常会使用 UINavigationController 对各个页面进行导 ...

随机推荐

  1. ubuntu杂记

    安装ssh: sudo apt-get install openssh-server sudo /etc/init.d/ssh start 将主机中vmware8的网络改为自动获取ip,就可以ping ...

  2. 离开ACM了,总结一下

    写这篇博客,一如当初我对着电脑显示器,不知道从哪里下手才是,所以没准写着写着就出现了倒叙插叙补叙等充满语文功底的修辞手法,不过不会有45度的妩媚和忧伤. 像一位程序员所说:今天的努力是为了儿时吹过的牛 ...

  3. bzoj 1902: Zju2116 Christopher lucas定理 && 数位DP

    1902: Zju2116 Christopher Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 172  Solved: 67[Submit][Stat ...

  4. Word里插入表格不带左右边框

    插入表格后选中,然后开始-----段落------选择右下角的边框设置,选择无左右边框.

  5. 编写优质嵌入式C程序

    前言:这是一年前我为公司内部写的一个文档,旨在向年轻的嵌入式软件工程师们介绍如何在裸机环境下编写优质嵌入式C程序.感觉是有一定的参考价值,所以拿出来分享,抛砖引玉. 转载请注明出处:http://bl ...

  6. Cocos2d—X游戏开发之CCScrollView(滑动视图)(十二)

    CCScrollView在Cocos2d-X引擎中主要使用在图片尺寸远大于屏幕尺寸的时候使用. 总体来说,使用起来比较简单. 一个是CCScrollView控件本身,一个是CCScrollViewDe ...

  7. 【HDOJ】4183 Pahom on Water

    就是一个网络流.red结点容量为2,查看最大流量是否大于等于2.对于条件2,把边反向加入建图.条件1,边正向加入建图. /* 4183 */ #include <iostream> #in ...

  8. 【HDOJ】3560 Graph’s Cycle Component

    并查集的路径压缩. #include <stdio.h> #include <string.h> #define MAXNUM 100005 int deg[MAXNUM], ...

  9. Vijos_1792_摆花_(动态规划,多重集组合数)

    描述 https://vijos.org/p/1792 共n种花,第i种花有a[i]个,要摆m个,同一种花连续且花按照序号从小到大排,问共有多少种摆花方案.   描述 小明的花店新开张,为了吸引顾客, ...

  10. POJ_2987_Firing_(最大流+最大权闭合图)

    描述 http://poj.org/problem?id=2987 要炒员工鱿鱼,炒了一个人,他的下属一定被炒.给出每个人被炒后公司的收益(负值表示亏损),问怎样炒公司收益最大,以及这种方法炒了几个人 ...