user522860
user522860

Reputation:

Dismissing keyboard on iPad from textfield inside tableviewcell

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:

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?

Upvotes: 1

Views: 1473

Answers (2)

KDaker
KDaker

Reputation: 5909

[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;
}

Upvotes: 6

Wain
Wain

Reputation: 119031

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

[tableView endEditing:YES];

Upvotes: 1

Related Questions