I'm trying to instantiate a xib from storyboard but I'm getting an infinite loop. I have seen this snippet working on a video but I'm probably doing something wrong. I can understand why, but not how to make it work... Txs for help ! )
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
func setup()
{
print("INFINITE LOOP :(")
self.view = self.loadViewFromNib()
self.view.frame = bounds
self.addSubview(self.view)
}
func loadViewFromNib() -> UIView
{
let nib: UINib = UINib(nibName: "ItemView", bundle: .main)
let view: UIView = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
return view
}
On Interface Builder, where defined your view, you should set the File Owner's custom class to ItemView
(or to other classes you are creating). Do not set the view class.
Unfortunately, StackOverflow won't let me post images, but see the screenshots below.