As far as I understand, a UIControl subclass maintains an internal dispatch table for the target-action pairs. When a user event occurs, a control finds the appropriate target and its matching action to call. And it does this by climbing the UIResponder hierarchy until it finds the right target and the action. Why wouldn't you specify the target? Is it so that the control becomes reusable?
What is a nil-targeted action for in Swift?
Can you be more specific? The target of what? And does is have anything to do with climbing the UIResponder hierarchy?Why wouldn't you specify the target?
No target, or removing all targets, is a way to deactivate the control.
As stated in doc:
As stated in doc:
To stop the delivery of events, always call the removeTarget(_:action:for:) method.
So an example would be something like this:
Code Block swift override func awakeFromNib() { super.awakeFromNib() class SomeClass { @objc func buttonPressed(_:Any) {} } self.addTarget(nil, action: #selector(SomeClass.buttonPressed), for: .valueChanged) }
@Ovis
addTarget(_:action:for:)
It gets a little bit clearer, but not enough.So an example would be something like this:
addTarget(_:action:for:)
Do you want to know how the responder chain works? Or why such a mechanism like the responder chain exists in iOS?target
The target object—that is, the object whose action method is called. If you specify nil, UIKit searches the responder chain for an object that responds to the specified action message and delivers the message to that object.
Sounds like that's what I'll have to do to understand this.Do you want to know how the responder chain works? Or why such a mechanism like the responder chain exists in iOS?