ios之BlocksKit源码学习1-UIControl
- (void)bk_addEventHandler:(void (^)(id sender))handler forControlEvents:(UIControlEvents)controlEvents { NSParameterAssert(handler); NSMutableDictionary *events = objc_getAssociatedObject(self, BKControlHandlersKey); if (!events) { events = [NSMutableDictionary dictionary]; objc_setAssociatedObject(self, BKControlHandlersKey, events, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } NSNumber *key = @(controlEvents); NSMutableSet *handlers = events[key]; if (!handlers) { handlers = [NSMutableSet set]; events[key] = handlers; } BKControlWrapper *target = [[BKControlWrapper alloc] initWithHandler:handler forControlEvents:controlEvents]; [handlers addObject:target]; [self addTarget:target action:@selector(invoke:) forControlEvents:controlEvents]; }
UIControl的
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
方法对target不执行retain操作
将UIControl的实例关联一个NSMutableDictionary用来持有多个target的引用。
这个NSMutableDictionary 以UIControlEvents为Key,以NSMutableSet为Value。
NSMutableSet可添加多个target。
这样就实现了为UIControl添加多个Block事件。