title: Pass parameters to button action in swift
body: <p>When setting the action with the &quot;addTarget&quot; method on a button in Swift, is there a way for me to pass a parameter to the function I want to trigger?
  Say I had a simple scenario like this:</p>
  <pre><code>let button = UIButton()
  button.addTarget(self, action: #selector(didPressButton), for: .touchUpInside)
  
  @objc func didPressButton() {
      // do something
  }
  </code></pre>
  <p>Obviously the above code works fine, but say I wanted the 'didPressButton' function to take in a parameter:</p>
  <pre><code>@objc func didPressButton(myParam: String) {
      // do something with myParam
  }
  </code></pre>
  <p>Is there a way I can pass a parameter into the function in the 'addTarget' method?
  Something like this:</p>
  <pre><code>let button = UIButton()
  button.addTarget(self, action: #selector(didPressButton(myParam: &quot;Test&quot;)), for: .touchUpInside)
  
  @objc func didPressButton(myParam: String) {
      // do something with myParam
  }
  </code></pre>
  <p>I'm coming from a JavaScript background and in JavaScript it's pretty simple to achieve this behavior by simply passing an anonymous function that would then call the 'didPressButton' function. However, I can't quite figure how to achieve this with swift. Can a similar technique be used by using a closure? Or does the '#selector' keyword prevent me from doing something like that?</p>
  <p>Thank you to anyone who can help!</p>