简体   繁体   English

为UIGestureRecognizer选择器操作传递参数

[英]Pass Parameter for UIGestureRecognizer Selector Action

I am aware (and have found several posts here on SO) that one cannot pass along any additional parameters for a selector. 我知道(并且在SO上发现了几个帖子),一个人无法传递选择器的任何其他参数。 For example whenever someone taps on my image view, I have the following taking place: 例如,每当有人点击我的图像视图时,我就会发生以下情况:

 imageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action:Selector("tapImage:")))

This works correctly, and many solutions say that if you wish to pass a parameter, simply assign the tag of the view to something, and then reference that as the sender within the tapImage function. 这很正常,许多解决方案都说如果你想传递一个参数,只需将视图的标签分配给某个东西,然后在tapImage函数中引用它作为发送者。 The thing is i'm actually using the tag value for something else, so would need to somehow store another value somewhere else. 事情是我实际上使用标签值为其他东西,所以需要以某种方式在其他地方存储另一个值。

What are some recommended ways I can pass a true/false (or 0/1) value into my TapGestureRecognizer action "tapImage" such that I can evaluate an expression? 我可以将一些真/假(或0/1)值传递给我的TapGestureRecognizer动作“tapImage”,以便我可以评估一个表达式? I also need to pass a collection of classes as well. 我还需要传递一组类。

I think the only solution is to use a different selector in this case (for example "tapImageFunctionA" vs. "tapImageFunctionB" which is fine, but before I go this route is there another way? Even with this way, I would need to access a collection of objects. Maybe I set a global variable in the view controller and access it that way? 我认为唯一的解决方案是在这种情况下使用不同的选择器(例如“tapImageFunctionA”与“tapImageFunctionB”这很好,但在我走这条路线之前还有另一种方法吗?即使这样,我也需要访问一组对象。也许我在视图控制器中设置一个全局变量并以这种方式访问​​它?

Thanks so much! 非常感谢!

  1. Use some tag formatting - for example bitmasks, like 16 lower bits for one kind of info / 16 higher bits for second kind of info 使用一些标签格式 - 例如位掩码,例如16种低位用于一种信息/ 16位高位用于第二种信息
  2. Use associative references . 使用关联引用 Here's some answers on these on stack overflow: How do I use objc_setAssociatedObject/objc_getAssociatedObject inside an object? 这里有关于堆栈溢出的一些答案: 如何在对象中使用objc_setAssociatedObject / objc_getAssociatedObject? , Any gotchas with objc_setAssociatedObject and objc_getAssociatedObject? 任何与objc_setAssociatedObject和objc_getAssociatedObject的陷阱? .

Personally i would use the first solution with tags, to reduce performance overhead on associative references (it's VERY minor, but it exists). 我个人会使用第一个带标签的解决方案,以减少关联引用的性能开销(它非常小,但它存在)。 Also, tags seems like more "programming" solution, when associative references - more technical and implementation-dependent. 此外,当关联引用 - 更多技术和实现依赖时,标签似乎更像“编程”解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为UIGestureRecognizer动作传递参数 - How to pass arguments for a UIGestureRecognizer action 查找UIGestureRecognizer动作(选择器)的名称和目标 - Find UIGestureRecognizer action (selector) name and target 创建UIGestureRecognizer时,为什么其操作使用#selector? - When creating a UIGestureRecognizer, why does its action use a #selector? action:@selector(showAlert :)如何在这个showAlert方法中传递参数? - action:@selector(showAlert:) how to pass parameter in this showAlert method? 在UIButton的选择器上传递参数 - Pass parameter on selector of UIButton UIGestureRecognizer initWithTarget设置为类ID(UIViewController)的对象会导致编译器警告未声明的操作选择器 - UIGestureRecognizer initWithTarget set to object of class id (UIViewController) results in compiler warning for undeclared action selector 在iOS中获取UIGestureRecognizer的操作 - Getting the Action of UIGestureRecognizer in iOS 具有setDelegate和action的UIGestureRecognizer - UIGestureRecognizer with both setDelegate and action InterfaceBuilder目标/操作中的UIGestureRecognizer - UIGestureRecognizer in InterfaceBuilder target/action 斯威夫特:除了使用button.tag之外,还有一种更干净的方法来传递额外的UIBarButtonItem操作:Selector参数吗? - Swift: Is there a cleaner way to pass an extra UIBarButtonItem action: Selector parameter other than using button.tag?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM