addTarget(_:action:for:)
Associates a target object and action method with the control.
Declaration
func addTarget(_ target: Any?, action: Selector, for controlEvents: UIControl.Event)Parameters
- target:
The target object—that is, the object whose
actionmethod is called. If you specifynil, UIKit searches the responder chain for an object that responds to the specified action message and delivers the message to that object. - action:
A selector identifying the action method to be called. You may specify a selector whose signature matches any of the signatures in the code example in Uicontrol. This parameter must not be
nil. - controlEvents:
A bitmask specifying the control-specific events for which the action method is called. Always specify at least one constant. For a list of possible constants, see Event.
Mentioned in
Discussion
You may call this method multiple times to configure multiple targets and actions for the control. It is also safe to call this method multiple times with the same values for the target and action parameters. The control maintains a list of its attached targets and actions along with the events each supports.
The control does not retain the object in the target parameter. It is your responsibility to maintain a strong reference to the target object while it is attached to a control.
Specifying a value of 0 for the controlEvents parameter does not prevent events from being sent to a previously registered target and action method. To stop the delivery of events, always call the removeTarget(_:action:for:) method.