3

I have some text edit fields, and also a button to show a uidatepicker.. if I go to the uitextedit, the keyboard appears, but when I click the button, the keyboard is still here... how can I remove it?

thanks!

swiftBoy
  • 33,793
  • 26
  • 129
  • 124
ghiboz
  • 7,297
  • 20
  • 76
  • 122

4 Answers4

9

You need to use resignFirstResponder, see this similar question.

[myTextField resignFirstResponder];
Community
  • 1
  • 1
WrightsCS
  • 49,871
  • 22
  • 132
  • 180
5

See this answer for the easiest way to do it: Easy way to dismiss keyboard?

[self.view endEditing:YES];
Community
  • 1
  • 1
Onato
  • 8,758
  • 4
  • 42
  • 52
2

Call -resignFirstResponder on your currently-editing text field.

Lily Ballard
  • 169,315
  • 25
  • 364
  • 333
0

There are cases where I don't have direct access to the 'first responder', so I tend to use a different approach. I have a utility class for the keyboard with, among other functions, this one:

+ (BOOL)dismiss:(UIView *)view
{
    if (view.isFirstResponder) {
        [view resignFirstResponder];
        return YES;
    }
    for (UIView *subView in view.subviews) {
        if ([Keyboard dismiss:subView]) // It's calling itself, just to be perfectly clear
            return YES;
    }
    return NO;
}

This lets me simply call for example: [Keyboard dismiss:self.view] from anywhere within a UIViewController.

kaka
  • 1,148
  • 1
  • 13
  • 15