Saad Saad - 1 month ago 12
Objective-C Question

keyboard not dismiss when come from another view ios

i have two view controller. suppose, viewcontroller1 and viewcontroller2.
in viewcontroller1 have textview and a button.when i click textview keyboard open, and if press button keyboard dismiss nicely.i am using this code for dismiss keyboard.

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


but, when keyboard open, and i go to viewcontroller2 and then return to viewcontroller1, in viewWillAppear method i use same code for dismissing keyboard, but keyboard not dismiss. i am so much astonished to see this. please help

Answer

Get rid of that sending Action routine

First, remove this line:

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

Option no.1

Now In your other ViewController's viewDidLoad, write this line:

[self.view endEditing:YES];

Option no.2

You can also resign the textField and textViews before navigating to second VC. E.g. Do it like this in your first VC's navigation code:

//Method moveToSecondVC()
[self.view endEditing:YES];
[self.navigationController pushViewController:secondVC animated:YES];

Option no.3

If you are using segues, you can do it in prepareForSegue method like:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    [self.view endEditing:YES];
}

Option no.4

Alternatively, you can just write this line in viewWillDisappear method of your ViewControllers like:

- (void)viewWillDisappear:(BOOL)animated 
{
    [self.view endEditing:YES];

    [super viewWillDisappear:animated];
}  
Comments