我根据我的应用程序 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/