ios 编辑 UITextField 时如何关闭键盘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/274319/
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-30 15:22:47  来源:igfitidea点击:

How do you dismiss the keyboard when editing a UITextField

iosiphonecocoa-touchuitextfielduikeyboard

提问by kubi

I know that I need to tell my UITextField to resign first responder when I want to dismis the keyboard, but I'm not sure how to know when the user has pressed the "Done" key on the keyboard. Is there a notification I can watch for?

我知道当我想关闭键盘时,我需要告诉我的 UITextField 退出第一响应者,但我不确定如何知道用户何时按下了键盘上的“完成”键。有我可以关注的通知吗?

回答by kubi

I set the delegate of the UITextFieldto my ViewControllerclass.

我将 的代表设置UITextField为我的ViewController班级。

In that class I implemented this method as following:

在那个类中,我实现了这个方法如下:

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

回答by Jason Coco

If you connect the DidEndOnExit event of the text field to an action (IBAction) in InterfaceBuilder, it will be messaged when the user dismisses the keyboard (with the return key) and the sender will be a reference to the UITextField that fired the event.

如果您将文本字段的 DidEndOnExit 事件连接到 InterfaceBuilder 中的操作 (IBAction),当用户关闭键盘(使用返回键)时,它将收到消息,并且发送方将是对触发该事件的 UITextField 的引用。

For example:

例如:

-(IBAction)userDoneEnteringText:(id)sender
{
    UITextField theField = (UITextField*)sender;
    // do whatever you want with this text field
}

Then, in InterfaceBuilder, link the DidEndOnExit event of the text field to this action on your controller (or whatever you're using to link events from the UI). Whenever the user enters text and dismisses the text field, the controller will be sent this message.

然后,在 InterfaceBuilder 中,将文本字段的 DidEndOnExit 事件链接到控制器上的此操作(或用于从 UI 链接事件的任何内容)。每当用户输入文本并关闭文本字段时,控制器都会收到此消息。

回答by Hugo

You can also create a method in your controller

您还可以在控制器中创建一个方法

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

and then in Connection Inspector in IB connect Event "Did End On Exit" to it.

然后在IB的Connection Inspector中将事件“Did End On Exit”连接到它。

回答by Hugo

kubi, thanks. Your code worked. Just to be explicit (for newbies like) as you say you have to set the UITextField's delegateto be equal to the ViewController in which the text field resides. You can do this wherever you please. I chose the viewDidLoadmethod.

库比,谢谢。您的代码有效。只是为了明确(对于新手),正如您所说,您必须将UITextField'sdelegate设置为等于文本字段所在的 ViewController 。您可以随心所欲地执行此操作。我选择了viewDidLoad方法。

- (void)viewDidLoad 
{
    // sets the textField delegates to equal this viewController ... this allows for the keyboard to disappear after pressing done
    daTextField.delegate = self;
}

回答by matt

Here is a trick for getting automatic keyboard dismissal behavior with no code at all. In the nib, edit the First Responder proxy object in the Identity inspector, adding a new first responder action; let's call it dummy:. Now hook the Did End on Exit event of the text field to the dummy:action of the First Responder proxy object. That's it! Since the text field's Did End on Exit event now has an action–target pair, the text field automatically dismisses its keyboard when the user taps Return; and since there is no penalty for not finding a handler for a message sent up the responder chain, the app doesn't crash even though there is no implementation of dummy:anywhere.

这是一个无需代码即可获得自动键盘关闭行为的技巧。在 nib 中,编辑身份检查器中的 First Responder 代理对象,添加新的 First Responder 操作;让我们称之为dummy:。现在将文本字段的 Did End on Exit 事件挂钩到dummy:First Responder 代理对象的操作。就是这样!由于文本字段的 Did End on Exit 事件现在有一个 action-target 对,当用户点击 Return 时,文本字段会自动关闭键盘;并且由于没有为响应者链上发送的消息找到处理程序不会受到惩罚,因此即使在dummy:任何地方都没有实现,应用程序也不会崩溃。

回答by user415897

Just add

只需添加

[textField endEditing:YES];

where you want to disable keyboard and display the picker view.

您要禁用键盘并显示选择器视图的位置。

回答by Safin Ahmed

In Swift you can write an IBAction in the Controller and set the Did End On Exitevent of the text field to that action

在 Swift 中,您可以在控制器中编写一个 IBAction并将文本字段的Did End On Exit事件设置为该操作

@IBAction func returnPressed(sender: UITextField) {
    self.view.endEditing(true)
}

回答by Gulsan Borbhuiya

Swift 4.2and It will work 100%

Swift 4.2它会工作 100%

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var textField: UITextField!


    override func viewDidLoad() {
        super.viewDidLoad()

        self.textField.delegate = self

    }

    // hide key board when the user touches outside keyboard

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

    // user presses return key

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }

}

回答by Metalhead1247

You can also use

你也可以使用

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
  {

   [self.yourTextField resignFirstResponder];

  }

Best one if You have many Uitextfields :

如果你有很多 Uitextfields 最好的一个:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {    
     [self.view endEditing:YES];
 }

回答by Ryan Bourne

Here's what I had to do to get it to work, and I think is necessary for anyone with a Number Pad for a keyboard (or any other ones without a done button:

这是我必须做的才能让它工作,我认为对于任何有数字键盘的键盘(或任何其他没有完成按钮的人)来说都是必要的:

  1. I changed the UIView in the ViewController to a UIControl.
  2. I created a function called

    -(IBAction)backgroundIsTapped:(id)sender
    
  1. 我将 ViewController 中的 UIView 更改为 UIControl。
  2. 我创建了一个名为

    -(IBAction)backgroundIsTapped:(id)sender
    

This was also defined in the .h file.

这也在 .h 文件中定义。

After this, I linked to to the 'touch down' bit for the ViewController in Interface Builder.

在此之后,我链接到 Interface Builder 中 ViewController 的“着陆”位。

In the 'background is tapped' function, I put this:

在“背景被点击”功能中,我把这个:

    [answerField resignFirstResponder];

Just remember to change 'answerField' to the name of the UITextField you want to remove the keyboard from (obviously make sure your UITextField is defined like below:)

请记住将“answerField”更改为要从中删除键盘的 UITextField 的名称(显然请确保您的 UITextField 定义如下:)

    IBOutlet UITextField * <nameoftextfieldhere>;

I know this topic probably died a long time ago... but I'm hoping this will help someone, somewhere!

我知道这个话题可能很久以前就死了……但我希望这能帮助某人,在某个地方!