Like seriously after going through this...
Easy way to dismiss keyboard?
... I have multiple TextFields
and a few TextViews
. Is there not a way to a have a batch or group Dismiss First Responder for all text fields? Will I need to make method for each field? Maybe I overlooked something in that link?
Maybe I can follow something like this:
https://stackoverflow.com/questions/3282837/problem-with-multiple-textfields-to-make-the-keyboard-dissapear
Would the latter make sense? Thanks in advance.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
I figured it out....
Controller.h
@interface Controller : UIViewController <UITextFieldDelegate> {
IBOutlet UITextField *clickedDone;
}
@property (nonatomic, retain) IBOutlet UITextField *clickedDone;
Controller.m
#import "Controller.h"
@implementation Controller
@synthesize clickedDone;
- (void)viewDidLoad
{
[super viewDidLoad];
[clickedDone setDelegate:self];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
You can force the currently-editing view to resign its first responder status with [view endEditing:YES] . This hides the keyboard.
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.
The first responder is whatever control is currently ready to respond to actions. In UIKit this is usually the control that has activated the keyboard and is receiving input.
The view has an endEditing:
method you can use. The docs say
Causes the view (or one of its embedded text fields) to resign the first responder status.
In your view controller you can just call:
[[self view] endEditing:YES];
clickedDone.returnKeyType = UIReturnKeyDone; // in viewDidLoad
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Its very easy now. You can follow different approach depending on your use cases. In my case I had multiple textfields in UITableViewController. What I did is this :
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.view endEditing:YES];
}
Best answer is:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With