I have two UITextFields
(e.g. username and password) but I cannot get rid of the keyboard when pressing the return key on the keyboard. How can I do this?
14 Answers
First you need to conform to the UITextFieldDelegate
Protocol in your View/ViewController's header file like this:
@interface YourViewController : UIViewController <UITextFieldDelegate>
Then in your .m file you need to implement the following UITextFieldDelegate
protocol method:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
[textField resignFirstResponder];
makes sure the keyboard is dismissed.
Make sure you're setting your view/viewcontroller to be the UITextField's delegate after you init the textfield in the .m:
yourTextField = [[UITextField alloc] initWithFrame:yourFrame];
//....
//....
//Setting the textField's properties
//....
//The next line is important!!
yourTextField.delegate = self; //self references the viewcontroller or view your textField is on

- 4,142
- 8
- 37
- 50

- 9,508
- 5
- 39
- 60
-
Hi Siddharth, I have done what you said above but it still doesn't make the keyboard disappear. I've done the `UITextFieldDelegate` and then in my .m file, I have the above. Inside, `[self.usernameField resignFirstResponder]; return YES;`. Still not working though... Do you know why? Thanks. – K.Honda Jun 01 '11 at 09:09
-
7You can also implement the delegate in storyboard by clicking on the textfield, show Utilities panel, click Connections Inspector, drag delegate outlet onto the view controller. – abc123 Mar 20 '13 at 16:13
-
@Sid Hi, this works great when the `UITextField`s are in a view. If I have `UITextField`s inside a scroll view, when tapped on a `UITextField`, even the keyboard doesn't show up. So I changed the delegate to the scroll view. Now the keyboard shows up but when the return key is pressed, it doesn't fire the `textFieldShouldReturn` method. Any idea how to get around this? – Isuru Mar 29 '13 at 10:59
-
1Where is your textFieldShouldReturn method implemented? You need to set that class to be the UITextField's delegate. The delegate callback will only fire from the class that is set to be the delegate, and if the class has implemented it. – Sid Mar 29 '13 at 18:39
-
This works for me, but I have a question. I get a warning for Assigning to 'id' UITextFieldDelegate from incompatible type 'MyViewController' *const_strong. How to I get rid of that warning? – Steven Marlowe Apr 18 '13 at 22:48
-
1Have you set MyViewController to conform to UITextFieldDelegate in it's header? – Sid Apr 18 '13 at 23:41
-
Is it safe to call `myTextField.delegate = self` in `initWithNibName`? – raffian Sep 10 '13 at 03:45
-
1As long as myTextField has been allocated correctly and is not nil. Technically, it's fine if it's nil (no crash) but then that won't do anything :) – Sid Sep 10 '13 at 04:38
-
1And if I may add, it doesn't matter if you are just using an *overridden* UITextField class, such as UIFloatLabelTextField, you STILL NEED yourTextField.delegate = self;!!! – Gellie Ann Jan 13 '16 at 09:14
Implement the UITextFieldDelegate method like this:
- (BOOL)textFieldShouldReturn:(UITextField *)aTextField
{
[aTextField resignFirstResponder];
return YES;
}

- 47,228
- 12
- 98
- 108
Took me couple trials, had same issue, this worked for me:
Check your spelling at -
(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
I corrected mine at textField
instead of textfield
, capitalise "F"... and bingo!! it worked..
Your UITextFields should have a delegate object (UITextFieldDelegate). Use the following code in your delegate to make the keyboard disappear:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
}
Should work so far...

- 1,195
- 11
- 24
-
Hey mate, I've done what you have said above but I still can't get the keyboard to disappear. Do you have any ideas? Thanks. – K.Honda Jun 01 '11 at 09:11
-
When the return key is pressed, call:
[uitextfield resignFirstResponder];

- 2,998
- 7
- 37
- 69
-
Hey Conor, how does the app know when the return key is pressed? Thanks. – K.Honda Jun 01 '11 at 09:11
After quite a bit of time hunting down something that makes sense, this is what I put together and it worked like a charm.
.h
//
// ViewController.h
// demoKeyboardScrolling
//
// Created by Chris Cantley on 11/14/13.
// Copyright (c) 2013 Chris Cantley. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate>
// Connect your text field to this the below property.
@property (weak, nonatomic) IBOutlet UITextField *theTextField;
@end
.m
//
// ViewController.m
// demoKeyboardScrolling
//
// Created by Chris Cantley on 11/14/13.
// Copyright (c) 2013 Chris Cantley. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// _theTextField is the name of the parameter designated in the .h file.
_theTextField.returnKeyType = UIReturnKeyDone;
[_theTextField setDelegate:self];
}
// This part is more dynamic as it closes the keyboard regardless of what text field
// is being used when pressing return.
// You might want to control every single text field separately but that isn't
// what this code do.
-(void)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
}
@end
Hope this helps!

- 51
- 1
Set the Delegate of the UITextField to your ViewController, add a referencing outlet between the File's Owner and the UITextField, then implement this method:
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == yourTextField)
{
[textField resignFirstResponder];
}
return NO;
}

- 7,398
- 2
- 30
- 60

- 1,399
- 11
- 17
Add this instead of the pre-defined class
class ViewController: UIViewController, UITextFieldDelegate {
To remove keyboard when clicked outside the keyboard
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
and to remove keyboard when pressed enter
add this line in viewDidLoad()
inputField is the name of the textField used.
self.inputField.delegate = self
and add this function
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}

- 3,371
- 2
- 22
- 27
Swift 2 :
this is what is did to do every thing !
close keyboard with Done
button or Touch outSide
,Next
for go to next input.
First Change TextFiled Return Key
To Next
in StoryBoard.
override func viewDidLoad() {
txtBillIdentifier.delegate = self
txtBillIdentifier.tag = 1
txtPayIdentifier.delegate = self
txtPayIdentifier.tag = 2
let tap = UITapGestureRecognizer(target: self, action: "onTouchGesture")
self.view.addGestureRecognizer(tap)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
if(textField.returnKeyType == UIReturnKeyType.Default) {
if let next = textField.superview?.viewWithTag(textField.tag+1) as? UITextField {
next.becomeFirstResponder()
return false
}
}
textField.resignFirstResponder()
return false
}
func onTouchGesture(){
self.view.endEditing(true)
}

- 2,788
- 1
- 30
- 49
-
Using tags is explicitly discouraged by apple. You could use an IBOutletCollection instead – Antzi Oct 15 '15 at 15:25
in swift you should delegate UITextfieldDelegate, its important don't forget it, in the viewController, like:
class MyViewController: UITextfieldDelegate{
mytextfield.delegate = self
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
}
}

- 863
- 9
- 20
You can add an IBAction to the uiTextField(the releation event is "Did End On Exit"),and the IBAction may named hideKeyboard,
-(IBAction)hideKeyboard:(id)sender
{
[uitextfield resignFirstResponder];
}
also,you can apply it to the other textFields or buttons,for example,you may add a hidden button to the view,when you click it to hide the keyboard.

- 436
- 5
- 3
You can try this UITextfield subclass which you can set a condition for the text to dynamically change the UIReturnKey:
https://github.com/codeinteractiveapps/OBReturnKeyTextField

- 1
- 1
-
Please explain your answer and copy the relevant portion from the link to the post. – Rohit Gupta Oct 21 '15 at 01:09