6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

UITapGestureRecognizerにパラメーターを送りたい

Posted at

In this post, I send String parameter to Gesture, but actually you can send all types of parameter.

#Purpose
Personal Note

#Development Environment

  • OS X El Captain 10.11.2
  • Xcode Version 8.0

#Language
Swift 3.0

1.You have to create class

class MyTapGestureRecognizer: UITapGestureRecognizer {
    var targetString: String?
}

2.Call that class

let gesture = MyTapGestureRecognizer(target: self, action: #selector(self.tapText(tap:)))
gesture.targetString = "Hello Qiita"

func tapText(sender: MyTapGestureRecognizer) {
     print(sender.targetString)
}

Source Code

ViewController.swift
import UIKit

class MyTapGestureRecognizer: UITapGestureRecognizer {
    var targetString: String?
}

class ViewController: UIViewController {
   
    @IBOutlet weak var label: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let gesture = MyTapGestureRecognizer(target: self, action: #selector(self.tapText(sender:)))
        gesture.targetString = "Hello Qiita"
        label.addGestureRecognizer(gesture)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

/* Function */
extension ViewController{
    func tapText(sender: MyTapGestureRecognizer) {
        print(sender.targetString!)
    }
}
6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?