快速将参数传递给#selector方法 [英] Passing arguments to #selector method in swift

查看:206
本文介绍了快速将参数传递给#selector方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to pass multiple argument to a function while I click an image. Here is my code

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
        }

如何传递参数,以便获得正确的值?

How can I pass arguments so that I can get correct values?

推荐答案

您不能.从文档中:

手势识别器具有一个或多个与之相关的目标动作对.
...
调用的操作方法必须符合以下签名之一:

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;

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

You may use instance variables to pass the parameters.

这篇关于快速将参数传递给#selector方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆