views:

41

answers:

1

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.

+1  A: 

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.
    }
}
Noah Witherspoon
at least I can stop banging my head on passing more than 1 argument - thanks! But I am still left in the same spot, trying to figure out something to identify which image was clicked. I know an image was clicked already because it's the only thing in that view responding to the tap, just don't know which one.
Slee
sorry clicked = tapped in the above
Slee
is there a way to get an index of where the view is in the superview? Say I added 40 UIImageViews and the send was 28 - that would work perfectly for me.
Slee
In the code I provided above, touchedView get set to the image view that was tapped. You might set each view's "tag" property to a distinct number when you create it, then retrieve the tag in the above method and index that against your array of SKUs.
Noah Witherspoon
DOH - sweet! I saw that I could set a tag to an int, just didn't click in my head - thanks!
Slee