参见英文答案 >
Pass a NSDictionary as parameter to UITapGestureRecognizer3个
我已经为视图创建了动态UIimage视图和UITapGestureRecognizer,它看起来像这样
我已经为视图创建了动态UIimage视图和UITapGestureRecognizer,它看起来像这样
UIImageView *image = [[UIImageView alloc ] initWithFrame:CGRectMake(x,200,150)]; NSString *ImageURL = [str objectForKey:@"imageLink"]; NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]]; image.image = [UIImage imageWithData:imageData]; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)]; singleTap.numberOfTapsrequired = 1; image.userInteractionEnabled = YES; [image addGestureRecognizer:singleTap]; [documentory addSubview:image];
和我这样的动作方法
-(void)tapDetected:(UIGestureRecognizer *)recognizer{ NSLog(@"single Tap on imageview"); }
我想传递一个字符串参数到tapDetected方法可以有人告诉我如何做到这一点非常感谢你
解决方法
您可以扩展UITapGestureRecognizer以保存更多数据:
// MYTapGestureRecognizer.h @interface MYTapGestureRecognizer : UITapGestureRecognizer @property (nonatomic,strong) NSString *data; @end // MYTapGestureRecognizer.m @implementation MYTapGestureRecognizer @end // ===================== .... MYTapGestureRecognizer *singleTap = [[MYTapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)]; singleTap.data = @"Hello"; ..... // ==================== -(void)tapDetected:(UITapGestureRecognizer *)tapRecognizer { MYTapGestureRecognizer *tap = (MYTapGestureRecognizer *)tapRecognizer; NSLog(@"data : %@",tap.data); }