我正在使用UITextfied,而点击textfied键盘出现,但当我按下返回键,键盘没有消失。我使用了以下代码:

func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore.
{
    return true;
}

方法resignfirstresponder没有进入函数。


当前回答

你可以把它放在任何地方,但不能放在UIButton中

 func TextFieldEndEditing(text fiend name: UITextField!) -> Bool
{
    return (false)
}

然后你可以把这段代码放在一个按钮中(也是一个例子):

self.view.endEditing(true)

这对我很有效

其他回答

我讨厌给每个UIViewController添加相同的函数。 通过扩展UIViewController来支持UITextFieldDelegate,你可以提供一个默认行为“返回按下”。

extension UIViewController: UITextFieldDelegate{
    public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true;
    }
}

当你创建新的UIViewController和UITextField时,你所要做的就是在你的UIViewController中写一行代码。

override func viewDidLoad() {
    super.viewDidLoad()
    textField.delegate = self
}

你甚至可以通过在Main.storyboard中钩子委托来省略这一行代码。(使用“ctrl”从UITextField拖动到UIViewController)

@RSC

对我来说,Xcode Version 6.2 (6C86e)的关键添加是override func viewDidLoad()

 self.input.delegate = self;

我试着用返回键让它工作了几个小时,直到我找到了你的帖子,RSC。谢谢你!

此外,如果你想隐藏键盘,如果你触摸屏幕上的其他地方:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        self.view.endEditing(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

所有在一个隐藏键盘和移动键盘打开视图:Swift 5

override func viewDidLoad() {
    super.viewDidLoad()
    let tap = UITapGestureRecognizer(target: self, action: #selector(taped))
    view.addGestureRecognizer(tap)
    NotificationCenter.default.addObserver(self, selector: #selector(KeyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(KeyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}


   override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(true)
    NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
}
 @objc func taped(){
    
    self.view.endEditing(true)
    
}

@objc func KeyboardWillShow(sender: NSNotification){
    
    let keyboardSize : CGSize = ((sender.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.size)!
    if self.view.frame.origin.y == 0{
        self.view.frame.origin.y -= keyboardSize.height
    }
    
    
}

@objc func KeyboardWillHide(sender : NSNotification){
    
    let keyboardSize : CGSize = ((sender.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size)!
    if self.view.frame.origin.y != 0{
        self.view.frame.origin.y += keyboardSize.height
    }
    
}


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

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.