I have created a custom view (ImageAndToolBarContainerView) with a corresponding XIB file that I would like to load into multiple UIViewControllers in my app.
I have been hunting for a proper tutorial of how to do this, but almost every one I've come across either is too old or causes major exceptions.
When I try loading it through the story board / NIB, (I create a view in the UIViewController on the storyboard and I set the "Class" attribute to ImageAndToolBarContainerView. I Set the outlets, including the View, but none of them seem to load when the class is called, and I get the error :
fatal error: unexpectedly found nil while unwrapping an Optional value
when I try to access the view:
required init?(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
//setup()
self.view.frame = self.bounds
self.addSubview(view)
}
Then, I try using the following code to load the NIB instead (by uncommenting the setup() function above. The code of setup is this:
func setup()
{
self.loadViewFromNIB()
self.view.frame = self.bounds
self.addSubview(view)
}
func loadViewFromNIB() -> UIView
{
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "ImageAndToolBarContainer", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
return view
}
That of course leads to an infinite loop.
I also have the following function declares:
override func awakeFromNib()
{
super.awakeFromNib()
self.view.frame = self.bounds
self.addSubview(view)
}
What am I missing here? Where did I go wrong? Is there a definitive tutorial in how to do this properly?