2

i have a screen having navigation controller and text field. when i move next and come back i want the keyboard should be hidden in first screen. I am hiding keyboard like on textfield event.

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return NO;
}

But how to do that in View related events so that whenever my view appears keyboard is hidden.. Pls guide/Help. thanks in adv.

Lalit_vicky
  • 295
  • 1
  • 5
  • 15

5 Answers5

4

I think this is also a good way to remove keyboard with in iOS App if your UITextView or UITextField not connected through the IBOutlet.

If you want to Hide Keyboard with UIViewController LifeCycle Events like with viewWillAppear or etc. Follow this

-(void)viewWillAppear:(BOOL)animated    {
    [super viewWillAppear:animated];
    [[self view] endEditing:YES];
}

Otherwise if you object connected using IBOutLet this code will work fine as you describe too.

[yourTextField resignFirstResponder];
Buntylm
  • 7,345
  • 1
  • 31
  • 51
  • bit-whacker.. i have 4 textfields and tried for all like [TF1 resignFirstResponder].. still not working.. – Lalit_vicky Oct 07 '13 at 12:42
  • Use the viewWillAppear method code as it is mentioned by bit-whacker it will Work for you. – Anuj Oct 07 '13 at 12:47
  • @Lalit_vicky there is no need to write `[TF1 resignFirstResponder]` this as it will for everyone one `UITextField` you used. Write just one single line. `[[self view] endEditing:YES];` – Buntylm Oct 07 '13 at 12:49
  • bit-whacker.. i did that. any field in editing mode means i have to select any of the txt field. to enable editing.. I am just navigating from one view to another.. – Lalit_vicky Oct 07 '13 at 12:51
  • also tried the one [yourTextField resignFirstResponder];.. is it something not set as resignfirstresponder? – Lalit_vicky Oct 07 '13 at 12:54
  • resignFirstResponder is for single `tf1` and as i mentioned is for all. – Buntylm Oct 07 '13 at 13:02
1

Add this code to your ViewWillAppear :

for(id obj in self.view.subviews)
    {
        if([obj isKindOfClass:[UITextField class]])
        {
             [obj resignFirstResponder];
        }
    }

This would take in all the textfields in that particular view here it is the whole view and add the code you had written previously for removing the keyboard.

IronManGill
  • 7,222
  • 2
  • 31
  • 52
1

A good habit is to write this code in your screen's -viewWillDisappear. So, when you navigate from one screen to another at that time it will remove the keyboard from that screen.

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

For multiple textFields, it is better to use -endEditing for that particular view instead of -resignFirstResponder for any single textField. Take a look at my Answer.

Community
  • 1
  • 1
Bhavin
  • 27,155
  • 11
  • 55
  • 94
  • no luck mate.. i am also adding [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; inside the viewWillDisappear is tats's the problem – Lalit_vicky Oct 07 '13 at 13:01
  • @Lalit_vicky: Do one thing. Write the above code in `-viewDidDisappear:` instead of `-viewWillDisappear`. – Bhavin Oct 07 '13 at 13:03
  • @Lalit_vicky : Then the problem is somewhere else. This code should work. – Bhavin Oct 17 '13 at 10:12
0
//This is for Swift
override func viewWillAppear(animated: Bool)
{
   self.view.endEditing(true)
}
Bhavin Bhadani
  • 22,224
  • 10
  • 78
  • 108
Iya
  • 1,888
  • 19
  • 12
  • 3
    It is missing the `super.viewWillAppear` call. – Shamas S Sep 12 '15 at 09:34
  • i think no need to call super.viewWillAppear – Iya Sep 14 '15 at 06:44
  • You always need to make "super" calls or you risk messing up with general app's flow.. See doc: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/index.html#//apple_ref/occ/instm/UIViewController/viewWillAppear: – alasker Jan 11 '16 at 13:26
-1

The thing that you are doing wrong is , when you are moving back previous controller to the current controller , the keyboard is up due to the selected textfield of previous controller .

And in the current controller the code:

-(void)viewWillAppear:(BOOL)animated    {
    [super viewWillAppear:animated];
    [[self view] endEditing:YES];
}

It will not work as no textfield is selected at this controller. So what you need to do is write the same code in the previous controller viewWillDisappear Method it will surely resolve your Problem .

- (void)viewWillDisappear:(BOOL)animated
{
    [self.view endEditing:YES];
    [super viewWillDisappear:animated];
}
Anuj
  • 820
  • 4
  • 11
  • Anuj.. i have screen1 n screen2..m moving back from screen2 to screen1. u meant to add in screen2..? i tried that but still issue exits.. – Lalit_vicky Oct 07 '13 at 13:42