Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss keyboard iOS programmatically when pressing return

I created a UITextField programmatically making the UITextField a property of the viewController. I need to dismiss the keyboard with the return and the touch on the screen. I was able to get the screen touch to dismiss, but pressing return is not working.

I've seen how to do it with storyboards and by allocating and initializing the UITextField object directly without creating it as a property. Possible to do?

.h

#import <UIKit/UIKit.h>  @interface ViewController : UIViewController <UITextFieldDelegate>  @property (strong, atomic) UITextField *username;  @end 

.m

#import "ViewController.h"  @interface ViewController ()  @end  @implementation ViewController  - (void)viewDidLoad {     [super viewDidLoad];          // Do any additional setup after loading the view, typically from a nib.          self.view.backgroundColor = [UIColor blueColor];     self.username = [[UITextField alloc] initWithFrame:CGRectMake(100, 25, 80, 20)];     self.username.placeholder = @"Enter your username";     self.username.backgroundColor = [UIColor whiteColor];     self.username.borderStyle = UITextBorderStyleRoundedRect;     if (self.username.placeholder != nil) {         self.username.clearsOnBeginEditing = NO;     }     _username.delegate = self;           [self.view addSubview:self.username];     [_username resignFirstResponder];      }  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{     NSLog(@"touchesBegan:withEvent:");     [self.view endEditing:YES];     [super touchesBegan:touches withEvent:event]; }  @end 
like image 615
noobsmcgoobs Avatar asked Sep 12 '13 04:09

noobsmcgoobs


People also ask

How do I dismiss the keyboard in IOS?

This is the quickest way to implement keyboard dismissal. Just set a Tap gesture on the main View and hook that gesture with a function which calls view. endEditing . Causes the view (or one of its embedded text fields) to resign the first responder status.

How do you dismiss a keyboard from the tap?

Android devices have a solution; press the physical back button (provided on some mobile phones) or the soft key back button, and it closes the keyboard.

How do you dismiss a keyboard in Obj C?

You can force the currently-editing view to resign its first responder status with [view endEditing:YES] . This hides the keyboard.


2 Answers

The simple way is to connect the delegate of UITextField to self (self.mytestField.delegate = self) and dismiss the keyboard in the method textFieldShouldReturn using [textField resignFirstResponder];

Another way to dismiss the keyboard is the following:

Objective-C

[self.view endEditing:YES]; 

Swift:

self.view.endEditing(true) 

Put [self.view endEditing:YES]; where you would like to dismiss the keyboard (Button event, Touch event, etc.).

like image 103
Nitin Gohel Avatar answered Sep 28 '22 03:09

Nitin Gohel


Add a delegate method of UITextField like this:

@interface MyController : UIViewController <UITextFieldDelegate> 

And set your textField.delegate = self; then also add two delegate methods of UITextField

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {        return YES; }  // It is important for you to hide the keyboard - (BOOL)textFieldShouldReturn:(UITextField *)textField {     [textField resignFirstResponder];     return YES; } 
like image 38
iPatel Avatar answered Sep 28 '22 04:09

iPatel