Created
December 27, 2011 13:38
-
-
Save puppybits/1523687 to your computer and use it in GitHub Desktop.
Replace common iOS delegate APIs with blocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// iOS_API+Blocks.h | |
// | |
// Created by Bobby Schultz on 12/1/11. | |
@interface UIAlertView (PBBlocks) | |
+ (id) alertWithTitle:(NSString*)title | |
message:(NSString*)message | |
clickedBlock:(void(^)(NSInteger))buttonIndexClickedBlock | |
cancelButtonTitle:(NSString*)cancelButtonTitle | |
otherButtonTitles:(NSString*)otherButtonTitles; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// iOS_API+Blocks.m | |
// | |
// Created by Bobby Schultz on 12/1/11. | |
// Generic Block Delegate | |
@interface DelegateBlock:NSObject | |
typedef void (^HeapBlock)(NSInteger); | |
@property (nonatomic, copy) HeapBlock callbackBlock; | |
@end | |
@implementation DelegateBlock | |
@synthesize callbackBlock; | |
- (id) initWithBlock:(void(^)(NSInteger))callback | |
{ | |
// Init and copy Callback Block to the heap (@see accessor) | |
if (self = [super init]) | |
[self setCallbackBlock:callback]; | |
return [self autorelease]; | |
} | |
- (void) dealloc | |
{ | |
// Release the block | |
[callbackBlock release], callbackBlock = nil; | |
[super dealloc]; | |
} | |
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex | |
{ | |
// Return the result to the callback | |
callbackBlock(buttonIndex); | |
// Detach the block delegate, will decrement retain count | |
SEL key = @selector(alertWithTitle:message:clickedBlock:cancelButtonTitle:otherButtonTitles:); | |
objc_setAssociatedObject(alertView, key, nil, OBJC_ASSOCIATION_RETAIN); | |
key = nil; | |
// Release the Alert | |
[alertView release]; | |
} | |
@end | |
// Alert View Category | |
// | |
// @example __block BOOL shouldHide = NO; | |
// [UIAlertView alertWithTitle:@"Danger!" | |
// message:@"Danger Will Robinson!" | |
// clickedBlock:^(NSInteger buttonIndex){ | |
// if (buttonIndex == 1) { | |
// shouldHide = YES; | |
// } | |
// } | |
// cancelButtonTitle:@"Run" | |
// otherButtonTitles:@"Hide"]; | |
// | |
@implementation UIAlertView (PBBlocks) | |
+ (id) alertWithTitle:(NSString*)title | |
message:(NSString*)message | |
clickedBlock:(void(^)(NSInteger))buttonIndexClickedBlock | |
cancelButtonTitle:(NSString*)cancelButtonTitle | |
otherButtonTitles:(NSString*)otherButtonTitles | |
{ | |
// Create class to hold delegatee and copy block to heap | |
DelegateBlock *delegatee = [[DelegateBlock alloc] initWithBlock:buttonIndexClickedBlock]; | |
[[delegatee retain] autorelease]; | |
// Create delegater | |
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title | |
message:message | |
delegate:delegatee | |
cancelButtonTitle:cancelButtonTitle | |
otherButtonTitles:otherButtonTitles, nil]; | |
// Attach the Delegate Block class to the Alert View, increase the retain count | |
objc_setAssociatedObject(alert, _cmd, delegatee, OBJC_ASSOCIATION_RETAIN); | |
// Display the alert | |
[alert show]; | |
return alert; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment