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!)
}
}