pass parameter to UITapGestureRecognizer [duplicate]

你离开我真会死。 提交于 2019-11-30 08:36:07

问题


i have created dynamic UIimage view and and UITapGestureRecognizer to the view its look like this

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]; 

and my action method like this

-(void)tapDetected:(UIGestureRecognizer *)recognizer{
    NSLog(@"single Tap on imageview");

}

i want to pass a string parameters to tapDetected method can anyone tell me how to do this thank you very much


回答1:


You can extend a UITapGestureRecognizer for holding more data:

// 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);

}



回答2:


Pass the sender,

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


来源:https://stackoverflow.com/questions/19703767/pass-parameter-to-uitapgesturerecognizer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!