IOS 4中Block實戰(zhàn)之UIActionSheet
IOS 4中Block實戰(zhàn)之UIActionSheet是本文介紹的內(nèi)容,Block是IOS 4的新東西,有了它,源碼的邏輯將更清楚,代碼的可讀性將提高。熟悉函數(shù)指針的朋友對Block不會感冒的,因為它們實質(zhì)是一樣的,只是叫清一不樣。今天將實戰(zhàn)BLOCK,我們將封裝一個支持Block的UIActionSheet。好了廢話少說,直接上代碼:
- PLActionSheet.h
- #import <UIKit/UIKit.h>
- /**
- * A simple block-enabled API wrapper on top of UIActionSheet.
- */
- @interface PLActionSheet : NSObject <UIActionSheetDelegate> {
- @private
- UIActionSheet *_sheet;
- NSMutableArray *_blocks;
- }
- - (id) initWithTitle: (NSString *) title;
- - (void) setCancelButtonWithTitle: (NSString *) title block: (void (^)()) block;
- - (void) addButtonWithTitle: (NSString *) title block: (void (^)()) block;
- - (void) showInView: (UIView *) view;
- @end
- #import <UIKit/UIKit.h>
- /**
- * A simple block-enabled API wrapper on top of UIActionSheet.
- */
- @interface PLActionSheet : NSObject <UIActionSheetDelegate> {
- @private
- UIActionSheet *_sheet;
- NSMutableArray *_blocks;
- }
- - (id) initWithTitle: (NSString *) title;
- - (void) setCancelButtonWithTitle: (NSString *) title block: (void (^)()) block;
- - (void) addButtonWithTitle: (NSString *) title block: (void (^)()) block;
- - (void) showInView: (UIView *) view;
- @end
- PLActionSheet.m
- #import "PLActionSheet.h"
- @implementation PLActionSheet
- - (id) initWithTitle: (NSString *) title {
- if ((self = [super init]) == nil)
- return nil;
- /* Initialize the sheet */
- _sheet = [[UIActionSheet alloc] initWithTitle: title delegate: self cancelButtonTitle:
- nil destructiveButtonTitle: nil otherButtonTitles: nil];
- /* Initialize button -> block array */
- _blocks = [[NSMutableArray alloc] init];
- return self;
- }
- - (void) dealloc {
- _sheet.delegate = nil;
- [_sheet release];
- [_blocks release];
- [super dealloc];
- }
- - (void) setCancelButtonWithTitle: (NSString *) title block: (void (^)()) block {
- [self addButtonWithTitle: title block: block];
- _sheet_sheet.cancelButtonIndex = _sheet.numberOfButtons - 1;
- }
- - (void) addButtonWithTitle: (NSString *) title block: (void (^)()) block {
- [_blocks addObject: [[block copy] autorelease]];
- [_sheet addButtonWithTitle: title];
- }
- - (void) showInView: (UIView *) view {
- [_sheet showInView: view];
- /* Ensure that the delegate (that's us) survives until the sheet is dismissed */
- [self retain];
- }
- - (void) actionSheet: (UIActionSheet *) actionSheet clickedButtonAtIndex: (NSInteger) buttonIndex {
- /* Run the button's block */
- if (buttonIndex >= 0 && buttonIndex < [_blocks count]) {
- void (^b)() = [_blocks objectAtIndex: buttonIndex];
- b();
- }
- /* Sheet to be dismissed, drop our self reference */
- [self release];
- }
- @end
- #import "PLActionSheet.h"
- @implementation PLActionSheet
- - (id) initWithTitle: (NSString *) title {
- if ((self = [super init]) == nil)
- return nil;
- /* Initialize the sheet */
- _sheet = [[UIActionSheet alloc] initWithTitle: title delegate: self cancelButtonTitle:
- nil destructiveButtonTitle: nil otherButtonTitles: nil];
- /* Initialize button -> block array */
- _blocks = [[NSMutableArray alloc] init];
- return self;
- }
- - (void) dealloc {
- _sheet.delegate = nil;
- [_sheet release];
- [_blocks release];
- [super dealloc];
- }
- - (void) setCancelButtonWithTitle: (NSString *) title block: (void (^)()) block {
- [self addButtonWithTitle: title block: block];
- _sheet_sheet.cancelButtonIndex = _sheet.numberOfButtons - 1;
- }
- - (void) addButtonWithTitle: (NSString *) title block: (void (^)()) block {
- [_blocks addObject: [[block copy] autorelease]];
- [_sheet addButtonWithTitle: title];
- }
- - (void) showInView: (UIView *) view {
- [_sheet showInView: view];
- /* Ensure that the delegate (that's us) survives until the sheet is dismissed */
- [self retain];
- }
- - (void) actionSheet: (UIActionSheet *) actionSheet clickedButtonAtIndex: (NSInteger) buttonIndex {
- /* Run the button's block */
- if (buttonIndex >= 0 && buttonIndex < [_blocks count]) {
- void (^b)() = [_blocks objectAtIndex: buttonIndex];
- b();
- }
- /* Sheet to be dismissed, drop our self reference */
- [self release];
- }
- @end
用法如下:
- - (void) displaySheet {
- PLActionSheet *sheet = [[PLActionSheet alloc] initWithTitle: @"Destination"];
- /* A re-usable block that simply displays an alert message */
- void (^alert)(NSString *) = ^(NSString *message) {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Destination Selected"
- message: message
- delegate: nil
- cancelButtonTitle: @"OK"
- otherButtonTitles: nil];
- [alert show];
- [alert release];
- };
- [sheet addButtonWithTitle: @"Work" block: ^{
- alert(@"Work selected");
- }];
- [sheet addButtonWithTitle: @"Home" block: ^{
- alert(@"Home selected");
- }];
- [sheet addButtonWithTitle: @"School" block: ^{
- alert(@"School selected");
- }];
- [sheet setCancelButtonWithTitle: @"Cancel" block: ^{}];
- [sheet showInView: self.window];
- [sheet release];
- }
- - (void) displaySheet {
- PLActionSheet *sheet = [[PLActionSheet alloc] initWithTitle: @"Destination"];
- /* A re-usable block that simply displays an alert message */
- void (^alert)(NSString *) = ^(NSString *message) {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Destination Selected"
- message: message
- delegate: nil
- cancelButtonTitle: @"OK"
- otherButtonTitles: nil];
- [alert show];
- [alert release];
- };
- [sheet addButtonWithTitle: @"Work" block: ^{
- alert(@"Work selected");
- }];
- [sheet addButtonWithTitle: @"Home" block: ^{
- alert(@"Home selected");
- }];
- [sheet addButtonWithTitle: @"School" block: ^{
- alert(@"School selected");
- }];
- [sheet setCancelButtonWithTitle: @"Cancel" block: ^{}];
- [sheet showInView: self.window];
- [sheet release];
- }
采用BLOCK的方法,源碼可讀性大大增強(qiáng)。如果我們在同一個Controller里需要多個UIActionSheet, 而只有一個delegate方法,那在這個delegate方法里就要跟蹤現(xiàn)在是哪一個UIActionSheet,這樣就會有很多if else的代,也難于維護(hù)。以后將多采用BLOCK來寫程序了。
小結(jié):IOS 4中Block實戰(zhàn)之UIActionSheet的內(nèi)容介紹完了,希望本文對你有所幫助!