swift - 快速将参数传递给#selector 方法

标签 swift swift2 uitapgesturerecognizer

<分区>

我想在单击图像时将多个参数传递给函数。 这是我的代码

var param1 = 120
var param2 = "hello"
var param3 =  "world"

let image: UIImage = UIImage(named: "imageName")!
 bgImage = UIImageView(image: image)

let singleTap = UITapGestureRecognizer(target: self, action:#selector(WelcomeViewController.tapDetected(_:secondParam:thirdParam:)))
singleTap.numberOfTapsRequired = 1
bgImage!.userInteractionEnabled = true
bgImage!.addGestureRecognizer(singleTap)

调用函数

func tapDetected(firstParam: Int, secondParam: String, thirdParam: String) {
print("Single Tap on imageview")
            print(firstParam)  //print 120
            print(secondParam) // print hello
            print(thirdParam) /print world
        }

如何传递参数才能获得正确的值?

最佳答案

你不能。来自文档:

A gesture recognizer has one or more target-action pairs associated with it.
...
The action methods invoked must conform to one of the following signatures:

- (void)handleGesture;
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;

您可以使用实例变量来传递参数。

关于swift - 快速将参数传递给#selector 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38328956/

相关文章:

ios - Sqlite3 插入 BLOB 而不是文本

swift - Swift 元组

ios - Xcode 7 Debug模式是否有效?

swift - 两个 NSDate 的计算差异是错误的 - 已修复

swift - 当不在闭包中时,传递属于 self 的函数是否会导致保留循环?

ios - 当superview有手势时collectionView没有调用didSelectItemAtIndexPath

ios - 向 UILabel 的一部分添加点击手势

ios - 将 UITapGestureReconizer 添加到 UIImage

swift - 为什么indexPath和IndexPath都不被接受?

ios - 使用 Parse 时如何获取所有 Facebook 好友的列表?