鍍金池/ 教程/ iOS/ iOS - Toolbar(工具欄)
iOS - Switches(切換/開(kāi)關(guān))
iOS - Labels(標(biāo)簽)
iOS - Table View(表格視圖)
IOS - 攝像頭管理
iOS - Text View(文本視圖)
IOS - 開(kāi)發(fā)環(huán)境配置
iOS教程
iOS - Twitter & Facebook
iOS - UI元素
iOS - iAd 整合
IOS - 應(yīng)用程序調(diào)試
iOS - Split View(分割視圖)
iOS - Status Bar(狀態(tài)欄)
iOS - Navigation Bar(導(dǎo)航欄)
iOS - Tab bar(標(biāo)簽欄)
IOS - 文件處理
IOS - 自動(dòng)布局
iOS - Image View(圖像視圖)
iOS - 應(yīng)用程序內(nèi)購(gòu)買(mǎi)
iOS - Pickers(選取器)
iOS - Delegates實(shí)例
iOS - 創(chuàng)建第一個(gè)iPhone應(yīng)用
iOS (iPhone, iPad)教程
iOS - 發(fā)送電子郵箱(Email)
iOS - View Transitions(視圖轉(zhuǎn)換)
iOS - 內(nèi)存管理
iOS - Icons(圖標(biāo))
iOS - 音頻和視頻
iOS - Storyboards(演示圖板演)
iOS - Buttons(按鈕)
iOS - Text Field(文本域)
iOS - Sliders(滑動(dòng)條)
iOS - Scroll View(滾動(dòng)視圖)
IOS - 輸入類型 文本字段
iOS - 位置處理
iOS - Accelerometer(加速度傳感器)
IOS - 快速入門(mén)
iOS - SQLite 數(shù)據(jù)庫(kù)
iOS - GameKit
IOS - 訪問(wèn)地圖
iOS - Objective-C基礎(chǔ)
iOS - Toolbar(工具欄)
IOS - 動(dòng)作和插座(Outlets)
iOS - Alerts(警示)
IOS - 通用應(yīng)用程序

iOS - Toolbar(工具欄)

toolbar(工具欄)使用實(shí)例

IOS 中如果我們想操縱一些東西,基于目前的視圖,我們可以使用工具欄(toolbar)。

例如將電子郵件應(yīng)用程序的收件箱項(xiàng)選擇刪除,做標(biāo)志,回復(fù)等。如下所示。

iOS Tutorial

 

重要的屬性

  • barStyle

  • items

添加一個(gè)自定義的方法addToolbar

-(void)addToolbar
{
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] 
    initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
    target:nil action:nil];
    UIBarButtonItem *customItem1 = [[UIBarButtonItem alloc]
    initWithTitle:@"Tool1" style:UIBarButtonItemStyleBordered 
    target:self action:@selector(toolBarItem1:)];
    UIBarButtonItem *customItem2 = [[UIBarButtonItem alloc]
    initWithTitle:@"Tool2" style:UIBarButtonItemStyleDone 
    target:self action:@selector(toolBarItem2:)];
    NSArray *toolbarItems = [NSArray arrayWithObjects: 
    customItem1,spaceItem, customItem2, nil];
    UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:
    CGRectMake(0, 366+54, 320, 50)];
    [toolbar setBarStyle:UIBarStyleBlackOpaque];
    [self.view addSubview:toolbar];
    [toolbar setItems:toolbarItems];
}

為了解所執(zhí)行的操作,我們添加 UILabel 在 ViewController.xib 中并創(chuàng)建一個(gè) IBOutlet  的 UILabel,并將它命名為 label。

我們還需要添加兩個(gè)方法以執(zhí)行工具欄項(xiàng)目的操作,如下圖所示

-(IBAction)toolBarItem1:(id)sender{
    [label setText:@"Tool 1 Selected"];
}

-(IBAction)toolBarItem2:(id)sender{
    [label setText:@"Tool 2 Selected"];    
}

 

更新ViewController.m 中的方法 viewDidLoad 如下

- (void)viewDidLoad
{
    [super viewDidLoad];
    // The method hideStatusbar called after 2 seconds
    [self addToolbar];    
    // Do any additional setup after loading the view, typically from a nib.
}

輸出

現(xiàn)在,當(dāng)我們運(yùn)行程序時(shí),我們會(huì)得到下面的輸出。

iOS Tutorial

 

點(diǎn)擊 tool1 和 tool2 欄按鈕,我們得到

iOS Tutorial