// add target/action for particular event. you can call this multiple times and you can specify multiple target/actions for a particular event. // passing in nil as the target goes up the responder chain. The action may optionally include the sender and the event in that order // the action cannot be NULL. Note that the target is not retained. - (void)addTarget:(nullableid)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
Note that the target is not retained.新发现,原来UIControl并不会强引用target。所以self addTarget:self,并不会引起内存问题。不过这跟我们现在的问题没啥关系。
removeTarget:
1 2
// remove the target/action for a set of events. passin NULL for the action to remove all actions for that target - (void)removeTarget:(nullable id)targetaction:(nullable SEL)action forControlEvents:(UIControlEvents)controlEvents;
文档说明:
1 2
Use this methodto prevent the delivery of control events to target objects associated with control. If you specify a validobjectin the target parameter, this method stops the delivery of the specified events toall action methods associated with that object. If you specify nil for the target parameter, this method prevents the delivery of those events toall action methods ofall target objects. Although the action parameter isnot considered when stopping the delivery of events, you should specify an appropriate value anyway. If the specified target/action combination no longer has anyvalid control events associated with it, the control cleans up its corresponding internal data structures. Doing so can affect the setof objects returned by the allTargets method.
里面有一句重要说明:
If you specify a valid object in the target parameter, this method stops the delivery of the specified events to all action methods associated with that object.If you specify nil for the target parameter, this method prevents the delivery of those events to all action methods of all target objects.