简体   繁体   English

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

[英]unrecognized selector sent to instance resignFirstResponder

I'm using TPKeyboardAvoiding in my app to hide move text fields when the keyboard is showing, but I'm getting an exception when I try to end editing the text field. 我在我的应用程序中使用TPKeyboardAvoiding来显示键盘时隐藏移动文本字段,但是当我尝试结束编辑文本字段时遇到异常。 It's coming from this method in 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];
}

I'm a bit confused here. 我在这里有点困惑。 Shouldn't all UIViews respond to resignFirstResponder ? 不是所有的resignFirstResponder都响应resignFirstResponder吗? Thanks for the help. 谢谢您的帮助。

Full error: 完整错误:

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

Not sure if you have called [yourTextField resignFirstResponder] as well . 不知道如果你调用[yourTextField resignFirstResponder] 为好 So it is possible that the UITextField (in the code you have provided) is not the FirstResponder at that point . 因此,此时的UITextField(在您提供的代码中) 可能不是FirstResponder I would suggest to adjust your code like this: 我建议像这样调整您的代码:

- (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];
}

Also, if you using PODs please make sure you are using a latest version, because the one i am using has something like this in this event: 另外,如果您使用的是POD,请确保您使用的是最新版本,因为在这种情况下,我使用的是这样的:

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self TPKeyboardAvoiding_findFirstResponderBeneathView:self] resignFirstResponder];
    [super touchesEnded:touches withEvent:event];
}

Hope it helps! 希望能帮助到你!

Update your code as follows: 如下更新代码:

- (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];
}

Try this simple method.... 试试这个简单的方法。

- (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];
}

I solved the problem by having my view controller than contains the TPKeyboardAvoidingScrollView implement the UITextFieldDelegate protocol. 我通过使视图控制器比包含TPKeyboardAvoidingScrollView的UITextFieldDelegate协议实现来解决了该问题。 Most importantly, these two methods: 最重要的是,这两种方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM