Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle tap gesture with an argument iphone / ipad

My problem is similar to this one with the only exception - my ImageView appears at the same place inside the window with different content in it. Content has unique identifier which I want to use to call content-specific actions.

To quickly recap, the guy is looking for a way to pass a parameter into the selector section of the initWithTarget method.

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(handleTapGesture:itemSKU:)];

How can I pass an attribute to the handleTapGesture method or how do I read the unique value otherwise?

Any thoughts appreciated.

EDIT: The content is being pulled from a database and is different every time. The unique identifier is pretty much like an SSN - doesn't repeat.

like image 752
Rakhilya Lála Ibildayeva Avatar asked Dec 11 '25 09:12

Rakhilya Lála Ibildayeva


1 Answers

You could set the UIImageView tag property with your content identifier, and then read that information form the selector.

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(handleTapGesture:)];

[imageView addGestureRecognizer:tapGesture];
imageView.tag = 0;

And then:

- (void)handleTapGesture:(UITapGestureRecognizer *)sender
{
    if( ((UIImageView *) sender.view).tag == 0 ) // Check the identifier
    {
        // Your code here
    }
}
like image 125
Xavi Gil Avatar answered Dec 12 '25 21:12

Xavi Gil