crazyoxygen
crazyoxygen

Reputation: 716

resignFirstResponder Don't work?

I try to hide keyboard on iPad but I don't know why resignFirstResponder don't work. But popToRoot has work well.

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{
    NSString *desc = [NSString stringWithFormat:@"%@",[descTF text]];
    [textField resignFirstResponder];
    [self.navigationController popToRootViewControllerAnimated:YES];


    return YES;
}

So Could you guide me what should I do please ??

Upvotes: 5

Views: 3929

Answers (5)

Mike Gledhill
Mike Gledhill

Reputation: 29161

(Apologies... this is the fourth place on the StackOverflow site where I'm posted this fix..)

I had huge problems with XCode 5 & iOS 7, trying to get the onscreen keyboard to hide.

Eventually, I found a very simple solution: I just disabled the UITextFields in my form. Magically, this was enough to make the onscreen keyboard slide away.

More details here

Surprisingly, this even works on Modal UIViewControllers. Yeah, it surprised me aswell !!

Upvotes: 0

Rob Jones
Rob Jones

Reputation: 4985

To follow up on samvermette's answer, if it is inside a UIModalPresentationFormSheet you can now override the disablesAutomaticKeyboardDismissal method to get the behavior you desire. Add the following method to your class and the UITextFields and UITextViews will respond to resignFirstResponder.

- (BOOL)disablesAutomaticKeyboardDismissal {
    return NO;
}

Upvotes: 7

samvermette
samvermette

Reputation: 40437

Is this field inside a UIModalPresentationFormSheet? If so, it's a known issue that you can not dismiss the keyboard programmatically until the view controller gets dismissed.

UPDATE: according to this thread from the Apple Developer Forums, a possible workaround for this is to present the sheet view control from inside a navigation controller subclass that implements the disablesAutomaticKeyboardDismissal method. So something like:

MyViewController *myViewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
CustomNavigationController *navController = [[CustomNavigationController alloc] initWithRootViewController:myViewController];
theNavigationController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:theNavigationController animated:YES];

Upvotes: 19

NSResponder
NSResponder

Reputation: 16861

You should never have occasion to send -resignFirstResponder. What you should do is just send -endEditing:YES to the view in question.

Upvotes: 1

Legolas
Legolas

Reputation: 12325

Did you implement the UITextFieldDelegate ? If you had implemented that then this, then it should work.

Upvotes: 0

Related Questions