3

I have two view controller. suppose, viewcontroller1 and viewcontroller2.

In viewcontroller1 I have a 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

NSNoob
  • 5,548
  • 6
  • 41
  • 54
Saad
  • 235
  • 2
  • 12
  • Possible duplicate of [How do I dismiss the iOS keyboard?](http://stackoverflow.com/questions/6906246/how-do-i-dismiss-the-ios-keyboard) – NSNoob Oct 10 '16 at 08:09

1 Answers1

6

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];
}  
NSNoob
  • 5,548
  • 6
  • 41
  • 54