What is a nil-targeted action for in Swift?

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?


Why wouldn't you specify the target?

Can you be more specific? The target of what? And does is have anything to do with climbing the UIResponder hierarchy?
No target, or removing all targets, is a way to deactivate the control.

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

So an example would be something like this:

It gets a little bit clearer, but not enough.

addTarget(_:action:for:)

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.

Do you want to know how the responder chain works? Or why such a mechanism like the responder chain exists in iOS?

Do you want to know how the responder chain works? Or why such a mechanism like the responder chain exists in iOS?

Sounds like that's what I'll have to do to understand this.
What is a nil-targeted action for in Swift?
 
 
Q