iosobjective-ccocoa-touch

UITextField and tap gesture in leftView


I've an icon on leftView of textField and I want to add tap gesture to it. This is my code :

if (!textField.leftView){
    UIImageView *infoImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 18)];
    infoImage.image = [UIImage imageNamed:@"info"];
    infoImage.contentMode = UIViewContentModeCenter;
    textField.leftView = infoImage;
    textField.leftViewMode = UITextFieldViewModeAlways;
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showErrorMessage:)];
    tapRecognizer.numberOfTapsRequired = 1;
    [infoImage addGestureRecognizer:tapRecognizer];
}

The problem when user click on the image on leftview nothing is happend and the showErrorMessage is not called .

Update The tap issue was solved by enabling userInteraction on the infoImage.
But now I'm getting [UITapGestureRecognizer tag]: unrecognized selector sent to instance at second line of my Show errorMessage :

-(void)showErrorMessage:(id)sender{
UIImageView *icon = (UIImageView *)sender;
NSString *key = [@(icon.tag) stringValue];
NSLog(@"%@", [_errorDictionary objectForKey:key]);

}


Solution

  • Missing infoImage.userInteractionEnabled = YES;