将参数传递给UITapGestureRecognizer

时间:2013-10-31 10:18:51

标签: ios ios6 uitapgesturerecognizer

我为视图创建了动态UIimage视图和UITapGestureRecognizer,看起来像这样

UIImageView *image = [[UIImageView alloc ] initWithFrame:CGRectMake(x, 0, 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方法可以有人告诉我该如何做到这一点非常感谢

2 个答案:

答案 0 :(得分:17)

您可以扩展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);

}

答案 1 :(得分:6)

传递发件人,

- (void)tapDetected:(UIGestureRecognizer *)sender
{
    NSLog(@"%@", sender.view);   // imageview 
}