在自定义UITextField类中实现TextField协议

时间:2018-08-08 11:29:46

标签: ios swift uitextfield formatter

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

  

未在super.init调用中初始化属性'self.textInputDelegates'

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

1 个答案:

答案 0 :(得分:0)

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
        }
    }
}
相关问题