How to hide the keyboard in UIViewController by the return button click-> iphone

Hi, I find some such questions, but they are talking about textView, I have a ViewController, with scrollView, where there are 6 text fields and one textView. I want a function that makes the keyboard disappear on the done / return click button. I have implemented retirement functions for the first responder that hide my keyboard when I click outside of scrollView, but that’s not quite what I want because I like to she disappeared at the touch of a button.

thanks for the help

+7
source share
4 answers

Configure the class that conforms to the UITextFieldDelegate protocol and make the delegate of your text fields an instance of this class. Implement the method:

- (BOOL)textFieldShouldReturn:(UITextField *)textField 

Properly:

 - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } 
+14
source

Hi, I found it so that the point with the text fields was to add these lines to the viewdidload:

 textFieldOne.returnKeyType = UIReturnKeyDone; textFieldCislo.delegate = self; textFieldTwo.returnKeyType = UIReturnKeyDone; textFieldCislo.delegate = self; ... 

And this implementation method:

 -(BOOL)textFieldShouldReturn:(UITextField *)theTextField { if (theTextField == textFieldOne) { [textFieldOne resignFirstResponder]; } ... } 
+5
source

After quite a while, hunting for something that makes sense, this is what I put together, and it worked like a charm.

.h

 // // ViewController.h // demoKeyboardScrolling // // Created by Chris Cantley on 11/14/13. // Copyright (c) 2013 Chris Cantley. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITextFieldDelegate> // Connect your text field to this the below property. @property (weak, nonatomic) IBOutlet UITextField *theTextField; @end 

.m

 // // ViewController.m // demoKeyboardScrolling // // Created by Chris Cantley on 11/14/13. // Copyright (c) 2013 Chris Cantley. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // _theTextField is the name of the parameter designated in the .h file. _theTextField.returnKeyType = UIReturnKeyDone; [_theTextField setDelegate:self]; } // This part is more dynamic as it closes any text field when pressing return. // You might want to control every single text field separately but that isn't // what this code do. -(void)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; } @end 
+2
source

U can use this method to hide the keyboard by clicking anywhere in the view

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } 
+2
source

All Articles