1

我在我的应用程序中使用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
4

4 回答 4

1

不知道你是否也打过[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];
}

希望能帮助到你!

于 2014-03-25T06:01:34.653 回答
0

更新您的代码如下:

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UIView* view =[self TPKeyboardAvoiding_findFirstResponderBeneathView:self];
    NSLog(@"%@",[view description]);
    if([view isKindOfClass:[UITextField class]]) {
        UITextField *myTextField = (UITextField *)view;
        [myTextField resignFirstResponder];
    }
    [super touchesEnded:touches withEvent:event];
}
于 2014-03-25T03:13:29.857 回答
0

我通过让我的视图控制器比包含 TPKeyboardAvoidingScrollView 实现 UITextFieldDelegate 协议解决了这个问题。最重要的是,这两种方法:

- (void)textFieldDidBeginEditing:(UITextField *)textField{}
- (void)textFieldDidEndEditing:(UITextField *)textField{}
于 2014-03-27T04:57:21.643 回答
0

试试这个简单的方法......

- (void)viewDidLoad
{
    [super viewDidLoad];

    //--Gesture to resign keyborad on touch outside
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    tap.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tap];
}


//-- Resign keyboard on touch UIView
-(void)dismissKeyboard
{
    self.view.frame = CGRectMake(self.view.frame.origin.x, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self.view endEditing:YES];
}
于 2014-03-25T04:00:56.120 回答