iOS SDK: Dismiss keyboard when a button gets clicked?
Asked Answered
G

4

5

How can I dismiss the keyboard when the user clicks a button? A short example for a better understanding: the user edits some text in some textfields and at the end he doesn't click "Done" or smething else on the keyboard, but he clicks on an button "Save" while the keyboard is still shown. So, how can I now dismiss the keyboard?

Thx. Regards, Daniel

Goodhumored answered 15/6, 2011 at 11:26 Comment(0)
S
4

in button action write,

if([textField isFirstResponder]){
  [textField resignFirstResponder];
}

if there are more textfields get the current textfield reference everytime while editing started, and resign its responder in button action.

Slew answered 15/6, 2011 at 11:32 Comment(3)
thx! additionally, I want to dismiss the keyboard when the user taps onto the screen either. I used: UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyBoard)]; //[self.view addGestureRecognizer:tap]; But then the button action doesn't work - maybe the button click is recognized as a tap onto the "blank screen"?Diopside
@Daniel: is the view contains any scrollviews?Slew
yep, scrollview and textview too. btw... is it possible to make a uiscrollview and within that there are two textviews and the textviews shouldn't be able to scroll independently - instead of that only the uiscrollview should be scrolled so that the rest of the textviews gets visible/shown (so, they get scrolled too)?Diopside
C
6

You can also call [view endEditing:YES] on any view above the text field in the hierarchy. This is useful if you aren't sure which text field has first responder status. It also optionally lets a text field delegate stop the action (by returning NO from shouldEndEditing:) which is nice if you are using a delegate to do validation on the fields.

Cologne answered 27/6, 2013 at 18:43 Comment(0)
S
4

in button action write,

if([textField isFirstResponder]){
  [textField resignFirstResponder];
}

if there are more textfields get the current textfield reference everytime while editing started, and resign its responder in button action.

Slew answered 15/6, 2011 at 11:32 Comment(3)
thx! additionally, I want to dismiss the keyboard when the user taps onto the screen either. I used: UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyBoard)]; //[self.view addGestureRecognizer:tap]; But then the button action doesn't work - maybe the button click is recognized as a tap onto the "blank screen"?Diopside
@Daniel: is the view contains any scrollviews?Slew
yep, scrollview and textview too. btw... is it possible to make a uiscrollview and within that there are two textviews and the textviews shouldn't be able to scroll independently - instead of that only the uiscrollview should be scrolled so that the rest of the textviews gets visible/shown (so, they get scrolled too)?Diopside
R
1

If you have multiple text fields and don't know which one is first responder (or you simply don't have access to the text fields from wherever you are writing this code) you can call endEditing: on the parent view containing the text fields.

In Swift, in the related view controller, add this line to the button function:

self.view endEditing(true)
Reincarnate answered 21/7, 2016 at 16:42 Comment(0)
F
0

In addition to SriPriya's answer, please have here the Swift 2 styled version:

if numberTextField.isFirstResponder() {
    numberTextField.resignFirstResponder()
}

Please enjoy...

Flong answered 4/12, 2015 at 22:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.