49

I have a series of imageviews that I identify using their tag. I have added a single tap gesture to the images.

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectImage:)];
[tableGridImage addGestureRecognizer:singleTap];
tableGridImage.userInteractionEnabled = YES;
[singleTap release];

This manages to call the selectImage selector ok, but passes the gesture as the sender. I need the imageview as the sender so I can get the tag.

Any ideas on how I can get the imageview and it's tag?

rekire
  • 47,260
  • 30
  • 167
  • 264
dysan819
  • 1,031
  • 1
  • 8
  • 6

4 Answers4

54

I figured out how to get the tag, which was the most important part of the question for me. Since the gesture is the sender, I figured out the the view it is attached to is sent along with it:

[(UIGestureRecognizer *)sender view].tag

I am still curious if anyone can tell me how to send an argument through a UITapGestureRecognizer selector.

dysan819
  • 1,031
  • 1
  • 8
  • 6
  • 1
    I am still curious if anyone can tell me how to send an argument through a UITapGestureRecognizer selector. Me too. – Adam Waite Feb 22 '13 at 14:01
  • 1
    Adam I'm pretty sure that's the only way to do it, via the tag on the UIImageView. You know, there's some positives to using the tag on the image itself: imagine if for example you had more than one gesture recogniser, or, you were getting that info about the image in some other way. It's good that it is on the image, rather than on the G.R. It's a great tip thanks for the great question/answer!! – Fattie Dec 09 '13 at 23:38
  • Thank you! This helped me too! – Chen Li Yong Apr 28 '16 at 02:10
28

The only argument you can send through UITapGestureRecognizer selector is the UITapGestureRecognizer itself as following:

Make sure to put ":" after the selector name like you previously did :

UITapGestureRecognizer *singleTap = 
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectImage:)];

Then add a parameter to selectImage so you can retrieve the View as following:

-(void) selectImage:(UITapGestureRecognizer *)gestureRecognizer{

    //Get the View
    UIImageView *tableGridImage = (UIImageView*)gestureRecognizer.view;
}
Samidjo
  • 2,315
  • 30
  • 37
  • 2
    Thanks for your explanation, Samidjo! As an additional note, I've found it useful to add my own property on the view - `@property(weak, nonatomic) id relatedObject` . This way, you can effectively pass in whatever you want by assigning it to the property of the view! :D – JRG-Developer Dec 13 '12 at 21:49
4

From @dysan819 answer, I manage to get object without tag. In my case is UILabel.

- (void)labelTap:(id)sender {
    NSLog(@"tap class: %@", [[(UIGestureRecognizer *)sender view] class]);
    if ([[(UIGestureRecognizer *)sender view] isKindOfClass:[UILabel class]]) {
        UILabel *lb = (UILabel*)[(UIGestureRecognizer *)sender view];
        NSLog(@"tap: %@", lb.text);
    }
}
2

If you need distinct functionality for the handler you might check out the BlocksKit project and this file in particular. The project is a CocoaPods project so you can install it easily into your toolchain.

An example from the first referenced code file:

UITapGestureRecognizer *singleTap = [UITapGestureRecognizer recognizerWithHandler:^(id sender) {
     NSLog(@"Single tap.");
 } delay:0.18];
 [self addGestureRecognizer:singleTap];

This could effectively allow you to setup a gesture recognizer easily for each image.

Norman H
  • 2,248
  • 24
  • 27