I am programmatically creating a UIView that is being loaded from a nib in a separate class in its own init method:
@IBOutlet weak var label: UILabel!
override init (frame: CGRect) {
super.init(frame: frame)
let xibView = UINib(nibName: "aView", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
self.addSubview(xibView)
}
In another class I am calling my UIView class and trying to set text to my label:
self.aView = AView()
self.aView?.frame = CGRect(x: 0, y: 0, width:
self.aView?.label.text = "Sample Text"
self.view.bounds.size.width, height: 200)
self.pageScrollView.addSubview(self.aView!)
Whenever I attempt to set "Sample Text" to my UILabel I keep getting the error:
Optional unexpectedly found nil while attempting to unwrap
So since this is being loaded from its nib is it because the view has yet to be drawn to the view, so it wouldn't be able to set the text to label property? If so, how can I fix this?