关闭键盘 - iOS 7 中的多个 UITextFields

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19647096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:39:38  来源:igfitidea点击:

Dismiss The Keyboard - Multiple UITextFields in iOS 7

iosobjective-cxcodeuitextfield

提问by Miles Works

below you'll find my .h & .m files for my primary viewcontroller.

在下面你会找到我的主视图控制器的 .h 和 .m 文件。

I have 3 questions.

我有3个问题。

1.) Because I have multiple uitextfields, do I have to set each with their own resignFirstResponder statement ? and 2.) where would I do that, in what method ? 3.) Is my syntax right for resigning the first responder ?

1.) 因为我有多个 uitextfields,我是否必须用他们自己的 resignFirstResponder 语句设置每个?和 2.) 我会在哪里做,用什么方法?3.) 我的语法适合让第一响应者辞职吗?

Also it would be really nice if I could dismiss the keyboard when the user clicks out of the field NOT on hitting the return key!

此外,如果我可以在用户点击字段而不是点击返回键时关闭键盘,那将是非常好的!

I know this has been asked and answered before, but to be honest with you i'm still a little confused as to what goes where.

我知道以前有人问过并回答过这个问题,但老实说,我仍然对去哪里有点困惑。

I'm using storyboards, with XCode 5, and iOS 7.

我正在使用情节提要、XCode 5 和 iOS 7。

=============================

==============================

.h file

.h 文件

@interface ViewController : UIViewController <UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITextField *danceDate;
@property (weak, nonatomic) IBOutlet UITextField *dancePlace;
@property (weak, nonatomic) IBOutlet UITextField *danceTerminal;
@property (weak, nonatomic) IBOutlet UITextField *danceGate;

.m file

.m 文件

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self retrieveFromParse];

    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.navigationItem.rightBarButtonItem = self.editButtonItem;

    // SET DELEGATE HERE
    //
    // if I uncomment 1 of these lines, i'll get an error.
    //
    // _dancePlace.delegate = self; 
    // dancePlace.delegate = self; 
    // dancePlace = self; 

}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{

}

-(BOOL) textFieldShouldReturn: (UITextField *) textField
{
    [textField resignFirstResponder];

    return YES;
}


-(BOOL) textFieldShouldReturn: (UITextField *) textField
{
    return YES;
}

回答by Kamaros

Try the following:

请尝试以下操作:

[[self view] endEditing:YES]

回答by dRAGONAIR

Resigning the textField: All your textField.delegate should be set as ViewController's object. And then implement the below delegate method.

退出 textField:你所有的 textField.delegate 都应该被设置为 ViewController 的对象。然后实现下面的委托方法。

-(BOOL) textFieldShouldReturn: (UITextField *) textField {
[textField resignFirstResponder];
    return YES;
}

To dismiss Keyboard on tap of the View: Add a Tap gesture to your ViewController.view as follows:

要在点击视图时关闭键盘:向您的 ViewController.view 添加点击手势,如下所示:

//declare a property to store your current responder
@property (nonatomic, assign) id currentResponder;


//in viewDidLoad:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignOnTap:)];
    [singleTap setNumberOfTapsRequired:1];
    [singleTap setNumberOfTouchesRequired:1];
    [self.view addGestureRecognizer:singleTap];
    [singleTap release];

//Implement the below delegate method:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.currentResponder = textField;
}

//Implement resignOnTap:

- (void)resignOnTap:(id)iSender {
    [self.currentResponder resignFirstResponder];
}
// was missing ; after the call --> [self.currentResponder resignFirstResponder]
    // also in textFieldDidEndEditing set self.currentResponder = nil;

回答by Mirko Catalano

  -(BOOL) textFieldShouldReturn: (UITextField *) textField{

        [textField resignFirstResponder];
        return YES;
  }

also connect your UITextFieldDelegate.

也连接您的UITextFieldDelegate

回答by Daniel Shalev

This answer works for iOS 7 and arc,

这个答案适用于 iOS 7 和 arc,

  1. dismiss keyboard when user touches return: in ViewController add the following action

    -(IBAction)textFieldReturn:(id)sender
    {
        [sender resignFirstResponder];
    }
    
  1. 当用户触摸返回时关闭键盘:在 ViewController 中添加以下操作

    -(IBAction)textFieldReturn:(id)sender
    {
        [sender resignFirstResponder];
    }
    

next, in main.storyboard select the textField and from the connections inspector control + drag "Did End On Exit" event to the view controller.

接下来,在 main.storyboard 中选择 textField 并从连接检查器控件+拖动“Did End On Exit”事件到视图控制器。

  1. dismiss keyboard when user touches background: implement the following method in the ViewController

     - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
            UITouch *touch = [[event allTouches] anyObject];
            if ([YOUR_TEXT_FIELD isFirstResponder] && [touch view] != YOUR_TEXT_FIELD) {
                [YOUR_TEXT_FIELD resignFirstResponder];
            }
            [super touchesBegan:touches withEvent:event];
        }
    
  1. 当用户触摸背景时关闭键盘:在 ViewController 中实现以下方法

     - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
            UITouch *touch = [[event allTouches] anyObject];
            if ([YOUR_TEXT_FIELD isFirstResponder] && [touch view] != YOUR_TEXT_FIELD) {
                [YOUR_TEXT_FIELD resignFirstResponder];
            }
            [super touchesBegan:touches withEvent:event];
        }
    

回答by Takide

Here's what I use in my code. It works great and is more efficient than the other answers.

这是我在代码中使用的内容。它效果很好,并且比其他答案更有效。

In yourviewcontroller.h add:

在 yourviewcontroller.h 添加:

@property (nonatomic) UITapGestureRecognizer *tapRecognizer;

@property (nonatomic) UITapGestureRecognizer *tapRecognizer;

Now in the .m file, add this to your ViewDidLoad function:

现在在 .m 文件中,将其添加到您的 ViewDidLoad 函数中:

- (void)viewDidLoad {
    //Keyboard stuff
    tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapAnywhere:)];
    tapRecognizer.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:tapRecognizer];
}

Also, add this function in the .m file:

此外,在 .m 文件中添加此函数:

- (void)handleSingleTap:(UITapGestureRecognizer *) sender
{
    [self.view endEditing:YES];
}

回答by tomi

try to use self.dancePlace.delegate = self;instead of dancePlace.delegate = self;to set the UITextFieldDelegate. Does that work?

尝试使用self.dancePlace.delegate = self;而不是dancePlace.delegate = self;设置 UITextFieldDelegate。那样有用吗?

回答by ryan cumley

When the UITextFieldin question calls the delegate method of - (BOOL)textFieldShouldReturn:(UITextField*)textFieldit passes itself in as the argument.

当有UITextField问题调用它的委托方法时- (BOOL)textFieldShouldReturn:(UITextField*)textField,将自身作为参数传入。

So the specific textFieldavailable as an argument to this method IS the specific one you care about. Within this delegate method, you can just refer to it as "textField".

因此,textField可用作此方法参数的特定对象是您关心的特定对象。在此委托方法中,您可以将其称为“textField”。

That means that you should use what Mirko Catalano advised calling resignFirstResponderon textFieldrather than on the individual properties like you were doing.

这意味着您应该使用 Mirko Catalano 建议调用的内容resignFirstRespondertextField而不是像您所做的那样使用单个属性。

Mirko's suggestion to verify that the delegate is indeed assigned is critical as well. You'll want to make sure that ALL of your UITextFields in the nib or storyboard have their delegate property pointing to File's Owner. Otherwise the delegate message will go nowhere and promptly be ignored!

Mirko 验证代表确实已分配的建议也很重要。您需要确保笔尖或故事板中的所有 UITextFields 的委托属性都指向文件的所有者。否则委托消息将无处可去并立即被忽略!

回答by yehyatt

In swift 3 :

在 swift 3 中:

 //Dismiss keyboard , When touch outside
 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
    {
        self.view.endEditing(true)
    }