gpt4 book ai didi

ios - 在自定义 UITextField 类中实现 TextField 协议(protocol)

转载 作者:行者123 更新时间:2023-11-30 11:16:00 27 4
gpt4 key购买 nike

我正在使用 SkyFloatingTextField 框架(其子类 UITextField)作为我的文本字段,并且我还想使用 AnyFormatKit 格式化程序作为电话号码掩码。因为它使用 TextInput 委托(delegate),所以我创建了一个自定义类来实现 TextInput 委托(delegate),但出现了以下错误。

Property 'self.textInputDelegates' not initialized at super.init call

class customTextField: SkyFloatingLabelTextField, TextInput{
var content: String?

var attributedContent: NSAttributedString?

var textInputDelegates: MulticastDelegate<TextInputDelegate>


override init(frame: CGRect) {
super.init(frame: frame)
}

init(){
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
textInputDelegates.add(delegate: self.inputDelegate as! TextInputDelegate)
}
}

https://github.com/luximetr/AnyFormatKit

https://github.com/Skyscanner/SkyFloatingLabelTextField

最佳答案

You can implement delegate methods of a textfield subclass by making use of a supportive class as below.

class TextFieldSupport: NSObject, TextInputDelegate {
//Supporting class for your textfield delegate

var tfDelegate: UITextFieldDelegate?
//MARK: Implement TextInputDelegate Delegate Methods here.

}

class CustomTextField: SkyFloatingLabelTextField {

//SkyFloatingLabelTextField Subclass
var content: String?

var attributedContent: NSAttributedString?

var textInputDelegates: TextFieldSupport = TextFieldSupport.init()

override init(frame: CGRect) {
super.init(frame: frame)
}

init(){

}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
textInputDelegates.add(delegate: self.inputDelegate as! TextInputDelegate)
}

override var delegate: UITextFieldDelegate? {
didSet {
textInputDelegates.tfDelegate = delegate
super.delegate = support
}
}
}

关于ios - 在自定义 UITextField 类中实现 TextField 协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51745612/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com