开发者

Can't resignFirstResponder with UITextView

开发者 https://www.devze.com 2023-01-02 06:49 出处:网络
I have a UITextView. I implemented a navigationBar UIBarButtonItem to respond to a touch and resign the firstResponder for my 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 canResignFirstRe开发者_开发百科sponder] == NO) {
        NSLog(@"I don't want to resign!");
    }

    [self.textInput.textView resignFirstResponder];
}


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.


Addressing your UITextField by way of a UIView subclass is a little non-standard. If I were you I'd get myself a direct handle on that UITextField right from inside your view controller. Set it up as a synthesized property of your view controller, give it the keyword IBOutlet so you can talk about it in Interface Builder, then make sure the outlet is hooked up to the text field.

In other words, rather than talking about self.textInput.textView, you want to be talking about self.textView directly. And double-check your IB outlet hookup, because I suspect that's where the real trouble is.


I figured it out. I had a 'UITextViewDelegate' method in my 'textInput' view, textViewShouldEndEditing that was overrided to return NO. Changed it to YES and it solved the problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号