0

So i was reading the description about UITextView's and it says that it automatically hides the keyboard when you press the 'Return' button on the keyboard. But it wasn't working, so I tried creating an

- (IBAction)textViewReturn:(id)sender; 
{
   [myTextView resignFirstResponder];
}

That did not work either so i tried also doing:

- (BOOL)textViewShouldReturn:(UITextView *)textView
{
  [myTextView resignFirstResponder];
  return NO;
} 

Not sure why the whole deal isn't working in the first place. Wondering if anyone could help?

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
trludt
  • 1,801
  • 2
  • 13
  • 15
  • Keep in mind that textView's *typically* are for allowing users to enter multiple lines of text, in which case you need the Return button to not resign the keyboard, but instead to insert a newline. If you only want one line of text, then a textField may be a better way to go. – lnafziger Dec 17 '12 at 04:16
  • Yes so how do you suggest finding a way to hide the keyboard in that situation? @lnafziger – trludt Dec 17 '12 at 05:22

3 Answers3

2

I don't see anything in the UITextView Class Reference that says it automatically hides the keyboard when you press Return.

Also, there is no textViewShouldReturn: message in the UITextViewDelegate protocol. There is a textFieldShouldReturn: message in the UITextFieldDelegate protocol, but a text view is not a text field.

If you want it to hide the keyboard when the user presses Return, you need to do two things.

First, you need to connect some object - usually your view controller - to the text view's delegate outlet. You can do that in your nib, or you can do it in code, perhaps in your viewDidLoad method:

- (void)viewDidLoad {
    [super viewDidLoad];
    myTextView.delegate = self;
}

Second, you need to implement the textView:shouldChangeTextInRange:replacementText: in your delegate object:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    } else {
        return YES;
    }
}

Note that if the user pastes in text containing a newline and other characters, this will not catch the newline. It will only notice when the user either taps the Return key, or when he pastes in text containing just a newline.

You can declare the delegate's class as conforming to the UITextViewDelegate protocol, in which case Xcode will helpfully autocomplete the method name. But it will work even if the class doesn't conform to the protocol.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • I typed in both of those sections of code.. didn't change anything.. how else can i connect some object to the text view's delegate outlet? @robmayoff – trludt Dec 17 '12 at 04:28
  • In your nib, you can control-drag from the File's Owner to the text view and choose the `delegate ` outlet. You also need to make sure `myTextView` is actually hooked up to your text view. – rob mayoff Dec 17 '12 at 04:37
  • is my nib the storyboard? @robmayoff – trludt Dec 17 '12 at 04:44
  • In a storyboard, you need to control-drag from the view controller placeholder (in the bar under the view) to the text view. – rob mayoff Dec 17 '12 at 04:45
  • I have already connected the textView in the storyboard to the property (weak, nonatomic) IBOutlet UITextView *myTextView; line.. is that what you mean? @robmayoff – trludt Dec 17 '12 at 05:10
  • It's not working... am i missing something in my header file you think? all i have is the (at sign)interface SecondViewController : UIViewController @robmayoff – trludt Dec 17 '12 at 05:23
  • Your comment says `UITextFieldDelegate`. Are you using a `UITextField` or a `UITextView`? They are different. – rob mayoff Dec 17 '12 at 06:48
1

Return button of UITextView is used to mote the cursor to the new line. But if you want to remove the keyboard on return button then please try the following code. It resigns the keyboard when return button is pressed by user. So try following code which definitely solved your problem.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    } else {
        return YES;
    }
}
Girish
  • 4,692
  • 4
  • 35
  • 55
  • in this section of code.. since i named my text view as myTextView. do i replace every time you put textView, with myTextView? @Girish – trludt Dec 17 '12 at 04:33
  • No need of it. This method has a parameter of textView which is current textView. Only you need to set delegate to the each texview in xib. – Girish Dec 17 '12 at 04:35
  • I am so confused right now. I've written out that entire section.. nothing changed... @Girish – trludt Dec 17 '12 at 05:15
  • Set the debugger on the delegate method of textview, if you are setting wrong delegates then debugger comes. – Girish Dec 17 '12 at 05:19
1

Per your comment, if you want to use a UITextField instead of a UITextView, then things remain the same except that in order to hide the keyboard when you hit return, you need to implement the following function in the text field's delegate (make sure that you have set this first):

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
lnafziger
  • 25,760
  • 8
  • 60
  • 101