码迷,mamicode.com
首页 > 移动开发 > 详细

ios之BlocksKit源码学习1-UIControl

时间:2015-04-15 13:07:08      阅读:402      评论:0      收藏:0      [点我收藏+]

标签:

- (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事件。

ios之BlocksKit源码学习1-UIControl

标签:

原文地址:http://www.cnblogs.com/sfce/p/4427916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!