Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't resignFirstResponder with UITextView

I have a UITextView. I implemented a navigationBar UIBarButtonItem to respond to a touch and resign the firstResponder for my UITextView.

But, when the selector method is called, the keyboard doesn't get dismissed. I checked the UITextView's responder status with isFirstResponder and it returns YES. I also checked it with canResignFirstResponder and the return value is NO.

I must be missing something here...why is it returning NO?

I get that I can override canResignFirstResponder by subclassing UITextView, but I'd like to avoid that if possible.

Here's a code snippet:

- (void) commentCancelButtonTouched:(id)sender
{
    NSLog(@"Cancel button touched");
    [self.navigationBar popNavigationItemAnimated: NO];

    if ([self.textInput.textView canResignFirstResponder] == NO) {
        NSLog(@"I don't want to resign!");
    }

    [self.textInput.textView resignFirstResponder];
}
like image 218
Calvin Avatar asked Dec 28 '22 15:12

Calvin


1 Answers

Just in case anyone wants to hide the keyboard when you touch outside of the textview, it's pretty easy...

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];

    [self.xFront resignFirstResponder];
}

xFront is my outlet to my UITextView.

like image 95
JC Aulabaugh Avatar answered Dec 31 '22 04:12

JC Aulabaugh