/BlocksKit/UIControl+BlocksKit.m
Objective C | 111 lines | 78 code | 29 blank | 4 comment | 7 complexity | 82417fa32f3d92f94a232ae01bf5fac9 MD5 | raw file
1// 2// UIControl+BlocksKit.m 3// BlocksKit 4// 5 6#import "UIControl+BlocksKit.h" 7#import "NSObject+AssociatedObjects.h" 8#import "NSSet+BlocksKit.h" 9 10static char kControlHandlersKey; 11 12#pragma mark Private 13 14@interface BKControlWrapper : NSObject <NSCopying> 15 16- (id)initWithHandler:(BKSenderBlock)aHandler forControlEvents:(UIControlEvents)someControlEvents; 17@property (nonatomic, copy) BKSenderBlock handler; 18@property (nonatomic) UIControlEvents controlEvents; 19 20@end 21 22@implementation BKControlWrapper 23 24@synthesize handler, controlEvents; 25 26- (id)initWithHandler:(BKSenderBlock)aHandler forControlEvents:(UIControlEvents)someControlEvents { 27 if ((self = [super init])) { 28 self.handler = aHandler; 29 self.controlEvents = someControlEvents; 30 } 31 return self; 32} 33 34- (id)copyWithZone:(NSZone *)zone { 35 return [[BKControlWrapper alloc] initWithHandler:self.handler forControlEvents:self.controlEvents]; 36} 37 38- (void)invoke:(id)sender { 39 self.handler(sender); 40} 41 42- (void)dealloc { 43 self.handler = nil; 44 [super dealloc]; 45} 46 47@end 48 49#pragma mark Category 50 51@implementation UIControl (BlocksKit) 52 53- (void)addEventHandler:(BKSenderBlock)handler forControlEvents:(UIControlEvents)controlEvents { 54 NSParameterAssert(handler); 55 56 NSMutableDictionary *events = [self associatedValueForKey:&kControlHandlersKey]; 57 if (!events) { 58 events = [NSMutableDictionary dictionary]; 59 [self associateValue:events withKey:&kControlHandlersKey]; 60 } 61 62 NSNumber *key = [NSNumber numberWithUnsignedInteger:controlEvents]; 63 NSMutableSet *handlers = [events objectForKey:key]; 64 if (!handlers) { 65 handlers = [NSMutableSet set]; 66 [events setObject:handlers forKey:key]; 67 } 68 69 BKControlWrapper *target = [[BKControlWrapper alloc] initWithHandler:handler forControlEvents:controlEvents]; 70 [handlers addObject:target]; 71 [self addTarget:target action:@selector(invoke:) forControlEvents:controlEvents]; 72 [target release]; 73} 74 75- (void)removeEventHandlersForControlEvents:(UIControlEvents)controlEvents { 76 NSMutableDictionary *events = [self associatedValueForKey:&kControlHandlersKey]; 77 if (!events) { 78 events = [NSMutableDictionary dictionary]; 79 [self associateValue:events withKey:&kControlHandlersKey]; 80 } 81 82 NSNumber *key = [NSNumber numberWithUnsignedInteger:controlEvents]; 83 NSSet *handlers = [events objectForKey:key]; 84 85 if (!handlers) 86 return; 87 88 [handlers each:^(id sender) { 89 [self removeTarget:sender action:NULL forControlEvents:controlEvents]; 90 }]; 91 92 [events removeObjectForKey:key]; 93} 94 95- (BOOL)hasEventHandlersForControlEvents:(UIControlEvents)controlEvents { 96 NSMutableDictionary *events = [self associatedValueForKey:&kControlHandlersKey]; 97 if (!events) { 98 events = [NSMutableDictionary dictionary]; 99 [self associateValue:events withKey:&kControlHandlersKey]; 100 } 101 102 NSNumber *key = [NSNumber numberWithUnsignedInteger:controlEvents]; 103 NSSet *handlers = [events objectForKey:key]; 104 105 if (!handlers) 106 return NO; 107 108 return handlers.count; 109} 110 111@end