TextField will NOT resign first responder with UIModalPresentationFormSheet view

802 Views Asked by At

I've created a button on one viewController that loads another view modally using the UIModalPresentationFormSheet presentation style. On this loaded view, I have two textFields, and I'm forcing the first textField to be first responder so that the keyboard will appear immediately with the new view. I've set up the textFields to have an action method that is hooked up to "Did End on Exit" event. However, whenever I hit "return" on the keyboard for either textField, the keyboard fails to go away (Here is my code):

// addCustomPage method that is called when button from original view is touched
- (IBAction) addCustomPage:(id)sender
{
    NSLog(@"Adding Custom Page");
    if (!self.customPageViewController)
    {
        self.customPageViewController =
        [[CustomPageViewController alloc] initWithNibName:@"CustomPageViewController" bundle: nil];

    }
    customPageViewController.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentModalViewController:customPageViewController animated:YES];
    // force keyboard to appear with loaded page on the first textField
    [customPageViewController.firstTextField becomeFirstResponder];

}

@interface CustomPageViewController : UIViewController

@property (strong, nonatomic) IBOutlet UITextField *firstTextField;
@property (strong, nonatomic) IBOutlet UITextField *secondTextField;
 - (IBAction)keyboardEndOnExit:(id)sender; // DID END ON EXIT EVENT
@end

//in CustomPageViewController.m
-(IBAction)keyboardEndOnExit:(id)sender
{
    [sender resignFirstResponder];
}

This is a fairly straight forward problem, and I have no problem normally dismissing keyboards using this technique with basic views and textFields. I'm not sure if using a view in this presentation format, or set up makes things different. Thanks!

2

There are 2 best solutions below

0
On

You have confirmed that you keyboardEndOnExit method is actually being called?

You could also take a more direct approach by calling [yourTextView resignFirstResponder] when a specific action is take by the user, such as a key pressed etc. I would still check if that method is ever being called using breakpoints or a log.

0
On

Have a look at this question. Pretty sure it is the same problem caused by UIModalPresentationFormSheet.