ios - 未调用 UITextFieldDelegate 方法

标签 ios swift uitextfield

我正在关注 Big Nerd Ranch iOS 编程书籍(Swift 版)并且我有这个文件。

import UIKit

class ConversionViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet var celsiusLabel: UILabel!
    @IBOutlet var textField: UITextField!

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        print("here")
        return true
    }

    //rest of the code here
}

由于某种原因,没有调用 textField 委托(delegate)方法。我在控制台中看不到“此处”日志。我多次仔细检查了命名和语法。有什么想法吗?

最佳答案

您需要将文本字段的委托(delegate)设置为自己。

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

关于ios - 未调用 UITextFieldDelegate 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34842153/

相关文章:

ios - UITextField 键盘 : enable done button when no text is entered

ios - Swift 中的模块化文本占位符 block

android - 如何将应用程序本地存储同步到数据库?

ios - 有什么办法可以在取消 UIPopoverController 的同时允许触摸取消按钮吗?

iphone - 在 iOS 中移除键盘的阴影

ios - 如何使用 dx 和 dy(导数)创建 Swift CGVector?

swift - 在 XCode 8 beta 6 中将 C api 迁移到 Swift 3

ios - Swift:如何在字符串的特定索引处插入 UIImage?

arrays - 获取更短版本的 for 循环?

ios - 文本字段 shouldChangeCharactersInRange 未调用