3

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?

3 Answers 3

9

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)

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

Comments