3

I am aware (and have found several posts here on SO) that one cannot pass along any additional parameters for a selector. 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. 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? 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?

Thanks so much!

NullHypothesis
  • 4,286
  • 6
  • 37
  • 79

1 Answers1

0
  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
  2. Use associative references. Here's some answers on these on stack overflow: How do I use objc_setAssociatedObject/objc_getAssociatedObject inside an object?, Any gotchas with objc_setAssociatedObject and 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.

Community
  • 1
  • 1
Petro Korienev
  • 4,007
  • 6
  • 34
  • 43