0

I currently have this in my code

tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(updateLeftLabel:)];

which calls

- (void)updateLeftLabel:(UIGestureRecognizer*)recognizer 

My code would be cleaner if the method was

- (void)updateLeftLabel:(UIGestureRecognizer*)recognizer fromData:(EquationData*)data

However, I need a way to send the 'data' parameter when I init UITapGestureRecognizer

Mahir
  • 1,684
  • 5
  • 31
  • 59

2 Answers2

4

Why Complicate ??? Subclass UITapGestureOrganiser and use any number of custom objects in the new class.

infiniteLoop
  • 2,135
  • 1
  • 25
  • 29
0

You can use associated objects to attach data to the gesture recognizer.

Costique
  • 23,712
  • 4
  • 76
  • 79
  • I looked at http://stackoverflow.com/questions/2846218/how-do-i-use-objc-setassociatedobject-objc-getassociatedobject-inside-an-object – Mahir Feb 07 '12 at 03:11
  • but I'm having trouble understanding the implementation. Do you mind further explaining how I can add associated objects to my code? – Mahir Feb 07 '12 at 03:12
  • The cleaner API you proposed suggests that `data` is ready by the time the gesture recognizer fires. Just associate `data` with `tap`, and you'll be able to retrieve `data` in `updateLeftLabel:`. – Costique Feb 07 '12 at 05:10