Reputation: 28248
when my tap gesture fires I need to send an additional argument along with it but I must be doing something really silly, what am I doing wrong here:
Here is my gesture being created and added:
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:itemSKU:)];
tapGesture.numberOfTapsRequired=1;
[imageView setUserInteractionEnabled:YES];
[imageView addGestureRecognizer:tapGesture];
[tapGesture release];
[self.view addSubview:imageView];
Here is where I handle it:
-(void) handleTapGesture:(UITapGestureRecognizer *)sender withSKU: (NSString *) aSKU {
NSLog(@"SKU%@\n", aSKU);
}
and this won't run because of the UITapGestureRecognizer init line.
I need to know something identifiable about what image was clicked.
Upvotes: 4
Views: 8246
Reputation: 57149
A gesture recognizer will only pass one argument into an action selector: itself. I assume you're trying to distinguish between taps on various image subviews of a main view? In that case, your best bet is to call -locationInView:
, passing the superview, and then calling -hitTest:withEvent:
on that view with the resulting CGPoint
. In other words, something like this:
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)];
...
- (void)imageTapped:(UITapGestureRecognizer *)sender
{
UIView *theSuperview = self.view; // whatever view contains your image views
CGPoint touchPointInSuperview = [sender locationInView:theSuperview];
UIView *touchedView = [theSuperview hitTest:touchPointInSuperview withEvent:nil];
if([touchedView isKindOfClass:[UIImageView class]])
{
// hooray, it's one of your image views! do something with it.
}
}
Upvotes: 12