当textfield成为第一响应者时,手势识别器即使在辞职后也没有响应

时间:2014-01-26 19:26:32

标签: objective-c uitextfield first-responder resignfirstresponder becomefirstresponder

我可能错过了一些东西...... 首先,我从UITextField继承并为UITextField(在指定的初始化程序中)添加了一个Tap手势识别器:

UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pressed:)];
[self addGestureRecognizer:ges];


-(void)pressed:(id)sender
{
    didPressed = YES;
    [self becomeFirstResponder];
}

然后我将viewController设置为textField委托并实现:

- (BOOL)textField:(UIOneLetterTextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"Key Pressed %@", string);
    textField.text = string;
    [textField resignFirstResponder];
     UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:textField action:@selector(pressed:)];
    [textField addGestureRecognizer:ges];
    [self gotoNextTextfield:textField.cellLoc];
    return NO;
}

从这一点来看,由于某种原因,在点击textField时不会调用pressed:。 任何想法为什么?

2 个答案:

答案 0 :(得分:1)

代表应该实施 gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: 并返回YES。

答案 1 :(得分:0)

您使用UITapGestureRecognizer的具体原因是什么? UITextFieldDelegate有一个方法,只要textField开始编辑就会调用:

  • textFieldShouldBeginEditing:
  • textFieldDidBeginEditing:

除非您有一些与这些方法冲突的代码,否则您不需要UITapGestureRecognizer