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

时间:2014-03-25 03:10:17

标签: 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];
}
我在这里有点困惑。难道不是所有的UIView都会回复resignFirstResponder吗?谢谢你的帮助。

完整错误:

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

4 个答案:

答案 0 :(得分: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];
}

希望它有所帮助!

答案 1 :(得分: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];
}

答案 2 :(得分: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];
}

答案 3 :(得分:0)

我通过使用我的视图控制器而不是包含TPKeyboardAvoidingScrollView实现UITextFieldDelegate协议来解决问题。最重要的是,这两种方法:

- (void)textFieldDidBeginEditing:(UITextField *)textField{}
- (void)textFieldDidEndEditing:(UITextField *)textField{}