ios - 无法识别的选择器发送到实例 resignFirstResponder

标签 ios objective-c resignfirstresponder tpkeyboardavoiding

我正在使用 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];
}

我在这里有点困惑。不应该所有的 UIViews 响应 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,请确保您使用的是最新版本,因为我使用的 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/

相关文章:

ios - 似乎我的应用程序正在以错误的顺序执行操作

ios - EXC_BAD_ACCESS w/OpenCV `cv::aruco::detectMarkers()` 在 IOS 上

iphone - 添加两个在文本字段中作为字符串输入的数字

objective-c - 为什么 appendAttributedString 在新行中显示附加字符串?

objective-c - 关闭商店 View 后在 App Purchase 中崩溃

ios - 从 PFQueryTableViewController 中删除 "Loading..."

ios - 是否可以向应用程序中的所有键盘添加完成键?

ios - UITextField 的 resignFirstResponder 既不工作也不工作 self.view.endEditing()

ios - 点击文本字段后如何立即隐藏键盘?

ios - 如何添加联系人自定义应用程序帐户,如下图所示