2015-09-26 14 views
-1

蘋果:UIControl如何管理其目標?

// 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如何管理目標,爲什麼選擇Target沒有公佈?

+0

UIControl目標未被釋放,因爲它未被保留。 – rmaddy

回答

0

bk_addEventHandler:forControlEvents:方法通過將BKControlWrapper實例添加到NSMutableSet來保留實例。該集由events字典保留,該字典由控件本身保留,因爲字典與使用OBJC_ASSOCIATION_RETAIN_NONATOMIC的控件相關聯。

重要的是,您傳遞給bk_addEventHandler:forControlEvents:的塊不會創建保留循環。您必須確保塊在需要時使用弱引用(如需引用視圖控制器而不是引用控件)。