UITextField并在leftView中点击手势

时间:2015-06-01 14:03:07

标签: ios objective-c cocoa-touch

我在textField的leftView上有一个图标,我想为它添加点按手势。这是我的代码:

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

当用户点击leftview上的图片时,问题就出现了,并且没有调用showErrorMessage

更新 通过在infoImage上启用userInteraction来解决tap问题。
但现在我在我的节目的第二行[UITapGestureRecognizer tag]: unrecognized selector sent to instance获得了errorMessage

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

}

2 个答案:

答案 0 :(得分:2)

缺少infoImage.userInteractionEnabled = YES;

答案 1 :(得分:0)

您必须将图像的属性userInteractionEnabled设置为TRUE