IOS 4中Block实战之UIActionSheet

移动开发 iOS
熟悉函数指针的朋友对Block不会感冒的,因为它们实质是一样的,只是叫清一不样。今天将实战BLOCK,我们将封装一个支持Block的UIActionSheet。

IOS 4Block实战之UIActionSheet是本文介绍的内容,BlockIOS 4的新东西,有了它,源码的逻辑将更清楚,代码的可读性将提高。熟悉函数指针的朋友对Block不会感冒的,因为它们实质是一样的,只是叫清一不样。今天将实战BLOCK,我们将封装一个支持BlockUIActionSheet。好了废话少说,直接上代码:

  1. PLActionSheet.h  
  2.  
  3. #import <UIKit/UIKit.h>     
  4.     
  5. /**   
  6.  * A simple block-enabled API wrapper on top of UIActionSheet.   
  7.  */    
  8. @interface PLActionSheet : NSObject <UIActionSheetDelegate> {    
  9. @private    
  10.     UIActionSheet *_sheet;    
  11.     NSMutableArray *_blocks;    
  12. }    
  13.     
  14. - (id) initWithTitle: (NSString *) title;    
  15.     
  16. - (void) setCancelButtonWithTitle: (NSString *) title block: (void (^)()) block;    
  17. - (void) addButtonWithTitle: (NSString *) title block: (void (^)()) block;    
  18.     
  19. - (void) showInView: (UIView *) view;    
  20.     
  21. @end    
  22. #import <UIKit/UIKit.h> 
  23.  
  24. /**  
  25.  * A simple block-enabled API wrapper on top of UIActionSheet.  
  26.  */  
  27. @interface PLActionSheet : NSObject <UIActionSheetDelegate> {  
  28. @private  
  29.     UIActionSheet *_sheet;  
  30.     NSMutableArray *_blocks;  
  31. }  
  32.  
  33. - (id) initWithTitle: (NSString *) title;  
  34.  
  35. - (void) setCancelButtonWithTitle: (NSString *) title block: (void (^)()) block;  
  36. - (void) addButtonWithTitle: (NSString *) title block: (void (^)()) block;  
  37.  
  38. - (void) showInView: (UIView *) view;  
  39.  
  40. @end  
  41. PLActionSheet.m  
  42.  
  43. #import "PLActionSheet.h"     
  44.     
  45. @implementation PLActionSheet    
  46.     
  47. - (id) initWithTitle: (NSString *) title {    
  48.     if ((self = [super init]) == nil)    
  49.         return nil;    
  50.         
  51.     /* Initialize the sheet */    
  52.     _sheet = [[UIActionSheet alloc] initWithTitle: title delegate: self cancelButtonTitle: 
  53. nil destructiveButtonTitle: nil otherButtonTitles: nil];    
  54.     
  55.     /* Initialize button -> block array */    
  56.     _blocks = [[NSMutableArray alloc] init];    
  57.     
  58.     return self;    
  59. }    
  60.     
  61. - (void) dealloc {    
  62.     _sheet.delegate = nil;    
  63.     [_sheet release];    
  64.     
  65.     [_blocks release];    
  66.     
  67.     [super dealloc];    
  68. }    
  69. - (void) setCancelButtonWithTitle: (NSString *) title block: (void (^)()) block {    
  70.     [self addButtonWithTitle: title block: block];    
  71.     _sheet_sheet.cancelButtonIndex = _sheet.numberOfButtons - 1;    
  72. }    
  73.     
  74. - (void) addButtonWithTitle: (NSString *) title block: (void (^)()) block {    
  75.     [_blocks addObject: [[block copy] autorelease]];    
  76.     [_sheet addButtonWithTitle: title];    
  77. }    
  78.     
  79. - (void) showInView: (UIView *) view {    
  80.     [_sheet showInView: view];    
  81.     
  82.     /* Ensure that the delegate (that's us) survives until the sheet is dismissed */    
  83.     [self retain];    
  84. }    
  85.     
  86. - (void) actionSheet: (UIActionSheet *) actionSheet clickedButtonAtIndex: (NSInteger) buttonIndex {    
  87.     /* Run the button's block */    
  88.     if (buttonIndex >= 0 && buttonIndex < [_blocks count]) {    
  89.         void (^b)() = [_blocks objectAtIndex: buttonIndex];    
  90.         b();    
  91.     }    
  92.     
  93.     /* Sheet to be dismissed, drop our self reference */    
  94.     [self release];    
  95. }    
  96.     
  97. @end    
  98. #import "PLActionSheet.h"  
  99.  
  100. @implementation PLActionSheet  
  101.  
  102. - (id) initWithTitle: (NSString *) title {  
  103.     if ((self = [super init]) == nil)  
  104.         return nil;  
  105.       
  106.     /* Initialize the sheet */  
  107.     _sheet = [[UIActionSheet alloc] initWithTitle: title delegate: self cancelButtonTitle: 
  108. nil destructiveButtonTitle: nil otherButtonTitles: nil];  
  109.     /* Initialize button -> block array */  
  110.     _blocks = [[NSMutableArray alloc] init];  
  111.  
  112.     return self;  
  113. }  
  114.  
  115. - (void) dealloc {  
  116.     _sheet.delegate = nil;  
  117.     [_sheet release];  
  118.  
  119.     [_blocks release];  
  120.  
  121.     [super dealloc];  
  122. }  
  123.  
  124. - (void) setCancelButtonWithTitle: (NSString *) title block: (void (^)()) block {  
  125.     [self addButtonWithTitle: title block: block];  
  126.     _sheet_sheet.cancelButtonIndex = _sheet.numberOfButtons - 1;  
  127. }  
  128.  
  129. - (void) addButtonWithTitle: (NSString *) title block: (void (^)()) block {  
  130.     [_blocks addObject: [[block copy] autorelease]];  
  131.     [_sheet addButtonWithTitle: title];  
  132. }  
  133.  
  134. - (void) showInView: (UIView *) view {  
  135.     [_sheet showInView: view];  
  136.  
  137.     /* Ensure that the delegate (that's us) survives until the sheet is dismissed */  
  138.     [self retain];  
  139. }  
  140.  
  141. - (void) actionSheet: (UIActionSheet *) actionSheet clickedButtonAtIndex: (NSInteger) buttonIndex {  
  142.     /* Run the button's block */  
  143.     if (buttonIndex >= 0 && buttonIndex < [_blocks count]) {  
  144.         void (^b)() = [_blocks objectAtIndex: buttonIndex];  
  145.         b();  
  146.     }  
  147.  
  148.     /* Sheet to be dismissed, drop our self reference */  
  149.     [self release];  
  150. }  
  151.  
  152. @end 

用法如下:

  1. - (void) displaySheet {    
  2.     PLActionSheet *sheet = [[PLActionSheet alloc] initWithTitle: @"Destination"];    
  3.     /* A re-usable block that simply displays an alert message */    
  4.     void (^alert)(NSString *) = ^(NSString *message) {    
  5.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Destination Selected"                                 
  6.       message: message                                                   
  7.  delegate: nil                                        
  8.    cancelButtonTitle: @"OK"                                       
  9.    otherButtonTitles: nil];    
  10.         [alert show];    
  11.  
  12.         [alert release];    
  13.     };    
  14.     [sheet addButtonWithTitle: @"Work" block: ^{    
  15.         alert(@"Work selected");    
  16.     }];    
  17.     [sheet addButtonWithTitle: @"Home" block: ^{    
  18.         alert(@"Home selected");    
  19.     }];    
  20.     [sheet addButtonWithTitle: @"School" block: ^{    
  21.         alert(@"School selected");    
  22.     }];     
  23.     [sheet setCancelButtonWithTitle: @"Cancel" block: ^{}];    
  24.      
  25.     [sheet showInView: self.window];    
  26.     [sheet release];    
  27. }    
  28. - (void) displaySheet {  
  29.     PLActionSheet *sheet = [[PLActionSheet alloc] initWithTitle: @"Destination"];  
  30.     /* A re-usable block that simply displays an alert message */  
  31.     void (^alert)(NSString *) = ^(NSString *message) {  
  32.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Destination Selected"         
  33.         message: message                                              
  34.       delegate: nil                                        
  35.    cancelButtonTitle: @"OK"                                  
  36.       otherButtonTitles: nil];  
  37.         [alert show];  
  38.         [alert release];  
  39.     };  
  40.       
  41.     [sheet addButtonWithTitle: @"Work" block: ^{  
  42.         alert(@"Work selected");  
  43.     }];  
  44.     [sheet addButtonWithTitle: @"Home" block: ^{  
  45.         alert(@"Home selected");  
  46.     }];  
  47.     [sheet addButtonWithTitle: @"School" block: ^{  
  48.         alert(@"School selected");  
  49.     }];  
  50.     [sheet setCancelButtonWithTitle: @"Cancel" block: ^{}];  
  51.     [sheet showInView: self.window];  
  52.     [sheet release];  

采用BLOCK的方法,源码可读性大大增强。如果我们在同一个Controller里需要多个UIActionSheet, 而只有一个delegate方法,那在这个delegate方法里就要跟踪现在是哪一个UIActionSheet,这样就会有很多if else的代,也难于维护。以后将多采用BLOCK来写程序了。

小结:IOS 4Block实战之UIActionSheet的内容介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2013-07-19 12:52:50

iOS中BlockiOS开发学习

2017-03-07 09:45:43

iOSBlock开发

2013-07-19 14:35:59

iOS中BlockiOS开发学习

2013-07-19 14:00:13

iOS中BlockiOS开发学习

2013-06-04 15:41:31

iOS开发移动开发block

2013-07-19 13:16:26

iOS中BlockiOS开发学习内存管理

2011-08-01 18:44:16

iPhone开发 UIALertVie UIActionSh

2011-08-16 10:45:25

iPhone开发控件

2010-09-03 10:18:06

CSSdisplay:inl

2011-07-28 09:49:50

IOS IOS 4 UI

2010-09-03 12:55:15

CSSblockinline

2011-08-19 13:51:12

2012-05-01 08:26:00

iOS

2014-05-09 12:59:26

iOS移动互联网

2010-09-09 15:54:00

blockinlineCSS

2014-07-30 11:12:09

block

2010-09-16 09:13:09

CSS display

2011-08-02 13:35:41

iOS开发 Get Post

2010-09-02 12:58:21

display:inlCSS

2013-01-07 10:42:43

HDFS
点赞
收藏

51CTO技术栈公众号