Prateek Regar
Prateek Regar

Reputation: 11

how can i get NibName of viewcontroller while not using storyboard

I'm trying to implement a page menu in my app using the pod from this pre-built page menu pod from github

In the instructions, it says:

var controller : UIViewController = UIViewController(nibName:"controllerNibName", bundle: nil)
controller.title = "SAMPLE TITLE"
controllerArray.append(controller)

things are all set up but i need only "controllerNibName". I am not using storyboard. And haven't made any xib file yet with viewController. so how can i get the NibName of viewController.

Upvotes: 1

Views: 3179

Answers (2)

Subramanian P
Subramanian P

Reputation: 4375

controllerNibName string is a file name of your XIB. Where you have done all the UI which is related with your UIViewController.

If your viewController class name and xib file names are same then you can directly allocate the UIViewController. It will pick the xib automatically.

Assume UIViewController name is PageOne & xib name also should be PageOne.xib

let pageOne = PageOne()

if your xib name is different from your view controller name

For Example : Page1.xib

let pageOne = PageOne(nibName:"Page1", bundle: nil)

Upvotes: 3

Eridana
Eridana

Reputation: 2408

There is a nice way to get a name of nib (if nib name is equal to controller name i.e. MyViewController.xib and MyViewController):

let controllerName = String(describing: MyViewController.self)
var controller : MyViewController = MyViewController(nibName:controllerName, bundle: nil)

Upvotes: 0

Related Questions