如何在 UIView 的子类中正确初始化传递 属性?

How to properly init passed property in subclass of UIView?

我有一个 UIView 的子 class,我想将 属性 传递给它。尽我所能,我并没有真正理解初始化的所有元素。

这是我的代码的简化版本:

class inputWithIncrementView : UIView, UITextFieldDelegate {
    var inputName : String // This is the property I want to receive and init

override init (frame : CGRect) {
    super.init(frame : frame)
    // [this is where i will use the inputName property passed on initialization] 
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder) 
}
// [other functions and stuff working fine here]
}

我已经尝试了很多东西,但我对 UIView 初始化器和我通常初始化非 subclassed class.

的方式感到困惑

如何修改此代码以接收字符串 属性、初始化它?谢谢

如果您想用自定义 属性 初始化 UIView,您必须重新配置其初始化程序:

class InputWithIncrementView: UIView {
    let inputName: String

    init(inputName: String) {
        self.inputName = inputName
        super.init(frame: .zero)
    }

    required init?(coder aDecoder: NSCoder) {
        return nil
    }
}