swift - UITextfield 类快速调用委托(delegate)方法

标签 swift uitextfielddelegate

我根据我的应用程序 UI 创建了一个自定义 UITextfield 类。但是我希望将一些委托(delegate)方法包含到类中,这样我就不需要在所有类中都编写它们。

open class SAIconTextField: SkyFloatingLabelTextFieldWithIcon {

required public init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    configure()
}

/// Override init method
override init(frame: CGRect) {
    super.init(frame: frame)
    configure()
}

/// Override layoutSubviews method
override open func layoutSubviews() {
    super.layoutSubviews()
}

///  This method is used to set general UI
func configure() {
    self.tintColor = UIColor.appBlue
    self.selectedTitleColor = .appBlue
    self.selectedLineColor = .appBlue
    self.selectedLineHeight = 0.5

    self.iconColor = .lightGray
    self.iconImageView.tintColor = .lightGray
    self.selectedIconColor = .appBlue
    self.iconMarginBottom = 7.0
    //        self.iconMarginLeft = 2.0
    self.errorColor = .errorColor
}
}

我扩展了我的类以执行委托(delegate)方法,以便所有文本字段都阻止在我的应用程序中输入表情符号,但未调用该方法。

// MARK: - UITextFieldDelegate
extension SAIconTextField: UITextFieldDelegate {

/// Delegate method
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if textField.textInputMode?.primaryLanguage == "emoji" || !((textField.textInputMode?.primaryLanguage) != nil) {
        return false
    }
    return true
}
}

还有其他方法可以做类似的事情吗?

最佳答案

您没有设置文本字段委托(delegate)。

将此行添加到您的 configure() 函数中:

delegate = self

关于swift - UITextfield 类快速调用委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53257467/

相关文章:

ios - 使用 Swift 在 UITextFields 之间自动切换

uitextfield - UIAlertController 的文本字段委托(delegate)未被调用

swift - 结构关联枚举中的计算属性

ios - 使用 'vendored_frameworks' 和 'source_files' 用于 cocoapod 使用 'use_frameworks!'

ios - UITextField 输入金额

ios - 在 Storyboard 中创建 UITextField 时调用什么 init 方法

ios - UITextField 代表不工作

ios - 第一个 Swift 计算器

ios - 移除广告 - 应用内购买

swift - 点播资源: Download request could not be completed