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.

最新文章

  1. python征程2.0(python基础)
  2. CodeForces比赛总结表
  3. Git/Github + TortoiseGit 使用教程
  4. SQL Server索引调优系列
  5. JMS学习(三)JMS 消息结构之属性及消息体详解
  6. 理解PHP 依赖注入|Laravel IoC容器
  7. JavaScript解析json
  8. 浅析ASP.NET的状态保持
  9. JavaScript 中的事件设计
  10. C语言 linux环境基于socket的简易即时通信程序
  11. 第21章 策略模式(Strategy Pattern)
  12. 将svg文件化成字体图标的步骤
  13. Class "xxxxx"defined without specifying a base class
  14. Unity 多人网络连接
  15. Sallen-Key Active Butterworth Low Pass Filter Calculator
  16. [转载]linux下core文件设置与查看
  17. Django之WSGI 和MVC/MTV
  18. 【拓扑 字符串还原 + 线段树维护】奇洛金卡达(father)
  19. C++11 delete和default
  20. python之旅:python中range()和len()函数区别

热门文章

  1. #Leet Code# Best Time to Buy and Sell Stock
  2. scrapy使用爬取多个页面
  3. C++ 11 笔记 (四) : std::bind
  4. 【原创】Mvc学习笔记(1)
  5. Linux下实现定时器Timer的几种方法
  6. Spring 通过工厂方法(Factory Method)来配置bean
  7. Handler sendMessage 与 obtainMessage (sendToTarget)
  8. 【HDOJ】2732 Leapin' Lizards
  9. MySQL优化器join顺序
  10. 微软开放技术开发了适用于 Windows Azure 移动服务的开源 Android SDK