如何正确初始化UIView子类中的传递属性? [英] How to properly init passed property in subclass of UIView?
本文介绍了如何正确初始化UIView子类中的传递属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个UIView的子类,我想将该属性传递给它.尽我所能,我并没有真正理解初始化的所有元素.
I have a subclass of UIView that I would like to pass a property to. As much as I've tried, I don't truly understand all elements of initializing.
这是我的代码的简化版本:
Here is a simplified version of my code:
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初始化程序和通常初始化非子类的方式之间却感到困惑.
I have tried a number of things, but I'm getting confused between the UIView initializer and the way I normally initialize a non-subclassed class.
如何修改此代码以接收字符串属性,并将其初始化?谢谢
How do I modify this code to receive the string property, initialize it? Thanks
推荐答案
如果要使用自定义属性初始化UIView
,则必须重新配置其初始化程序:
If you want to initialize a UIView
with a custom property you must reconfigure its initializer:
class InputWithIncrementView: UIView {
var inputName: String?
init(inputName: String) {
self.inputName = inputName
super.init(frame: CGRect.zero)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
这篇关于如何正确初始化UIView子类中的传递属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文