0

I'm working with storyboards on iOS 5 and have a simple screen that has a UITextField on it. I want to dismiss the keyboard when the user hits "Return". I followed other suggestions such as having my controller implements the UITextFieldDelegate protocol and implements textFieldShouldReturn: as follows:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [questionField resignFirstResponder];
    return YES;
}

However I see that this method is never called. I have set the controller of my view in storyboarding to my custom UIViewController.

I also tried a different implementation where I create an IBAction called dismissKeyboard but oddly I can't connect the Did Exit action to my UITextField.

Any ideas?

Update : So the problem seems to be that I'm using a UITextView and not a UITextField. I wanted a large area for the text to be entered. When I change the entry field to a UITextField it works fine. Any ideas on how to make it work with a UITextView?

imrank1
  • 629
  • 1
  • 6
  • 16
  • 2
    have you set `questionField.delegate = self` where `self` could be the controller where you created your `UItextField`? Also try to implement `textFieldDidEndEditing:`. This method is called whenever the editing session is finished (for example when the user closes the keyboard). – Lorenzo B Jan 15 '12 at 16:44

3 Answers3

0

You should connect the dismiss method to the text field's Did End Editing action.

FeifanZ
  • 16,250
  • 7
  • 45
  • 84
  • I tried doing that but oddly when i select the action uner 'Received Actions' and hover over the uitextfield it does not get highlighted. – imrank1 Jan 16 '12 at 05:13
0

So the solution to resign the keyboard for a UITextView is to implement shouldChangeTextInRange.

imrank1
  • 629
  • 1
  • 6
  • 16
0

If you want the keyboard to dismiss when tap the return key for the textview use this delegate function,

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    if([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }

    return YES;
}
Dhruv
  • 2,153
  • 3
  • 21
  • 45
Malek_Jundi
  • 6,140
  • 2
  • 27
  • 36