iosswiftuiresponder

Event Delegation like in JavaScript, but in iOS Swift


Is there a way in Swift to capture events on a screen similar to how Event Delegation in JavaScript works? I would like to find a way to respond to events without having to embed logging calls to each method. I believe UIResponder might have this ability, but I it's not clear how to listen for all UIEvents.

For example:

extension UIResponder {
  func listenForAllButtonTaps() {
    // listen to all button taps, figure out the button and screen, log to logger
    
  }
}

@objc func buttonOne() { 
  // no logging method 
  ...
}

@objc func buttonTwo() { 
  ...
}

Solution

  • Seems method swizzling is the most common approach to this.