22

I've got one text entry box in my iphone app, when you touch it in the simulator, the keyboard pops up. But there's no way to get rid of it. Other web pages give solutions, without explaining why they should work, and they don't work for me. One says make the text box's delegate your uiview then call resignfirstresponder on the object, but it never gets called. Any suggestions? Can anybody explain what's actually going on? I can figure it out myself if I knew what the design paradigm was...

Maybe I should just put a "go" button so I have something to get the focus away from the textfield?

stu
  • 8,461
  • 18
  • 74
  • 112

4 Answers4

19

One way to do it is to set an object as the delegate to the text field and implement

- (BOOL)textFieldShouldReturn:(UITextField *)textField

in which you call [textField resignFirstResponder]

This will cause the keyboard to disappear when they push the return/go/done button (whatever you set the bottom right keyboard key to be).

See the UITextFieldDelegate reference for more info.

Martin Gordon
  • 36,329
  • 7
  • 58
  • 54
  • 1
    When first starting out, I found [the "Your First iOS Application" tutorial](http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhone101/Articles/00_Introduction.html) to be helpful. For more on this answer, see the sub-section "The Text Field’s Delegate" in ["Implementing the View Controller"](http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhone101/Articles/06_ImplementingController.html). – sage Dec 29 '10 at 04:16
  • 1
    Simple enough, though why isn't this the default behavior? This works though.. implement the interface as above, and hook-up the 'delegate' Connection in IB to the File's Owner. – Bobby Sep 01 '11 at 22:16
  • Do I replace `[textField resignFirstResponder]` with the name of my text field, such as `[myTextField resignFirstResponder]`, I can't get it to work either way on xcode 4.2. – raffian Jun 14 '12 at 04:20
  • 5
    I am more than a little annoyed that this is not set by default. – Danny Feb 06 '13 at 23:39
16

To dismiss keyboard you can use TextField Delegate.

To use this follow these steps...
1. In you viewController.h add the delegate declaration like this:

@interface ViewController : UIViewController <UITextFieldDelegate> {
    }
2. In your viewController.m call this method:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
3. Then write a code like this:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
    }
4. Final step is to set the textField's delegate:
- (void)viewDidLoad {
        [super viewDidLoad];
        self.textField.delegate = self;
    }
matteodv
  • 3,992
  • 5
  • 39
  • 73
  • Yes, it should... Anyway if you want to apply the delegate only on certain text field, in the textFieldShouldReturn: method change [textField resignFirstResponder]; with [yourTextField resignFirstResponder]; – matteodv Jun 15 '12 at 12:30
  • Can't get this working, did everything, even added `myTextField.delegate = self` in `initWithNibName` in ViewController, but the delegate never gets called – raffian Sep 10 '13 at 03:54
5

- (BOOL)textFieldShouldReturn:(UITextField *)textField in your delegate is called when the return button is pressed.

You want to call [textField resignFirstResponder] and then return YES. That should do the trick. Also make sure the delegate is set. If in doubt, add a break point or NSLog and verify.

freespace
  • 16,529
  • 4
  • 36
  • 58
0

You can simply type the following:

textfield.endEditing(true)

Where textfield is the name of your UITextField.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • so I suppose it would be silly to point out that I worked out the problem 11 years ago? :-) – stu Jun 06 '20 at 13:28