简体   繁体   English

关闭具有多个文本字段的第一响应者/键盘

[英]Dismissing the First Responder/Keyboard with multiple Textfields

Like seriously after going through this...在经历了这些之后,就像认真...

Easy way to dismiss keyboard? 关闭键盘的简单方法?

... I have multiple TextFields and a few TextViews . ...我有多个TextFields和一些TextViews Is there not a way to a have a batch or group Dismiss First Responder for all text fields?有没有办法为所有文本字段设置批次或组 Dismiss First Responder? 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 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;    
}

The view has an endEditing: method you can use.该视图有一个endEditing:可以使用的方法。 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:在您看来 controller 您可以致电:

[[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.就我而言,我在 UITableViewController 中有多个文本字段。 What I did is this:我所做的是这样的:

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    [self.view endEditing:YES];
}

Best answer is:最佳答案是:

  1. Added UITextFieldDelegate protocol to your viewcontroller @interface ViewController: UIViewController将 UITextFieldDelegate 协议添加到您的视图控制器 @interface ViewController:UIViewController
  2. In your xib, select the textField, in your Ulitlites section in the right side pane of XCode in the subsection of "Connections Inspector", link the textField's delegate with the.xib's "File's Owner".在您的xib中,select textField,在“Connections Inspector”子部分中XCode右侧窗格的Ulitlites部分中,将textField的代表与.xib的“文件所有者”链接。
  3. In your Viewcontroller implementation, include the follwing在您的 Viewcontroller 实现中,包括以下内容

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

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

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