how to dismiss already opened keyboard when popover appears ios

In my iOS project, I have a form which contains various text fields. Some text fields are edited by keyboard and some by picker view which is placed on popover.

When I go on filling text fields, without dismissing it and then if I click on popover text field keyboard remains open.

It appears as both keyboard and popover present on screen at a same time, which I don't want.

I am able to get whether keyboard is opened or not by setting flag in keyboard notification methods and also last text filed that was edited through text filed delegates. And have tried

  • [self endEditing:YES]; (as it is in table cell)

  • [lastEditedTextField resignFirstResponder];

  • Even try to pass keyboard dismiss notification by my self (without knowing whether it is possible or not)

  • [[NSNotificationCenter defaultCenter] postNotificationName:UIKeyboardWillHideNotification object:nil];
  • but nothing is working.

    How can I dismiss keyboard (if already open) whenever popover appears.

    Thanks in advance.


    Try to implement textFieldShouldBeginEditing: and inside it check which text field is this. If it is one of the fields that should display a popover, first call [self.view endEditing:YES] to hide the keyboard, then present the popover and return NO . This way the text field won't take first responder status and the keyboard will not appear again. And if it is one of the "normal" text fields, just return YES .


    You can call:

    [self.view endEditing:YES];
    

    But, a better solution is likely to present the picker using the UIResponder inputView so it automatically replaces the keyboard and you don't need to mediate between 2 different things (and the user doesn't switch between different parts of the screen potentially).

    链接地址: http://www.djcxy.com/p/44290.html

    上一篇: UISearchBar关闭方法

    下一篇: 当弹出窗口出现时如何解除已打开的键盘ios