How to Make Return Key on iPhone Make Keyboard Disappear

How to make return key on iPhone make keyboard disappear?

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

Objective C close keyboard when return key is pressed

Add this in your ViewController class

yourTextField.delegate = self

and this UITextFieldDelegate method

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

How to hide keyboard when return key is hit - Swift

Your problem is that you didn't delegate a textField in order to use that method. First, your class must include the UITextFieldDelegate protocol:

class yourClass: UIViewController, UITextFieldDelegate { ... }

And in the viewDidLoad() method, add this as well:

scoreText.delegate = self

And then you have to change this:

func textFieldShouldReturn(_ scoreText: UITextField) -> Bool {
self.view.endEditing(true)
return false
}

to this:

func textFieldShouldReturn(_ scoreText: UITextField) -> Bool {
self.view.endEditing(true)
return true
}

Final code:

class yourClass: UIViewController, UITextFieldDelegate {

@IBOutlet weak var scoreText: UITextField!

override func viewDidLoad(){
super.viewDidLoad()
scoreText.delegate = self
}

func textFieldShouldReturn(_ scoreText: UITextField) -> Bool {
self.view.endEditing()
return true
}
}

If this is not working, the problem is not the textFieldShouldReturn() function. Please check your outlet connections.

How to dismiss keyboard for UITextView with return key?

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.

How to make the keyboard disappear after clicking return button

The problem with your code was that you were checking if your UITextFields were nil or not, which aren't equal to nil. So whenever the conditions were checked, it always went into the first case and returned (the other being in the else). The following scenarios will also work:

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

OR

-(BOOL) textFieldShouldReturn:(UITextField *)textField{
if (_cardNameTextField == textField)
{
[self.cardNameTextField resignFirstResponder];
}
else if (_pinTextField == textField)
{
[self.pinTextField resignFirstResponder];
}
return YES;
}

OR

Just remove the else keyword in the second case.

OR

Do it @Neelam Verma's way.

I would recommend the first scenario which forces the first responder to be resigned without checking for any conditions...

iPhone keyboard return key

You cannot customize the apple iOS's default keyboard.

As such you can create a custom control similar to apple iOS's keyboard and make it look and customize as you want but then there ar more chances than not that your app may be rejected when you try to submit your app on the Apple's app store.

So it is not preferable to create custom keyboard.

Hope this helps you.



Related Topics



Leave a reply



Submit