我正在使用下面的代码
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processTap)];
- (void) processTap
{
//do something
}
但我需要向 processTap 函数发送数据。有没有办法做到这一点?
我正在使用下面的代码
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processTap)];
- (void) processTap
{
//do something
}
但我需要向 processTap 函数发送数据。有没有办法做到这一点?
例子:
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);
}
在iOS - UITapGestureRecognizer - Selector with Arguments有一个类似问题的很好的答案,但是如果你想传递你不想附加到视图的数据,我建议创建第二个函数来访问你需要的数据. 例如:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(passDataToProcessTap)];
- (void)passDataToProcessTap {
[self processTapWithArgument:self.infoToPass];
// Another option is to use a static variable,
// Or if it's not dynamic data, you can just hard code it
}
- (void) processTapWithArgument:(id)argument {
//do something
}