I was wondering how could I resign my numeric textfields?
I have a loop in my viewDidLoad
method that creates the textfields
, how can I make that whenever I touch anywhere in the view, the app dismisses the keyboard?
I was wondering how could I resign my numeric textfields?
I have a loop in my viewDidLoad
method that creates the textfields
, how can I make that whenever I touch anywhere in the view, the app dismisses the keyboard?
Call endEditing:
. This has the advantage that you do not need to know who the first responder actually is! It is a UIView method, and it operates on all the subviews, and you are in a UIViewController, and all these text fields are subviews of it, so you can simply say this:
[self.view endEditing:YES];
As for where to put that line of code, that depends on what you want to react to. You say "whenever I touch anywhere in the view"; but what view? If it is the overall view of the UIViewController, then, as has already been pointed out, you can just put a UITapGestureRecognizer on self.view
and have its action method handler call endEditing:
as shown.
Same way you resign regular (non-numeric) textfields.
Set your view controller to be a text field delegate, and as soon as editing is finished, you can resignFirstResponder on that text field and the keyboard will dismiss.
You can also use a UITapGestureRecognizer assigned to the view and then call resignFirstResponder when it's hit (sample code can be found in these two links I've connected to).
You can also do work on background touch of UIView.As I have given steps below.
1)Change UIView's class to UIControl.
2)Drag control from touch down event of UIControl's event.
3)Connect to files Owner your background touch IBAction method.And write code to resign first responder.
-(IBAction)backgroundtouch:(id)sender
{
[_yourTextField resignFirstResponder];
}