Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

}

like image 351
Chlebta Avatar asked Sep 27 '22 18:09

Chlebta


1 Answers

Missing infoImage.userInteractionEnabled = YES;

like image 86
Misha Avatar answered Oct 24 '22 06:10

Misha