2

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.

Mamad Farrahi
  • 394
  • 1
  • 5
  • 20

1 Answers1

1

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)
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • By this code I'll get an runtime error in Appdelegate -> `Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key barImgVw.'` – Mamad Farrahi Jul 18 '18 at 18:23
  • This is irrelevant to the answer , but you can add this line beside the outlets of the class **ViewController** --------> **@IBOutlet weak var barImgVw:UIImageView!** – Shehata Gamal Jul 18 '18 at 18:25
  • yeah I know. my @IBOutlet is working safe but it gives me error. – Mamad Farrahi Jul 18 '18 at 18:41
  • Added that property ?? according to the crash it doesn't exists – Shehata Gamal Jul 18 '18 at 18:42