54

I'm working on an iphone app with multiple textfields on a viewcontroller. I'm a bit confused with resignfirstresponder and setting textfield delegates.I've been able to resign text fields when I set the delegate for one then call resignfirstresponder for that particular textfield. I have a view controller with about 7 text fields and don't want to call each individually. Does anyone have any example code that will close all of the textfields on a view controller? Also how do you set the delegate for all textfields?

Thanks!

Marco
  • 6,692
  • 2
  • 27
  • 38
Andrea F
  • 733
  • 2
  • 9
  • 16

1 Answers1

214

try using this

[self.view endEditing:YES];

This will hide keyboard if its presented due to a subview of your self.view

HarshIT
  • 4,583
  • 2
  • 30
  • 60
  • Thanks and do i need to do a sometextfield.delegate=self in viewdidload()? – Andrea F Jul 04 '13 at 10:26
  • not necessary to do that. in case you need you may also do the same in Interfacebuilder. For a large number of textfields, do for one , and then copy and paste it(a quicker way). – HarshIT Jul 04 '13 at 10:27
  • 4
    It's funny that although it's stated in the document `This method looks at the current view and its subview hierarchy for the text field that is currently the first responder`, it works for text views as well. – superarts.org Apr 29 '15 at 11:46
  • 5
    For swift try this self.view!.endEditing(true) – Milap Kundalia Jan 06 '16 at 12:42
  • @BalajiGupta : Kindly show this appreciation in Inx's Tuesday meeting. – AsgarAli Feb 29 '16 at 19:50