我已经用它的类(.Swift(创建了一个CustomView(.Xib)
。为了达到它的视图(小部件(属性,我定义了几个getter和setter,现在我需要调用它们进行修改,但当我从类中创建对象时,我不能修改属性或使用get {}
方法。
这是我自定义的视图类代码:
CustomClass.Swift
@IBOutlet weak var barLbl: UILabel!
@IBDesignable class CustomControl: UIView {
@IBInspectable var lblSetGetName : String! {
set { barLbl.text = newValue }
get { return barLbl.text }
}
在另一个视图控制器中,我无法实现获取和设置我的视图:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let CustomControlObj : CustomControl!
var lblName = CustomControlObj.lblSetGetName
CustomControlObj.lblSetGetName = "New Txt"
}
这里,在另一个视图中,控制器类编译器说:Constant 'CustomControlObj' used before being initialized
。
您可以像这样加载
let customControlObj = (Bundle.main.loadNibNamed("CustomView", owner: self, options: nil))?[0] as! CustomControl
customControlObj.frame = view.frame
customControlObj.lblSetGetName = "New Txt"
view.addSubview(customControlObj)