3

In my project I have several pages with many UITextField-s. So I figured it would be better to create an utility method named hideKeyboard which would work in any case. Here is my solution, but actually something in this code doesn't feel right. Is there any better solutions for this?

+(void) hideKeyBoard {
    UITextField* t = [[UITextField alloc] initWithFrame:CGRectNull];
    [[UIApplication sharedApplication].keyWindow addSubview:t];
    [t becomeFirstResponder];
    [t resignFirstResponder];
    [t removeFromSuperview];
}
vdd
  • 362
  • 1
  • 4
  • 11

1 Answers1

17

A pretty simple solution is to use the responder chain as noted here

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) 
                                           to:nil 
                                         from:nil
                                     forEvent:nil];

By sending the action to nil it get sent into the responder chain and of course the first object that get's a chance to respond is the object that is the firstResponder

Side note The only reason I saw this post is because of the weekly newsletter iOSDevWeekly, which is definitely worth subscribing to

Edit

I guess brute force way of doing it would be

[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
Community
  • 1
  • 1
Paul.s
  • 38,494
  • 5
  • 70
  • 88
  • Thanks for your quick reply. But this method doesn't seem to be working. Am I doing something wrong? – vdd Aug 09 '12 at 09:02
  • More specific: It just doesn't work. But the second answer works just fine. Thanks! – vdd Aug 09 '12 at 09:21
  • Here's how to implement. First set your textfield delegate to your view controller. You can do this in storyboard by rightclicking on the field and dragging to the VC. Then in your view controller put that line of code in the `-(BOOL)textFieldShouldReturn:(UITextField *)textField` method and boom it works. – Alex Reynolds Oct 16 '12 at 18:48
  • @AlexReynolds your answer does not really relate to the original question. Granted it works and it is how to get the keyboard to dismiss when hitting return **but** the OP wants to hide the keyboard - not necessarily when hitting return – Paul.s Oct 16 '12 at 20:28
  • And that may be true but as per comments people don't know how to implement your solution. Solutions on StackOverflow should include full answers and implementation is part of a full complete answer. Please update your answer with comments on how to implement pertaining to the original question then because it is still confusing people. – Alex Reynolds Oct 16 '12 at 20:40
  • 1
    @AlexReynolds the two snippets of code I have in my answer are the full answer. A user just needs to call one or the other that is all there is to it. Your solution is not for the same problem... – Paul.s Oct 16 '12 at 20:52
  • If it does not work for tableviewcells, try implementing `canPerformAction:withSender:`: http://stackoverflow.com/a/24343989/354018 – fabb Jul 13 '15 at 08:55