你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> IOS開發UIBarButtonItem導航欄添加按鈕

IOS開發UIBarButtonItem導航欄添加按鈕

編輯:關於IOS
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."); 
} 

- (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:


- (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. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved