【问题标题】:Why is the textFieldShouldReturn function not being called?为什么没有调用 textFieldShouldReturn 函数?
【发布时间】:2015-08-05 01:09:01
【问题描述】:

textFieldShouldReturn 函数根本没有被调用:没有错误但键盘根本没有响应。

我的情况与 How to hide keyboard in swift on pressing return key? 不同,因为在我的情况下什么都没有发生,而其他情况在 Objective-C 中。

这是我的代码:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var textField: UITextField!

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

textField 是我故事板上文本字段的一个出口。我也试过self.endEditing而不是resignFirstResponder

【问题讨论】:

    标签: swift keyboard


    【解决方案1】:

    这个答案的其余部分仍然非常有用,我会把它留在那里,因为它可能会帮助其他提问者......但是在这里,我错过了这个特定示例的明显问题......

    我们不会在文本字段上调用resignFirstResponder。我们在视图控制器上调用它。我们需要在文本字段上调用它,因此修改您的代码如下所示:

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

    UITextField 只会调用作为其委托的对象的 textFieldShouldReturn 属性。

    我们可以通过添加viewDidLoad 方法来以编程方式解决此问题:

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

    但我们也可以在构建时通过情节提要进行设置。

    右键单击文本字段以检查是否已设置委托:

    如果delegate 旁边的圆圈没有填充,我们还没有为UITextField 设置代理。

    要设置代理,请将鼠标悬停在此圆圈上。它将变为加号。现在单击并拖动到要委派文本字段的视图控制器(文本字段所属的视图控制器)。

    当您适当地将视图控制器连接为委托时,此菜单应如下所示:

    【讨论】:

    • ... 还要确保“ViewController”类支持 UITextFieldDelegate。所以应该是class ViewController: UIViewController, UITextFieldDelegate {...
    • Aah.. 关键是扩展 UITextFieldDelegate,但您必须为每个 textField 实现委托,例如。 self.textField.delegate = self。我忘了这样做。谢谢
    【解决方案2】:

    如果使用 Swift 3+,则必须在第一个属性之前添加下划线。喜欢:

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

    这在 Apple 文档中也有很好的记录。 https://developer.apple.com/documentation/uikit/uitextfielddelegate/1619603-textfieldshouldreturn

    【讨论】:

      【解决方案3】:

      我参加了 Swift 4 Udemy 课程,讲师说除了 Cntrl 之外,还要为 ViewController 添加 UITextFieldDelegate 类 - 从 textField 拖动到 ViewController 按钮并选择委托。

      导入 UIKit

      class ViewController: UIViewController, UITextFieldDelegate {
      
          func textFieldShouldReturn(textField: UITextField) -> Bool {
              textField.resignFirstResponder()
              return true
          }
      }
      

      【讨论】:

      • 如果我添加 textfield.resign... 那么即使 return truereturn false 它仍然有效。
      【解决方案4】:

      嗯,就我而言。我不小心启用了硬件键盘。确保取消选中“连接到硬件键盘”以使键盘显示在模拟器中。

      硬件 -> 键盘 -> 连接到硬件键盘

      希望这对其他人也有帮助!

      【讨论】:

        【解决方案5】:

        您可以在循环中设置所有 textFields 委托:

        var tF: [UITextField] = []
        
        tf = [my1TextField, my2TextField, my3TextField]
        
        for textField in tf {
            textField.delegate = self
        }
        

        【讨论】:

          猜你喜欢
          • 2013-02-16
          • 2017-08-15
          • 1970-01-01
          • 1970-01-01
          • 2014-10-13
          • 1970-01-01
          • 1970-01-01
          • 2011-04-18
          • 2016-06-22
          相关资源
          最近更新 更多