简体   繁体   English

使用MULTIPLE UITextFields关闭键盘?

[英]Dismiss the keyboard with MULTIPLE UITextFields?

Is it possible to dismiss the keyboard when you have MULTIPLE UITextFields ? 有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... 哦,如果我不必触摸DONE按钮,那将是非常酷的,我理想地喜欢一个解决方案,用户触摸任何东西,但有问题的字段和键盘自动消失...

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! 6年后你会想到一个成熟的操作系统会有一种全局隐藏键盘的方法....不!

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. 我的问题,假设任何人都可以回答这个......我怀疑没有,如果以下条件是MET,有没有办法全局隐藏键盘:1。)用户点击任何一个现有字段的OUT,2。)按下屏幕上的任何其他位置。 3.) Is no more than a line or two in the existing viewcontroller.m file. 3.)现有的viewcontroller.m文件中只有一行或两行。 4.) I don't have to add a confusing button on the viewcontroller. 4.)我不必在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. (任何时候我必须添加插座,这个该死的东西都会撞到我身上......然后发生了肮脏的事情,而且真的......记得我只是一个初学者,而且读起来很困惑我必须把它放在这里那里...... oy。简单的人,简单。我不是在寻找优雅的解决方案,只是为了它的工作原理。

I have a super class that all my view controllers inherit from. 我有一个超级类,我的所有视图控制器都继承自。 In that class I have this code. 在那堂课里,我有这段代码。

MySuperViewController.h MySuperViewController.h

#import <UIKit/UIKit.h>

@interface MySuperViewController : UIViewController
@property(strong, nonatomic) UITapGestureRecognizer *backgroundTapGestureRecognizer;
@end

MySuperViewController.m 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. 我将UITapGestureRecognizer作为公共属性,因此如果需要,我可以覆盖它。

subclass 子类

MyViewController.h MyViewController.h

#import <UIKit/UIKit.h>
#import "MySuperViewController.h"    


@interface MyViewController : MySuperViewController<UIGestureRecognizerDelegate>
@end

MyViewController.m 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. 我上传了一个示例项目到github。 https://github.com/JeffRegan/KeyboardBeGone https://github.com/JeffRegan/KeyboardBeGone

RDVKeyboardAvoiding is a scroll view with a tap gesture recognizer, designed for multiple textViews/textFields. RDVKeyboardAvoiding是一个带有轻手势识别器的滚动视图,专为多个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.. 点击textField外的任何地方..它会隐藏它..

[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. 为了知道正在编辑哪一个,你可以检查-(BOOL)isFirstResponder属性,如果它是第一个响应者(正在编辑的那个-(BOOL)isFirstResponder ,它将返回YES,如果不是,则返回NO。 Once you know which one is the first responder you can call -(void)resignFirstResponder on that one to get rid of the keyboard. 一旦你知道哪一个是第一个响应者你就可以调用-(void)resignFirstResponder来解决那个问题。

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 ,您想要关闭当前视图控制器,并且您有一个名为textArray数组,您可以执行一个textArray ,例如:

-(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. 这样,您可以拥有可变数量的textFields,它仍然有效。

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 : 如果您只有一个或两个textField并且您不想创建一个Array对象,则可以这样做(假设这些字段名为text1text2

-(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: 此外,为了使未来更容易,您可以为UIView创建一个类别方法(这就是我所做的),以获取当前第一个响应者(如果它作为该视图的子视图存在):

@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.. 如果你不想做这么多的事情,只是想解雇键盘,而不是给你的每个文本提交iboutlet到下面的方法..

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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