Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITapGestureRecognizer selector, sender is the gesture, not the ui object

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?

like image 442
dysan819 Avatar asked May 21 '11 14:05

dysan819


People also ask

What is gesture recognizer in Swift?

Overview. A gesture recognizer decouples the logic for recognizing a sequence of touches (or other input) and acting on that recognition.

How to Add tap gesture recognizer to UIView Swift?

Adding a Tap Gesture Recognizer to an Image View in Interface Builder. Open Main. storyboard and drag a tap gesture recognizer from the Object Library and drop it onto the image view we added earlier. The tap gesture recognizer appears in the Document Outline on the left.

What is gesturerecognizer?

A UIGestureRecognizer acts as an abstract base class for various sub-classes in the UIKit - commonly known as gesture recognizers. Developers can use existing gesture recognizers to support responses to a set of common gestures, or create additional gesture recognizers.


2 Answers

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.

like image 183
dysan819 Avatar answered Oct 04 '22 17:10

dysan819


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; } 
like image 33
Samidjo Avatar answered Oct 04 '22 19:10

Samidjo