-2

How do I remove a keyboard from the view as a result of the return key being touched when the UITextField was created programmatically.

If the UITextField was called in the viewDidLoad I know how to do this, but the UITextField was created as a result of an -(IBAction).

I created my UITextField programmatically. I know the resignFirstResponder removes the keyboard. I have it set up to do so when the screen is taped outsie the keyboard. I also have it working to where if the user triggers the IBAction with the UIButton related to the UITextField the keyboard goes away. I also want to be able to hide the keyboard when the user selects return from the keyboard.

James
  • 7
  • 2

2 Answers2

1

You need to make yourself a UITextFieldDelegate and implement:

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

}

Make sure you set the textField's delegate to self when you create it.

Dancreek
  • 9,524
  • 1
  • 31
  • 34
0

You can use this:

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

You'll need to set your text field's delegate to self first, though:

self.textField.delegate = self;

Or, you could right-click-drag from the text field in IB to little orange circle at the bottom.

IB

Undo
  • 25,519
  • 37
  • 106
  • 129
  • Why do you say "return NO;"? It works either way. Just would like to know whay you would put NO for the BOOL. – James May 03 '13 at 16:54
  • @James Because you *have* to return something, and NO is what came out when I rolled some dice. – Undo May 03 '13 at 17:25