Pass extra argument for UItapgestureRecognizer with selector
Short answer: no
The selector is called by the UITapGestureRecognizer
, and you have no influence on what parameters it passes.
However, what you can do is query the recognizer's view
property to get the same information.
func doubleTapComment(recognizer: UIGestureRecognizer) {
if recognizer.view == label1 {
...
}
else if recognizer.view == label2 {
...
}
}
Pass Parameter with UITapGestureRecognizer
One approach would be to subclass UITapGestureRecognizer and then set a property, I've posted an example below. You could also do some check on the sender and check if equal to some tag, class, string, e.t.c
class ViewController: UIViewController {
@IBOutlet weak var label1: UILabel!
@IBOutlet weak var image: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
image.userInteractionEnabled = true;
let tappy = MyTapGesture(target: self, action: #selector(self.tapped(_:)))
image.addGestureRecognizer(tappy)
tappy.title = "val"
}
func tapped(sender : MyTapGesture) {
print(sender.title)
label1.text = sender.title
}
}
class MyTapGesture: UITapGestureRecognizer {
var title = String()
}
There are lots of examples on SO, have a look, good luck.
How to pass additional parameter to UITapGestureRecognizer?
You can achieve this by creating your own TapGestureRecongnizer class which is the subclass of UITapGestureRecognizer. The example is shown below:
class MyTapGesture: UITapGestureRecognizer {
var floatValue = 0.0
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let tapped = MyTapGesture.init(target: self, action: #selector(handleTap))
tapped.floatValue = 5.0
tapped.numberOfTapsRequired = 1
view.addGestureRecognizer(tapped)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func handleTap(recognizer: MyTapGesture) {
print(recognizer.floatValue)
}
}
Passing arguments to selector in Swift
It looks like you're misunderstanding a couple of things.
When using target/action, the function signature has to have a certain form…
func doSomething()
or
func doSomething(sender: Any)
or
func doSomething(sender: Any, forEvent event: UIEvent)
where…
The
sender
parameter is the control object sending the action message.
In your case, the sender is the UITapGestureRecognizer
Also, #selector()
should contain the func signature, and does NOT include passed parameters. So for…
func handleTap(sender: UIGestureRecognizer) {
}
you should have…
let gesture = UITapGestureRecognizer(target: self, action: #selector(handleTap(sender:)))
Assuming the func and the gesture are within a view controller, of which modelObj
is a property / ivar, there's no need to pass it with the gesture recogniser, you can just refer to it in handleTap
Pass on Argument in #selector Swift 2.2
creating a generic UITapGestureRecognizer
instead use this:
class CustomTapGestureRecognizer: UITapGestureRecognizer {
var channelId: String?
}
also use this:
override func viewDidLoad() {
super.viewDidLoad()
let gestureRecognizer = CustomTapGestureRecognizer(target: self, action: #selector(tapped(_:))
gestureRecognizer.channelId = "Your string"
view1.addGestureRecognizer(gestureRecognizer)
}
func tapped(gestureRecognizer: CustomTapGestureRecognizer) {
if let channelId = gestureRecognizer.channelId {
//print
}
}
Pass multiple parameters to gesture recogniser
You could subclass the gesture recogniser you're using and add the extra variables you want. Example below.
class CustomGestureRecognizer : UITapGestureRecognizer {
var url : NSURL?
// any more custom variables here
init(target: AnyObject?, action: Selector, url : NSURL) {
super.init(target: target, action: action)
self.url = url
}
}
Then when you want to get the url back out.
func didTap(sender : CustomGestureRecognizer) {
print(sender.url)
}
How to pass an extra parameter
If the function has two parameters like below.
func clicked(sender:AnyObject,value:AnyObject)
{
}
Then
action = "clicked::"
example :
func switchCard(card: Int, withCard card1: Int)
{
print(card)
}
let singleTap1 = UITapGestureRecognizer(target: self, action: "switchCard:withCard:")
Just a note on Swift 2.2. You can now type the selector as
#selector(popoverSelectedCode(_:desc:)
Related Topics
How to Integrate .Proto Files in Xcode Compilation
How to Use Pod in Notification Service Extension
How to Call Objective-C Instancetype Method in Swift
How to Animate Uilabel's Textcolor Change
How to Prevent Duplicates in Realmswift List
Mfmailcomposeviewcontroller Without Mail App Installed
Can't Add a New iOS Version for My App
Default Uifont Size and Weight But Also Support Preferredfontfortextstyle
Swift - Weekday by Current Location by Currentcalendar()
Determine The Maximum Number of Characters a UIlabel Can Take
Swift - Add Same Navigation Bar Items to Every Page
Nstextattachment Image Not Shown in Nstextview (But in Uitextview)
How to Set Requestserializer in Alamofire
Random Glitchy Rendering of Svg on iOS/Wkwebview
iOS Wkwebview Swift JavaScript Enable
Swift: Iboutlets Are Nil in Custom Cell
Sprite Kit Collision Detection
Initialization of 'Unsafepointer<Int>' Results in a Dangling Pointer