简体   繁体   中英

Dismiss the keyboard with MULTIPLE UITextFields?

Is it possible to dismiss the keyboard when you have MULTIPLE UITextFields ? If so how ?

As a side note, do I have to dismiss the keyboard for Each and Every field or can it be done globally ? Oh and it would be super cool if I don't have to touch the DONE button, I'd ideally like a solution that where the user touches anything BUT the field in question and the keyboard automagically disappears...

Oh and if you'd be so kind step by step instructions.


I should have added that I have a method already to resign the keyboard....

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

My question is how to the keyboard to hide/dismiss without having to jump thru so many damned hoops! You'd figure after 6 years, a mature operating system would have a way to GLOBALLY hide 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 this is called when my button is submitted....

- (IBAction)addListingPressed:(id)sender {

// NSLog(@"BUTTON PRESSED");

[self hideKeyboard];
[self valuesAdded];

}

My question, assuming anyone can answer this...and I suspect not, is there a way to globally hide the keyboard if the following conditions are MET: 1.) the user taps OUT of any one of the existing fields, 2.) presses anywhere else on the screen. 3.) Is no more than a line or two in the existing viewcontroller.m file. 4.) I don't have to add a confusing button on the viewcontroller. (any time I have to add outlets, the damned thing is crashing on me...and then nastiness happens, and really...remember I am JUST a beginner, and its very confusing to read that I have to place this here and that there...oy. Simple folks, simple. I'm not looking for elegant solution, just so that it works.

I have a super class that all my view controllers inherit from. In that 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's view
    //this should dismiss the keyboard
    [[self view] endEditing:YES];
}

I have the 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's 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 an example project to github. https://github.com/JeffRegan/KeyboardBeGone

RDVKeyboardAvoiding is a scroll view with a tap gesture recognizer, designed for multiple textViews/textFields. It keeps track of the active view and removes a lot of boilerplate code.

tap anywhere outside the textField .. it will hide it..

[self.view endEditing:YES];

There are couple of other ways to do it.

[myEditField resignFirstResponder];
[myEditField endEditing];
[parentView endEditing];

Yes, you only have to dismiss it for the one that is currently being edited.

In order to know which one is being edited, you can check the -(BOOL)isFirstResponder property, 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 that one to get rid of the keyboard.

For example, if you have a method called -(void)aMethod that you want to dismiss the current view controller and you have an array of textViews called textArray , you could do a little loop such as:

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

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

If you only have one or two textFields and you do not want to create an Array object, you could do (assuming the fields are named text1 and text2 :

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

Also, to make things easier for the future you could create a category method for UIView (which is what I do) to get the current first responder if it exists as a subview of that 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 on the top of any file that you want to call it from, 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];
}

If you dont wont to do so many things and simply want to dismiss keyboard than give iboutlet to each of your text filed to following method..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM