I have an application with two simple screen. The first screen always opens same instance of the second screen. The second screen has a text field
and a button
to manually hide keyboard
.
The resignFirstResponder
on viewWillDisappear
is not working.
Case 1:
- Click openSearchButton on the main screen.
- SearchViewController is opened.
- Click text field.
- Keyboard becomes visible.
- Click back button.
- Application returns to the first screen.
- Click openSearchButton on the main screen.
- (iOS 10.2) Screen is completely white for a short time.
- SearchViewController is opened.
- The text field becomes first responder and keyboard becomes visible.
Case 2:
- Click openSearchButton on the main screen.
- SearchViewController is opened.
- Click text field.
- Keyboard becomes visible.
- Click hideKeyboardButton
- Keyboard becomes hidden.
- Click back button.
- Application returns to the first screen.
- Click openSearchButton on the main screen.
- SearchViewController is opened.
Code:
@implementation MainViewController
-(void) viewDidLoad {
[super viewDidLoad];
UIStoryboard *sb = [UIStoryboardWithName:@"Main" bundle:nil];
self.searchView = (SearchViewController*) [sb instantiateViewControllerWithIdentifier:@"searchView"];
}
- (IBAction)openSearchButtonClicked:(id)sender{
[self.navigationController pushViewController:self.searchView animated:YES];
}
@end
@implementation SearchViewController
- (void)viewWillDisappear:(BOOL)animated{
assert(self.searchTextField != nil);
[self.searchTextField resignFirstResponder];
[super viewWillDisappear:animated];
}
- (IBAction)hideKeyboardButtonClicked:(id)sender{
[self.searchTextField resignFirstResponder];
}
@end