简体   繁体   中英

Resign keyboard when viewController is dismissed

i have a cell which include a textField. When the viewController is opened i've assigned this to be the first responder by following code:

cell.namTextField.becomeFirstResponder()

When i dismiss the viewController the keyboard hides with a delay, which does not look good. How can i resignTheFirstResponder when the viewController? when the textField is in the cell and i cant access it in a function?

You can use self.view.endEditing(true) , which will traverse all the subviews in a view (here the controller's main view) and force (this is the true part) any view that is currently the first responder to resign.

I guess it would make sense to use it inside viewWillDisappear: in order to hide the keyboard before the controller is dismissed (or another one is pushed etc)

You can use following line of code to dismiss keyboard.

[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];

This method will dismiss any keyboard present in any of your application views

try this method to hide keyboard :-

- (void) hideKeyboard:(BOOL)hidden
{
    NSArray *arrWindows = [[UIApplication sharedApplication] windows];
    if ([arrWindows count] > 1)
    {
        UIWindow *keyboardWindow = [arrWindows objectAtIndex:1];
        [keyboardWindow setHidden:hidden];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM