Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: "FeedDetail", bundle: nil)
controller.view.backgroundColor = UIColor.whiteColor()
self.presentViewController(controller, animated: true, completion: nil)

This crash because:

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

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

enter image description here

like image 819
Luca Davanzo Avatar asked Oct 19 '22 11:10

Luca Davanzo


1 Answers

Sry I taken the answer from here, you missed something like (You have no view in xib so add one and then do this):

enter image description here

if you are using Xib follow this

follo following steps

1) open your xib file then right click on files owner and drag to your first view

enter image description here

2) then bind that view with outlet of "view"

enter image description here

hope you will get it...

like image 162
Anbu.Karthik Avatar answered Nov 04 '22 02:11

Anbu.Karthik