Question

I am creating several UIView's which I add a UITapGestureRecognizer with an action thats suppose to call a method in the viewController, but it doesn't seem to work.

-(void)setupFreeMoneyView {
    NSArray *array = @[@"Facebook", @"Twitter", @"SMS", @"Email"];
    int index = 0;
    for (NSString *str in array) {
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100*index++, 0, 100, 100)];
        [imgView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@Logo", str]]];
        [imgView addGestureRecognizer:[[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:NSSelectorFromString([NSString stringWithFormat:@"postTo%@", str])]];
        [freeView addSubview:imgView];
    }
}

freeView is a UIView created in the MainStoryboard.

Ive checked if its the NSSelectorFromString() by using a normal @selector() thats not working but it isn't the cause.

It does work if I change the line [imgView addGestureRecognizer:... to [freeView addGestureRecognizer:...]

I just don't seem to understand why this is happening.

Was it helpful?

Solution

try this

imgView.userInteractionEnabled = YES ;

userInteractionEnabled of UIImageView is set to NO by default.

OTHER TIPS

maybe you need set userInteractionEnabled = YES for the UIImageView

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top