title: Swift & UIView : How do we add an action to UIView via an extension?
body: <p>I can't add an action to my <code>UIView</code> when implementing in UIView <code>Extension</code>.</p>
  <p>UIView Extension</p>
  <pre><code>extension UIView {
  public func addAction(_ selector: Selector) {
          isUserInteractionEnabled = true
          let gesture = UITapGestureRecognizer(target: self, action: selector)
          self.addGestureRecognizer(gesture)
      }
  }
  </code></pre>
  <p>In function in ViewController</p>
  <pre><code>func setAction(_ button: UIView, _ selector: Selector?) {
          button.isUserInteractionEnabled = true
          let gesture = UITapGestureRecognizer(target: self, action: selector)
          button.addGestureRecognizer(gesture)
      }
  
  @objc func hello(){
    print(&quot;Hello&quot;)
  }
  </code></pre>
  <p>I have a UIView Controller called menu, and I want it to print out &quot;Hello&quot; when I press on it.</p>
  <p>If I do method 1, I will get an error.</p>
  <pre><code>Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView hello:]: unrecognized selector sent to instance 0x11fd4ca30'
  </code></pre>
  <p>if I do method 2, it works just fine.</p>
  <p>1 - <code>menu.addAction(#selector(hello))</code></p>
  <p>2 - <code>setAction(menu, #selector(hello))</code></p>
  <p>But I might be using this very often. How do we add an action to UIView via an extension?</p>