IOS 4中Block实战之UIActionSheet是本文介绍的内容,Block是IOS 4的新东西,有了它,源码的逻辑将更清楚,代码的可读性将提高。熟悉函数指针的朋友对Block不会感冒的,因为它们实质是一样的,只是叫清一不样。今天将实战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
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
用法如下:
- (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];
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
采用BLOCK的方法,源码可读性大大增强。如果我们在同一个Controller里需要多个UIActionSheet, 而只有一个delegate方法,那在这个delegate方法里就要跟踪现在是哪一个UIActionSheet,这样就会有很多if else的代,也难于维护。以后将多采用BLOCK来写程序了。
小结:IOS 4中Block实战之UIActionSheet的内容介绍完了,希望本文对你有所帮助!