[英]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! 非常感谢!
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.