我在我的应用程序中使用TPKeyboardAvoiding隐藏了显示键盘时的移动文本字段,但是当我尝试结束编辑文本字段时遇到了异常。它来自TPKeyboardAvoiding中的此方法:

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UIView* view =[self TPKeyboardAvoiding_findFirstResponderBeneathView:self];
    NSLog(@"%@",[view description]);
    [view resignFirstResponder]; //this line gives the exception
    [super touchesEnded:touches withEvent:event];
}

我在这里有点困惑。并非所有的UIView都响应resignFirstResponder吗?谢谢您的帮助。

完整错误:
2014-03-25 17:40:39.919 Rysk[5553:70b] -[MenuViewController textFieldDidBeginEditing:]: unrecognized selector sent to instance 0xb63c820

最佳答案

不知道您是否同时调用了[yourTextField resignFirstResponder] 。因此,此时的UITextField(在您提供的代码中)可能不是上的FirstResponder。我建议像这样调整您的代码:

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UIView* view =[self TPKeyboardAvoiding_findFirstResponderBeneathView:self];

    if([view conformsToProtocol:@protocol(UITextFieldDelegate)] || [view conformsToProtocol:@protocol(UITextViewDelegate)]) &&
       [view isFirstResponder] && [view canResignFirstResponder])
    {
       [view resignFirstResponder];
    }

    [super touchesEnded:touches withEvent:event];
}

另外,如果您使用的是POD,请确保您使用的是最新版本,因为在这种情况下,我使用的是这样的:
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self TPKeyboardAvoiding_findFirstResponderBeneathView:self] resignFirstResponder];
    [super touchesEnded:touches withEvent:event];
}

希望能帮助到你!

关于ios - 无法识别的选择器已发送到实例resignFirstResponder,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22624749/

10-10 21:05