sending a parameter argument to function through UITapGestureRecognizer selector

I am making an app with a variable amount of views all with a TapGestureRecognizer. When the view is pressed, i currently am doing this

func addView(headline: String) {
    // ...
    let theHeadline = headline
    let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
    // ....

but in my function “handleTap”, i want to give it an additional parameter (rather than just the sender) like so

  • How can I pause and resume NSTimer.scheduledTimerWithTimeInterval in swift?
  • UnsafeMutablePointer<Int8> from String in Swift
  • Saving Already Created Live Photos
  • Add button at the end of collection view in storyboard
  • UISwitch not deallocating
  • Sync data between two viewcontrollers to avoid creating same observer again
  • func handleTap(sender: UITapGestureRecognizer? = nil, headline: String) {

    How do i send the specific headline (which is unique to every view) as an argument to the handleTap-function?

    Solutions Collect From Internet About “sending a parameter argument to function through UITapGestureRecognizer selector”

    Instead of creating a generic UITapGestureRecognizer, subclass it and add a property for the headline:

    class MyTapGestureRecognizer: UITapGestureRecognizer {
        var headline: String?

    Then use that instead:

    override func viewDidLoad() {
        let gestureRecognizer = MyTapGestureRecognizer(target: self, action: "tapped:")
        gestureRecognizer.headline = "Kilroy was here."
    func tapped(gestureRecognizer: MyTapGestureRecognizer) {
        if let headline = gestureRecognizer.headline {
            // Do fun stuff.

    I tried this. It worked great.