Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPad dismiss Keyboard without knowing which Textfield opened it

Is there a way to do a general resignFirstResponder to hide the keyboard regardless of what textfield/view/etc calls it?

Reason is I have a lot of textfields on my view and don't want to have to resignFirstResponder for all textfields to hide the keyboard. Just want a general

[self resignFirstResponder].

Any ideas?

Thanks in advance

like image 725
IPadHackAndSlash Avatar asked Dec 17 '10 02:12

IPadHackAndSlash


2 Answers

I know that this has already been marked as answered, but for those that run into this like I did you can just use the following method on the view that contains the textfields.

- (BOOL)endEditing:(BOOL)force

This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign. UIView Documentation

[self.view endEditing:YES];

it will hide keyboard when we click on view.

like image 188
BLeB Avatar answered Nov 02 '22 22:11

BLeB


You can dismiss the keyboard without any reference to UITextfield / UITextView with help of below code:

[[[UIApplication sharedApplication] keyWindow] endEditing:YES];

this will dismiss the keyboard globally without the reference.

hope this will help you.

like image 26
sKhan Avatar answered Nov 02 '22 23:11

sKhan