3

In my app, I press a button and it pulls up a modal presentation sheet (for iPad). Within this modal view I have a scrollview within my main view, and 1 text field within my scroll view.

view controller
    view
        scrollview
            text field

Nothing I have tried resigns the keyboard and I don't know why. The only thing that happens is the blinking cursor in the textfield goes away. My class is the delegate for the scrollview and text fields. Here is what I have tried:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.titleTextField resignFirstResponder];
    [self.titleTextField endEditing:YES];
    [self.view endEditing:YES];
    [self.view resignFirstResponder];
    [self.scrollView endEditing:YES];
    [self.scrollView resignFirstResponder];
}

The method does get called, but the keyboard doesn't go away. Can anyone help me or at least tell me why?

Here is how I present this modalpresentation view:

(it comes from a tableviewcontroller)

didSelectRowAtIndexPath

EditVideo *targetController = [self.storyboard instantiateViewControllerWithIdentifier:@"editVideo"];

        targetController.delegate = self;

        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:targetController];

        navigationController.modalPresentationStyle = UIModalPresentationFormSheet;

        [self presentViewController:navigationController animated:YES completion:nil];

        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
Josue Espinosa
  • 5,009
  • 16
  • 47
  • 81
  • My guess is that your titleTextField is either not the first responder, or you are making it again somewhere else in your code. Take a look here too:http://stackoverflow.com/a/1823360/312312 – Lefteris Dec 01 '13 at 00:10
  • Have you confirmed you are first responder ? Try Steves's gewtfirstresponder code for UIView: http://stackoverflow.com/questions/8772468/get-firstresponder-in-objective-c Also does scrollViewDidScroll get called after the scroll and you may have put focus back on the scrollview and re-initiated the first responder ? – Aardvark Dec 01 '13 at 00:17
  • When I try to find the firstResponder, it returns null. – Josue Espinosa Dec 01 '13 at 00:25
  • However, when I check [self.titleTextField isFirstResponder], it returns 1(true). – Josue Espinosa Dec 01 '13 at 00:38

1 Answers1

4

On the iPad for any any non-fullscreen presented ViewController, you must implement -(BOOL)disablesAutomaticKeyboardDismissal to return NO to dismiss the keyboard.

- (BOOL)disablesAutomaticKeyboardDismissal
{
    return NO;
}

Once that is implemented, you can call [self.view endEditing:YES];.

Edit: The other common cause of this problem is returning NO from - (BOOL)textFieldShouldEndEditing:(UITextField *)textField. Implement the method in the UITextFieldDelegate and have it return YES unconditionally to prove that it is not a factor.

Holly
  • 5,270
  • 1
  • 24
  • 27
  • Tried that, unfortunately still nothing :( – Josue Espinosa Dec 01 '13 at 00:30
  • @JosueEspinosa: Did you implement it in both the presented (Form Sheet) view controller as well as the presenting? – Holly Dec 01 '13 at 00:48
  • I did. The only thing that changes is the text field loses its cursor, the keyboard remains where it's at. – Josue Espinosa Dec 01 '13 at 00:50
  • I updated my answer to now include both causes to this problem that I've encountered. You probably have a bug in your code. Post your view controller code so we can look it over. – Holly Dec 01 '13 at 00:54
  • Updated my question. Btw the text field wasn't made programmatically, it was made through Interface Builder. I don't alter the text field in any way in any class. – Josue Espinosa Dec 01 '13 at 00:59
  • To confirm, in your `EditVideo` view controller, you are implementing `-disablesAutomaticKeyboardDismissal`? – Holly Dec 01 '13 at 01:07
  • Yes, both in EditVideo and the view controller where it is presented. I honestly have no idea why it isn't working. – Josue Espinosa Dec 01 '13 at 01:09
  • I found the culprit in [this thread](http://stackoverflow.com/questions/3019709/modal-dialog-does-not-dismiss-keyboard/10507689#10507689). The `UINavigationController` does not implement `-disablesAutomaticKeyboardDismissal`. There are several possible solutions depending on your intent. – Holly Dec 01 '13 at 01:10
  • My intent is to dismiss the keyboard when the user scrolls in the scroll view. – Josue Espinosa Dec 01 '13 at 01:11
  • That's not quite what I meant. The options you have are a) create your own subclass of `UINavigationController` so you can override the function or b) create a category on UINavigationController to override it in the standard class. – Holly Dec 01 '13 at 01:14
  • I created a category on UINavigationController with the code provided in the link, then imported into EditVideo.m, but it still doesn't work. Am I doing something wrong? – Josue Espinosa Dec 01 '13 at 01:16
  • I can't tell you if you're doing something wrong without looking over every detail of your project and storyboard. This is the right cause, but you have to figure out the rest. – Holly Dec 01 '13 at 01:25
  • It's not that important to my project, just drives me crazy. Thanks for your help Neal. I'll accept your answer and eventually figure it out :) – Josue Espinosa Dec 01 '13 at 01:31
  • Depending on how important it is, you could create a GitHub project of the minimum code to create the problem and post it with your question. Then you would get a definitive solution. – Holly Dec 01 '13 at 01:34