unrecognized selector sent to instance resignFirstResponder

2k Views Asked by At

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. It's coming from this method in 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? Thanks for the help.

Full error:

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

There are 4 best solutions below

0
Connor Pearson On BEST ANSWER

I solved the problem by having my view controller than contains the TPKeyboardAvoidingScrollView implement the UITextFieldDelegate protocol. Most importantly, these two methods:

- (void)textFieldDidBeginEditing:(UITextField *)textField{}
- (void)textFieldDidEndEditing:(UITextField *)textField{}
3
Sujith Thankachan On

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];
}
2
Rajesh Loganathan On

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];
}
4
NeverHopeless On

Not sure if you have called [yourTextField resignFirstResponder] as well. So it is possible that the UITextField (in the code you have provided) is not the FirstResponder at that point. 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:

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

Hope it helps!