欄式按鈕項(UIBarButtonItem類)


UIBarButtonItem類繼承自UIBarItem類,而UIBarItem類是一個可以放置在Bar之上的所有小控件類的抽象類。繼承了該基類所有子類在外觀上類似於一個Button,它們都有一個標題,圖片,動作以及目標,這點可以從其子類的初始化方法處看到。

對於子類UIBarButtonItem而言,它是專門放置在UIToolbarUINavigationBar之上的Button,它從其類處繼承了Button的基本行爲。通常,我們會使用Interface Builder來創建和配置UIBarButtonItem對象,這種方式更直觀,更方便,也更易理解。

接下來,我們創建一個簡單工程以展示UIBarButtonItem在工具欄和導航欄上的應用,其中,UIBarButtonItem對象通過Interface Builder來創建。

備註:該工程使用Xcode 4.6.2 (4H1003)版本創建,目標iOS SDK版本爲6.1,並使用了自動引用計數。同時,該實例並沒有考慮實際應用的需要,僅作學習參考使用。

首先,通過嚮導創建一個Single View Application工程,命名爲UIBarButtonItemSample, 設Project Prefix爲WJF。創建完成後,修改WJFViewController.xib,首先從對象庫中分別拖出一個Navigation Bar和一個Toolbar放置在視圖View的上方和下方,然後再從對象庫中拖出幾個Bar Button Item分別放置在Navigation Bar和Toolbar上,最後,分別點中這幾個Bar Button Item後,在右側的屬性檢查器(Attributes Inspector)中設置它們的標示(Identifier)。修改後的WJFViewController.xib在Interface Builder中的顯示如下:


       

最終,在模擬器上的運行結果如下:

       

之前,我們通過Interface Builder來創建UIBarButtonItem,現在我們嘗試用代碼的方式來創建它們。

首先,我們刪除xib內文件所有者的view輸出口關聯,然後在右面的對象庫裏重新拖出一個view,並讓其與文件所有者的view輸出口建立關聯。接下來修改WJFViewController.h文件和WJFViewController.m文件,修改後如下:

WJFViewController.h文件

//
//  WJFViewController.h
//  UIBarButtonItemSample
//
//  Created by jiafu wan on 6/25/13.
//  Copyright (c) 2013 jiafu wan. All rights reserved.
//

#import <UIKit/UIKit.h>
#define NAVIGATIONBAR_HEIGHT    44
#define TOOLBAR_HEIGHT          44
@interface WJFViewController : UIViewController
@property (weak, nonatomic)UINavigationItem *navigationItem;
@end

WJFViewController.m文件

//
//  WJFViewController.m
//  UIBarButtonItemSample
//
//  Created by jiafu wan on 6/25/13.
//  Copyright (c) 2013 jiafu wan. All rights reserved.
//

#import "WJFViewController.h"

@interface WJFViewController ()
{
    UINavigationBar *navigationBar;
    UIToolbar       *toolbar;
}
@end

@implementation WJFViewController

@synthesize navigationItem;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    //1.創建NavigationBar及button
    UIBarButtonItem  *leftItemButton, *rightItemButton;
    navigationBar = [[UINavigationBar alloc] init];
    navigationItem = [[UINavigationItem alloc] initWithTitle:@"Title" ];
    leftItemButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:nil];
    rightItemButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
    if (leftItemButton != nil && self.navigationItem != nil) {
        self.navigationItem.leftBarButtonItem = leftItemButton;
    }
    if (rightItemButton != nil && self.navigationItem != nil) {
        self.navigationItem.rightBarButtonItem = rightItemButton;
    }
    if (navigationBar != nil) {
        navigationBar.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, NAVIGATIONBAR_HEIGHT);
        [navigationBar pushNavigationItem:self.navigationItem animated:FALSE];
        [self.view addSubview:navigationBar];  
    }
    //2.創建Toobar及button
    NSArray *itemButtons;
    UIBarButtonItem  *rewindItemButton, *fastForwardItemButton, *actionItemButton, *trashItemButton,*flexibleSpaceItem;
    toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height -  [[UIApplication sharedApplication] statusBarFrame].size.height -  TOOLBAR_HEIGHT, [UIScreen mainScreen].bounds.size.width, TOOLBAR_HEIGHT)];
    rewindItemButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind  target:self action:nil];
    rewindItemButton.style = UIBarButtonItemStyleBordered;
    fastForwardItemButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:nil];
    fastForwardItemButton.style = UIBarButtonItemStyleBordered;
    actionItemButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
    actionItemButton.style = UIBarButtonItemStyleBordered;
    trashItemButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:nil];
    trashItemButton.style = UIBarButtonItemStyleBordered;
    flexibleSpaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    //注意順序
    itemButtons = [[NSArray alloc ] initWithObjects:rewindItemButton, fastForwardItemButton, actionItemButton, flexibleSpaceItem, trashItemButton, nil];
    if (toolbar != nil) {
        toolbar.items = itemButtons;
        [self.view addSubview:toolbar];
    }
}

- (BOOL)shouldAutorotate
{
    return TRUE;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight|UIInterfaceOrientationMaskPortrait;
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
    if (interfaceOrientation == UIInterfaceOrientationPortrait) {
        navigationBar.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, NAVIGATIONBAR_HEIGHT);
        toolbar.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height -  [[UIApplication sharedApplication] statusBarFrame].size.height -  TOOLBAR_HEIGHT, [UIScreen mainScreen].bounds.size.width, TOOLBAR_HEIGHT);
    }
    else if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft | interfaceOrientation == UIInterfaceOrientationLandscapeRight){
        navigationBar.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, NAVIGATIONBAR_HEIGHT);
        toolbar.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.width -  [[UIApplication sharedApplication] statusBarFrame].size.width -  TOOLBAR_HEIGHT, [UIScreen mainScreen].bounds.size.height, TOOLBAR_HEIGHT);
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


最後,運行的結果同先前是一樣的。

不難看出,UIBarButtonItem只是一種Button,但它需要同NavigationBar和Toolbar結合使用時才能體現它的特別。我們在介紹NavigationBar和Toolbar時再大概地對這些特別支持進行介紹。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章