views:

289

answers:

2

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];
}
+1  A: 

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.

Dan Ray
Thanks for the tip, though I'm not using Interface Builder for the view, it's done programatically.
Calvin L
Gotcha. Even more so, then, having a layer of abstraction between your controller and your UITextField is probably not a great idea.
Dan Ray
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. Gah! But still, thank you for the advice.
Calvin L
A: 

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.

Calvin L