IOS 4中Block实战之UIActionSheet

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

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

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 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

2025-01-10 09:47:43

blockSDKiOS

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 12:55:15

CSSblockinline

2011-07-28 09:49:50

IOS IOS 4 UI

2010-09-03 10:18:06

CSSdisplay:inl

2010-09-16 09:13:09

CSS display

2012-05-01 08:26:00

iOS

2010-09-09 15:54:00

blockinlineCSS

2014-07-30 11:12:09

block

2011-08-19 13:51:12

2014-05-09 12:59:26

iOS移动互联网

2011-08-02 13:35:41

iOS开发 Get Post

2014-07-23 13:17:53

iOSUITextField
点赞
收藏

51CTO技术栈公众号