-1

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Look at the related questions to your question. Several already answer this question. Please look at the related questions before posting a new question. – rmaddy Apr 25 '13 at 03:56

3 Answers3

3

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • And here is my book's discussion of gesture recognizers: http://www.apeth.com/iOSBook/ch18.html#_gesture_recognizers – matt Apr 25 '13 at 03:51
  • And here is my book's discussion of the general problem of dismissing the keyboard: http://www.apeth.com/iOSBook/ch23.html#_summoning_and_dismissing_the_keyboard – matt Apr 25 '13 at 03:52
2

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).

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Could you please give me an example on how to do this? Thanks a lot! – Nicolás Múnera Apr 25 '13 at 03:35
  • With the UITapGestureRecognizer, How can I know in the dismiss method, which texfield it should resign? Thanks! – Nicolás Múnera Apr 25 '13 at 03:37
  • You either need to keep track of which text field that has begun editing (via a delegate method), or you could simply resign *all* of them. – Michael Dautermann Apr 25 '13 at 03:37
  • And how can I resign all of them? What if I dont keep a record of which textfields I create? :o – Nicolás Múnera Apr 25 '13 at 03:40
  • You could iterate through all the subviews of your view controller's content, calling "`resignFirstResponder`" on any subviews that match [`[subview isKindOfClass: [NSTextField class]] == YES`](http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/isKindOfClass:); Also, ewwww. – Michael Dautermann Apr 25 '13 at 03:43
0

You can also do work on background touch of UIView.As I have given steps below.

1)Change UIView's class to UIControl.

enter image description here

2)Drag control from touch down event of UIControl's event.

enter image description here

3)Connect to files Owner your background touch IBAction method.And write code to resign first responder.

-(IBAction)backgroundtouch:(id)sender
{
    [_yourTextField resignFirstResponder];
}

enter image description here

Prince Kumar Sharma
  • 12,591
  • 4
  • 59
  • 90