Mamad Farrahi
Mamad Farrahi

Reputation: 494

Use of setter getter from another view controller

I've already created a CustomView(.Xib) with it's class(.Swift). In order to reach it's views (widgets) property I've defined several getter and setter and for now I need to call them to modify but when I make an object from the class I can't modify the properties or use get {} method.

It's my custom class codes of views :

CustomClass.Swift

@IBOutlet weak var barLbl: UILabel!
@IBDesignable class CustomControl: UIView { 
@IBInspectable var lblSetGetName : String! {
    set { barLbl.text = newValue }
    get { return barLbl.text }
}

And here in another view controller that I can't achieve get and set of my views :

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    let CustomControlObj : CustomControl!
    var lblName = CustomControlObj.lblSetGetName
    CustomControlObj.lblSetGetName = "New Txt"
}

Here, In another view controller class compiler says : Constant 'CustomControlObj' used before being initialized.

Upvotes: 2

Views: 511

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100523

You can load it like this

let customControlObj  = (Bundle.main.loadNibNamed("CustomView", owner: self, options: nil))?[0] as! CustomControl
customControlObj.frame = view.frame
customControlObj.lblSetGetName = "New Txt"
view.addSubview(customControlObj)

Upvotes: 1

Related Questions