How do I get the nib Name of a UIViewControl object from the storyboard

Alexander Soare picture Alexander Soare · Dec 24, 2014 · Viewed 6.9k times · Source

I am trying to define a variable in a property within a UIViewControl class. The variable is a reference for another UIViewControl class called ViewControl.

var handle = ViewControl(nibName: "insert_viewcontroller_id_here", bundle: nil")

How do I get the nibName? Also what is an nib name and why does it need to be referenced when the view control already has a UIViewControl class name?

Best, Alex.

Answer

tikhop picture tikhop · Dec 24, 2014

If you want to create a new UIViewController you have two ways of doing this.

1st way (programmatically way) : Create a new class that is subclass of UIViewController. In this way, you instantiate a view controller using follow code:

var viewController = SomeViewController()

2nd way (xib/storyboard way) : Create new ViewController using xib or storyboard. So if you choice this way and you have a view controller created in xib or storyboard you should create a new reference of view controller using follow code:

//Xib
var viewController = UIViewController(nibName: "ViewController", bundle: nil)

//Storyboard
var viewControllerStoryboardId = "ViewController"
var storyboardName = "Main"
var storyboard = UIStoryboard(name: storyboardName, bundle: NSBundle.mainBundle())
let viewController = storyboard.instantiateViewControllerWithIdentifier(viewControllerStoryboardId) as UIViewController!