ios 如何在按下返回键时快速隐藏键盘?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24180954/
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
How to hide keyboard in swift on pressing return key?
提问by Ashwinkumar Mangrulkar
I am using UITextfied
while clicking on textfied keyboard appear but when i pressed the returnkey, keyboard is not disappearing. I used the following code:
我UITextfied
在点击文本化键盘时使用,但当我按下return键时,键盘没有消失。我使用了以下代码:
func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore.
{
return true;
}
the method resignfirstresponder is not getting in function.
方法 resignfirstresponder 没有发挥作用。
回答by rsc
You can make the app dismiss the keyboard using the following function
您可以使用以下功能使应用程序关闭键盘
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
Here is a full example to better illustrate that:
这是一个完整的示例,可以更好地说明这一点:
//
// ViewController.swift
//
//
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet var myTextField : UITextField
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.myTextField.delegate = self
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
}
Code source: http://www.snip2code.com/Snippet/85930/swift-delegate-sample
代码来源:http: //www.snip2code.com/Snippet/85930/swift-delegate-sample
回答by Mick MacCallum
The return true
part of this only tells the text field whether or not it is allowed to return.
You have to manually tell the text field to dismiss the keyboard (or what ever its first responder is), and this is done with resignFirstResponder()
, like so:
这return true
部分只告诉文本字段是否允许返回。
您必须手动告诉文本字段关闭键盘(或它的第一响应者是什么),这是通过 完成的resignFirstResponder()
,如下所示:
// Called on 'Return' pressed. Return false to ignore.
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
回答by Codetard
Add
UITextFieldDelegate
to the class declaration:class ViewController: UIViewController, UITextFieldDelegate
Connect the
textfield
or write it programmatically@IBOutlet weak var userText: UITextField!
set your view controller as the text fields delegate in view did load:
override func viewDidLoad() { super.viewDidLoad() self.userText.delegate = self }
Add the following function
func textFieldShouldReturn(userText: UITextField!) -> Bool { userText.resignFirstResponder() return true; }
with all this your keyboard will begin to dismiss by touching outside the textfield aswell as by pressing return key.
添加
UITextFieldDelegate
到类声明:class ViewController: UIViewController, UITextFieldDelegate
以
textfield
编程方式连接或编写@IBOutlet weak var userText: UITextField!
将您的视图控制器设置为视图中的文本字段委托加载:
override func viewDidLoad() { super.viewDidLoad() self.userText.delegate = self }
添加以下功能
func textFieldShouldReturn(userText: UITextField!) -> Bool { userText.resignFirstResponder() return true; }
有了所有这些,您的键盘将通过触摸文本字段外部以及按返回键开始关闭。
回答by Mark Moeykens
Swift 4.2 - No Delegate Needed
Swift 4.2 - 无需委托
You can create an action outlet from the UITextField for the "Primary Action Triggered" and resign first responder on the sender parameter passed in:
您可以从 UITextField 为“已触发的主要操作”创建一个操作插座,并在传入的发送者参数上退出第一响应者:
@IBAction func done(_ sender: UITextField) {
sender.resignFirstResponder()
}
Super simple.
超级简单。
(Thanks to Scott Smith's 60-second video for tipping me off about this: https://youtu.be/v6GrnVQy7iA)
(感谢 Scott Smith 的 60 秒视频让我知道这一点:https: //youtu.be/v6GrnVQy7iA)
回答by elarctheitroadis
Simple Swift 3 Solution: Add this function to your view controllers that feature a text field:
简单的 Swift 3 解决方案:将此功能添加到具有文本字段的视图控制器中:
@IBAction func textField(_ sender: AnyObject) {
self.view.endEditing(true);
}
Then open up your assistant editor and ensure both your Main.storyboard is on one side of your view and the desired view controller.swift file is on the other. Click on a text field and then select from the right hand side utilities panel 'Show the Connection Inspector' tab. Control drag from the 'Did End on Exit' to the above function in your swift file. Repeat for any other textfield in that scene and link to the same function.
然后打开你的助手编辑器并确保 Main.storyboard 位于视图的一侧,而所需的视图 controller.swift 文件位于另一侧。单击文本字段,然后从右侧实用程序面板中选择“显示连接检查器”选项卡。控制从 'Did End on Exit' 拖动到 swift 文件中的上述函数。对该场景中的任何其他文本字段重复此操作并链接到相同的功能。
回答by Tobias F. Meier
@RSC
@RSC
for me the critical addition in Xcode Version 6.2 (6C86e) is in override func viewDidLoad()
对我来说,Xcode 版本 6.2 (6C86e) 中的关键添加在 override func viewDidLoad()
self.input.delegate = self;
Tried getting it to work with the return key for hours till I found your post, RSC. Thank you!
尝试让它与返回键一起工作几个小时,直到我找到你的帖子,RSC。谢谢!
Also, if you want to hide the keyboard if you touch anywhere else on the screen:
此外,如果您想在触摸屏幕上的其他任何位置时隐藏键盘:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.view.endEditing(true);
}
回答by peacetype
To get automatic keyboard dismissal, I put this code inside one of the methods of my custom text field's class:
为了使键盘自动关闭,我将此代码放在我的自定义文本字段类的方法之一中:
textField.addTarget(nil, action:"firstResponderAction:", forControlEvents:.EditingDidEndOnExit)
Substitute your outlet's name for textField
.
将您的插座名称替换为textField
。
回答by James Thompson
Another way of doing this which mostly uses the storyboard and easily allows you to have multiple text fields is:
另一种主要使用故事板并轻松允许您拥有多个文本字段的方法是:
@IBAction func resignKeyboard(sender: AnyObject) {
sender.resignFirstResponder()
}
Connect all your text fields for that view controller to that action on the Did End On Exit
event of each field.
将该视图控制器的所有文本字段连接到Did End On Exit
每个字段事件的该操作。
回答by Ian Seth Colin
When the user taps the Done button on the text keyboard, a Did End On Exit event will be generated; at that time, we need to tell the text field to give up control so that the keyboard will go away. In order to do that, we need to add an action method to our controller class. Select ViewController.swift add the following action method:
当用户点击文本键盘上的 Done 按钮时,会产生一个 Did End On Exit 事件;那时,我们需要告诉文本字段放弃控制,这样键盘就会消失。为了做到这一点,我们需要向我们的控制器类添加一个操作方法。选择 ViewController.swift 添加以下操作方法:
@IBAction func textFieldDoneEditing(sender: UITextField) {
sender.resignFirstResponder()}
Select Main.storyboard in the Project Navigator and bring up the connections inspector. Drag from the circle next to Did End On Exit to the yellow View Controller icon in the storyboard and let go. A small pop-up menu will appear containing the name of a single action, the one we just added. Click the textFieldDoneEditing action to select it and that's it.
在 Project Navigator 中选择 Main.storyboard 并调出连接检查器。从 Did End On Exit 旁边的圆圈拖动到故事板中的黄色视图控制器图标,然后松开。将出现一个小的弹出菜单,其中包含我们刚刚添加的单个操作的名称。单击 textFieldDoneEditing 操作以选择它,就是这样。
回答by LAOMUSIC ARTS
I would sugest to init the Class from RSC:
我建议从 RSC 初始化类:
import Foundation
import UIKit
// Don't forget the delegate!
class ViewController: UIViewController, UITextFieldDelegate {
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@IBOutlet var myTextField : UITextField?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.myTextField.delegate = self;
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func textFieldShouldReturn(textField: UITextField!) -> Bool {
self.view.endEditing(true);
return false;
}
}
}