【问题标题】:swift 3.0 - UITextFieldDelegate protocol extension not workingswift 3.0 - UITextFieldDelegate 协议扩展不起作用
【发布时间】:2016-10-16 13:00:12
【问题描述】:

我尝试在ViewController 中实现UITextFieldDelegate 协议。我从apple tutorial 开始。我实现了相同的方法,例如在教程中,但它不起作用。 (XCODE 8)

class ViewController: UIViewController, UITextFieldDelegate {

    // MARK: Properties

    @IBOutlet weak var recipeNameField: UITextField!
    @IBOutlet weak var recipeNameLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        recipeNameField.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: Actions

    @IBAction func onSetDefaultRecipeClick(_ sender: UIButton) {
        recipeNameField.text = "Deafult recipe name"
    }

    // MARK: UITextFieldDelegate
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()

        print("return")
        return true
    }

}

【问题讨论】:

  • 从键盘打印时是否将“return”打印到控制台?
  • 不。什么都没有发生
  • 好的,viewDidLoad 被调用了吗?您可以使用断点或另一个 print("view did load") 检查它
  • 是的,它被调用了。奇怪...

标签: ios swift3 uitextfielddelegate


【解决方案1】:

您可以尝试删除 textField 委托中的第一行。

【讨论】:

  • func textFieldShouldReturn(_ textField: UITextField) -> Bool { print("return") return true }
猜你喜欢
  • 2015-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-04
  • 1970-01-01
相关资源
最近更新 更多