Created
March 26, 2010 22:01
-
-
Save jonsterling/345458 to your computer and use it in GitHub Desktop.
This file contains 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
#import <UIKit/UIKit.h> | |
@interface UIControl (Blocks) | |
- (void)setActionForControlEvents:(UIControlEvents)controlEvents withBlock:(void (^)(id))aBlock; | |
@end |
This file contains 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
#import "UIControl+Blocks.h" | |
#import "OTAssociatedObjectsSimulator.h" // hack to let sim use associated objects | |
IMP executeBlockWithControlEvents(id self, SEL _cmd, id sender); | |
static NSString * const kUIControlAssociatedObjectBlockPrefix = @"__actionblockforevents_"; | |
@implementation UIControl (Blocks) | |
- (void)setActionForControlEvents:(UIControlEvents)controlEvents withBlock:(void (^)(id))aBlock { | |
SEL key = NSSelectorFromString([kUIControlAssociatedObjectBlockPrefix stringByAppendingFormat:@"%i",controlEvents]); | |
class_addMethod([self class], key, (IMP)executeBlockWithControlEvents, "@@:@"); | |
objc_setAssociatedObject(self, key, [[aBlock copy] autorelease], OBJC_ASSOCIATION_RETAIN_NONATOMIC); | |
[self addTarget:self action:key forControlEvents:controlEvents]; | |
} | |
@end | |
IMP executeBlockWithControlEvents(id self, SEL _cmd, id sender) { | |
void (^block)(id) = objc_getAssociatedObject(self, _cmd); | |
if (block) { block(sender); } | |
return nil; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment