我正在使用UITextfied,而点击textfied键盘出现,但当我按下返回键,键盘没有消失。我使用了以下代码:
func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore.
{
return true;
}
方法resignfirstresponder没有进入函数。
我正在使用UITextfied,而点击textfied键盘出现,但当我按下返回键,键盘没有消失。我使用了以下代码:
func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore.
{
return true;
}
方法resignfirstresponder没有进入函数。
当前回答
我建议从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;
}
}
其他回答
它的返回true部分只告诉文本字段是否允许返回。 你必须手动告诉文本字段解散键盘(或者它的第一个响应器是什么),这是通过resignFirstResponder()来完成的,如下所示:
// Called on 'Return' pressed. Return false to ignore.
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
您可以使用以下函数使应用程序解除键盘
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
下面是一个完整的例子来更好地说明这一点:
//
// 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
}
}
代码来源:http://www.snip2code.com/Snippet/85930/swift-delegate-sample
斯威夫特3
将下面的代码添加到你的VC中
//hide keyboard when user tapps on return key on the keyboard
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true);
return false;
}
对我有用
当用户点击文本键盘上的Done按钮时,将生成Did End on Exit事件;那时,我们需要告诉文本字段放弃控制,这样键盘就会消失。为了做到这一点,我们需要向控制器类添加一个动作方法。 选择ViewController.swift添加以下动作方法:
@IBAction func textFieldDoneEditing(sender: UITextField) {
sender.resignFirstResponder()}
选择主要。项目导航器中的故事板,并调出连接检查器。从Did End On Exit旁边的圆圈拖动到故事板中的黄色视图控制器图标,然后松开。将出现一个小弹出菜单,其中包含我们刚刚添加的单个操作的名称。单击textFieldDoneEditing动作来选择它,就是这样。
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.