Swift: create programmatically UIViewController from xib

I’m approaching to swift in this days and I’ve a question.

What if I have to create programmatically a new UIViewController?

Easy with an empty/new view controller:

var controller: UIViewController = UIViewController()
controller.view.backgroundColor = UIColor.whiteColor()
self.presentViewController(controller, animated: true, completion: nil)

Now, I have a xib file that I would like to load on the controller:

var controller: UIViewController = UIViewController(nibName: "myXibName", bundle: nil)
controller.view.backgroundColor = UIColor.whiteColor()
self.presentViewController(controller, animated: true, completion: nil)

This crash because:

'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "myXibName" nib but the view outlet was not set.'

I’ve already read this post and I can’t understand what’s wrong!


Source: ios

Leave a Reply