【问题标题】:How does UIControl manage its target?UIControl 如何管理它的目标?
【发布时间】:2015-09-26 04:42:15
【问题描述】:

苹果:

// the action cannot be NULL. Note that the target is not retained.
- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

请注意,不保留目标。

在 UIControl+BlocksKit.m 中

BKControlWrapper *target = [[BKControlWrapper alloc] initWithHandler:handler forControlEvents:controlEvents];
[handlers addObject:target];
[self addTarget:target action:@selector(invoke:) forControlEvents:controlEvents];

那么UIControl如何管理目标,为什么目标没有释放?

【问题讨论】:

  • UIControl 目标没有被释放,因为它没有被保留。

标签: ios target uicontrol


【解决方案1】:

bk_addEventHandler:forControlEvents: 方法通过将BKControlWrapper 实例添加到NSMutableSet 来保留它。该集合由events 字典保留,该字典由控件本身保留,因为该字典使用OBJC_ASSOCIATION_RETAIN_NONATOMIC 与控件关联。

重要的是您传递给bk_addEventHandler:forControlEvents: 的块不会创建保留循环。为此,您必须确保块在需要时使用弱引用(例如,如果它需要引用视图控制器而不是引用控件)。

【讨论】:

    猜你喜欢
    • 2011-07-09
    • 2011-04-28
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    相关资源
    最近更新 更多