How to pass argument to action in UIGestureRecognizer initWithTarget action

mommomonthewind picture mommomonthewind · Jun 22, 2012 · Viewed 14.8k times · Source

I am using the code below

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processTap)];

- (void) processTap
{
//do something
}

But I need to send a data to processTap function. Is there anyway to do this?

Answer

Thunderbird picture Thunderbird · Nov 28, 2012

Example:

UIImageView *myPhoto = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"tab-me-plese.png"]];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processTap:)];

[myPhoto setTag:1]; //set tag value
[myPhoto addGestureRecognizer:tap];
[myPhoto setUserInteractionEnabled:YES];

- (void)processTap:(UIGestureRecognizer *)sender
{
    NSLog(@"tabbed!!");
    NSLog(@"%d", sender.view.tag);    
}