iphoneobjective-ckeyboarduitextviewiphone-softkeyboard

How to dismiss keyboard for UITextView with return key?


In IB's library, the introduction tells us that when the return key is pressed, the keyboard for UITextView will disappear. But actually the return key can only act as '\n'.

I can add a button and use [txtView resignFirstResponder] to hide the keyboard.

But is there a way to add the action for the return key in keyboard so that I needn't add UIButton?


Solution

  • UITextView does not have any methods which will be called when the user hits the return key. If you want the user to be able to add only one line of text, use a UITextField. Hitting the return and hiding the keyboard for a UITextView does not follow the interface guidelines.

    Even then if you want to do this, implement the textView:shouldChangeTextInRange:replacementText: method of UITextViewDelegate and in that check if the replacement text is \n, hide the keyboard.

    There might be other ways but I am not aware of any.