IOS開發(10)UIBarButtonItem導覽列添加按鈕

來源:互聯網
上載者:User

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.

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.