Currently I have in my code
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addCompoundToLabel:)];
My code would be cleaner if it was something like
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addCompoundToLabel:) withObject:data];
I read that associated objects allow you to pass objects in a gesture recognizer, but after reading the docs, I don’t quite understand how to implement it into my code. An example implementation of associated objects would be appreciated. Thanks
EDIT:
this is what the beginning of addCompound looks like
- (void)addCompoundToLabel:(UIGestureRecognizer *)recognizer {
if( [recognizer state] == UIGestureRecognizerStateEnded ) {
FormulaLabel* label = (FormulaLabel*)[recognizer view];
Using associated objects is a messy solution. Instead, you could create a false target object, give it your
UIViewand yourData, and have access to both of them in the selector method. It is a lot more readable, and your code expresses your intent a lot better.Here is a quick and dirty example:
Add
processTapFromSender:withData:method to your controller:Now you can create your tap recognizer like this: