1

I have a problem with the iOS keyboard. I have a UITextField inside a UITableViewCell. When tapping on another TableViewCell within the same TableView, I want to dismiss the keyboard before showing a UIPopoverController. All of this is shown in a form sheet.

Here is the view hierarchy:

  • Root View Controller
    • UINavigationController, shown as UIModalPresentationFormSheet
      • Table View Controller
        • Table View Cell 1
          • Text Field
        • Table View Cell 2
          • When tapped: UIPopoverController containing a UIDatePicker

In this graph, I want to add code in tableView:didSelectRowAtIndexPath: to dismiss the keyboard just before showing the UIPopoverController. This is not a problem on iPhone as I use a fullscreen modal view controller instead of a popover controller.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // nameTextField is the text field in Table View Cell 1
    [nameTextField resignFirstResponder];

    UITextField *tempTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 100, 10)];
    [self.view addSubview:tempTextField];
    [self.view setNeedsDisplay];
    tempTextField.enabled = NO;
    [tempTextField becomeFirstResponder];
    [tempTextField resignFirstResponder];
    [tempTextField removeFromSuperview];

    [self.view endEditing:YES];

    KDDatePickerViewController *dpvc = [[KDDatePickerViewController alloc] init];
    popoverController = [[UIPopoverController alloc] initWithContentViewController:dpvc];
    [popoverController presentPopoverFromRect:[self.tableView cellForRowAtIndexPath:indexPath].frame
                                       inView:self.view
                     permittedArrowDirections:UIPopoverArrowDirectionAny
                                     animated:YES];
}

As you can see from the code, I tried calling resignFirstResponder and endEditing, and I tried to create a temporary text field. I also tried implementing disablesAutomaticKeyboardDismissal, but it was never called.

Can anyone see what I am doing wrong?

  • What is the actual problem? You never say what is going wrong, just what you have tried. – rmaddy Aug 16 '13 at 23:49
  • is `nameTextField` the field you are refering to in Table View Cell 1? do you have other textfields textviews in the view? – KDaker Aug 16 '13 at 23:52
  • @maddy: The keyboard does not get dismissed when displaying the popover controller. I am trying to force the text field to lose focus, but the keyboard stays. –  Aug 17 '13 at 00:08
  • @KDaker Sorry, I forgot to mention that. You're right, nameTextField is the textfield in the table view cell 1. I do not have other textfields or textviews in my tableview. –  Aug 17 '13 at 00:09

2 Answers2

6

[nameTextField resignFirstResponder] should be sufficient. It could be that the IBOutlet isn't binding correctly or there is something else that is the first responder.

Try this answer to dismiss the current first responder. If this does work, however, I would suggest you still look into the issue of why it isn't dismissing when you reference it directly.

I would suggest removing tempTextField before trying this.

The answer I linked is helpful and you can use it but it is more appropriate in a case where you have several TextFields and you don't necessarily know which is the first responder.

hope this helps

Update

according to apple's documentation for disablesAutomaticKeyboardDismissal:

The default implementation of this method returns YES when the modal presentation style of the view controller is set to UIModalPresentationFormSheet and returns NO for other presentation styles. Thus, the system normally does not allow the keyboard to be dismissed for modal forms.

so simply override it in your controller and return NO

- (BOOL)disablesAutomaticKeyboardDismissal {
    return NO;
}
Community
  • 1
  • 1
KDaker
  • 5,899
  • 5
  • 31
  • 44
  • I noticed that after calling `[nameTextField resignFirstResponder]` the blinking cursor disappeared. So I think the text field has actually lost focus, but for some reason the keyboard doesn't go away. I also noticed that tapping the keyboard no longer inserts text into the text field. I don't have any other text fields. I've just tried the ansert you linked to but I still had the same problem: keyboard doesn't go away, but the text field seems to lose focus and typing on the keyboard no longer inserts text. –  Aug 17 '13 at 00:25
  • Thank you so much! Overriding `disablesAutomaticKeyboardDismissal` fixed my problem. –  Aug 17 '13 at 02:52
1

Don't create random new views to try to force the keyboard. UIView already offers a feature. Try:

[tableView endEditing:YES];
Wain
  • 118,658
  • 15
  • 128
  • 151
  • I commented out the other code where I tried adding the temporary text field and I inserted `[tableView endEditing:YES];`, but the keyboard still doesn't get dismissed. –  Aug 17 '13 at 00:06
  • Possibly `[tableView.window endEditing:YES];` – Wain Aug 17 '13 at 00:17
  • That also doesn't seem to work in this case. I noticed that when using either one of your methods, or `[nameTextField resignFirstResponder];` the blinking cursor from the text field disappeared. So it seems like the text field is no longer the first responder, but for some reason the keyboard doesn't disappear. –  Aug 17 '13 at 00:21