Disconnect the keyboard using MULTIPLE UITextFields?

Is it possible to remove the keyboard if you have MULTIPLE UITextFields? If so, how?

As a side note, do I have to reject the keyboard for the Every and Every fields, or can this be done globally? Oh, and it would be great if I didn’t have to touch the DONE button, I would ideally like the solution in which the user touches something, but the field in question and the keyboard automatically disappears ...

Oh, and if you were good step-by-step instructions.


I should have added that I have a method to put up with the keyboard already ....

However, it only starts when my form is submitted! (see method below)

My question is how to hide / fire on the keyboard without jumping over so many damned hoops! You would have done it in 6 years, a mature operating system would have a GLOBAL way of hiding the keyboard ... NOT!

Ok, enough whining ....

- (void)hideKeyboard { [self.dancePlace resignFirstResponder]; [self.danceGate resignFirstResponder]; [self.danceTerminal resignFirstResponder]; [self.danceText resignFirstResponder]; [self.danceDate resignFirstResponder]; [self.danceStyle resignFirstResponder]; [self.danceTimeOut resignFirstResponder]; } 

And that’s called when my button is sent ....

 - (IBAction)addListingPressed:(id)sender { // NSLog(@"BUTTON PRESSED"); [self hideKeyboard]; [self valuesAdded]; } 

My question is, assuming that someone can answer this question ... and I do not suspect that there is a way to globally hide the keyboard if the following MET conditions: 1.) the user removes OUT from any of the existing fields, 2.) clicks anywhere on the screen. 3.) No more than one line or two in the existing viewcontroller.m file. 4.) I do not need to add a confusing button to the viewcontroller. (anytime when I have to add stores, the damn thing crumbles on me ... and then the nasty thing happens, and really ... remember that I am ONLY a newbie and it is very confusing to read that I have to place this here and what is there ... Simple people, simple. I'm not looking for an elegant solution, just to make it work.

+4
source share
6 answers

I have a superclass that all my controllers inherit from. In this class, I have this code.

MySuperViewController.h

 #import <UIKit/UIKit.h> @interface MySuperViewController : UIViewController @property(strong, nonatomic) UITapGestureRecognizer *backgroundTapGestureRecognizer; @end 

MySuperViewController.m

 - (void)viewDidLoad{ //add a tap gesture recognizer to capture all tap events //this will include tap events when a user clicks off of a textfield self.backgroundTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onBackgroundTap:)]; self.backgroundTapGestureRecognizer.numberOfTapsRequired = 1; self.backgroundTapGestureRecognizer.cancelsTouchesInView = NO; [self.view addGestureRecognizer:self.backgroundTapGestureRecognizer]; } - (void)onBackgroundTap:(id)sender{ //when the tap gesture recognizer gets an event, it calls endEditing on the view controller view //this should dismiss the keyboard [[self view] endEditing:YES]; } 

I have a UITapGestureRecognizer as a public property, so I can override it if I need to.

Subclass

Myviewcontroller.h

 #import <UIKit/UIKit.h> #import "MySuperViewController.h" @interface MyViewController : MySuperViewController<UIGestureRecognizerDelegate> @end 

Myviewcontroller.m

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //You don't always want the keyboard to be dismissed, so you tie into the gesture recognizer delegate method //By doing this, you can stop the endEditing call from being made [self.backgroundTapGestureRecognizer setDelegate:self]; } - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { //touch.view is the view that recieved the touch //if this view is another textfield or maybe a button, you can return NO and the endEditing call won't be made if (touch.view == self.myViewThatShouldNotBeBlocked) { return NO; } //if you want the gesture recognizer to accept the event, return yest return YES; } 

I uploaded a sample project to github. https://github.com/JeffRegan/KeyboardBeGone

+6
source

RDVKeyboardAvoiding is a scroll view with a recognizing gesture designed for multiple text / text fields. It tracks the active view and removes many patterns.

+1
source

click somewhere outside the text box. he will hide it.

 [self.view endEditing:YES]; 

There are several other ways to do this.

 [myEditField resignFirstResponder]; [myEditField endEditing]; [parentView endEditing]; 
+1
source

Yes, you only need to reject it for the one that is currently being edited.

To find out which one is being edited, you can check the -(BOOL)isFirstResponder , which will return YES if it is the first responder (the one being edited), or NO if it is not. Once you know which one is the first responder, you can call -(void)resignFirstResponder on this to get rid of the keyboard.

For example, if you have a method called -(void)aMethod that you want to remove from the current view controller, and you have an array of text elements called textArray , you can do a little loop, for example:

 -(void)aMethod { for (UITextField *text in self.textArray) { if ([text isFirstResponder]) [text resignFirstResponder]; return; } } 

This way you can have a variable number of text fields, and it will still work.

If you have only one or two text fields, and you do not want to create an Array object, you can do (if the fields have the values text1 and text2 :

 -(void)aMethod { if ([text1 isFirstResponder]) [text1 resignFirstResponder]; else if([text2 isFirstResponder]) [text2 resignFirstResponder]; } 

In addition, to simplify the future, you can create a category method for UIView (which I do) to get the current first responder, if it exists as a subtitle of this view:

 @implementation UIView (GetFirstResponder) - (UIView *)getFirstResponder { if ([self isFirstResponder]) return self; else { for (UIView *subview in self.subviews) { UIView *firstResponder = [subview getFirstResponder]; if (firstResponder) return firstResponder; } } return nil; } 

You can put this method at the beginning of any file from which you want to call it, or create a separate file for it and import it.

Once you have this method, you can call:

 - (void)aMethod { UIView *view = [self.view getFirstResponder]; if (view) [view resignFirstResponder]; } 
0
source

If you are not going to do so much and just want to remove the keyboard, and not pass iboutlet to each of your text files that follow the next method.

 -(IBAction)hidekeyboard:(id)sender { [sender resignFirstResponder]; } 
0
source
  [superview endEditing:YES]; // superview can be the view controller view property. 
0
source

Source: https://habr.com/ru/post/1493990/


All Articles