Get Nib name/What exactly is a nib?

Michael Ninh picture Michael Ninh · Aug 27, 2015 · Viewed 14.6k times · Source

I'm trying to implement a page menu segue 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)

This is my code:

    var chatAreaCtrl : UIViewController = ChatAreaCtrl(nibName: "ChatAreaCtrl", bundle: nil)
    chatAreaCtrl.title = "Chats"
    controllerArray.append(chatAreaCtrl)

Which gets me the error:

    Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: ...(loaded)' with name 'ChatAreaCtrl''

I'm pretty new to programming and Swift but I guess I'm not specifying the what the Nib is correctly?

Thanks!

Answer

Duncan C picture Duncan C · Aug 27, 2015

Nibfiles are the old name of user interface files. They are actually .xib files now (the file extension changed a while back.) But older school developers, and the methods, still use the term nib.

Nibfiles (or XIB files, if you prefer) are still supported, but most people use storyboards instead.

If you create a user interface file in Xcode and save it with the name ChatAreaCtl.xib then you will be able to initialize it with the code you show.

The equivalent with storyboards would be to create a new scene in your storyboard that defines the interface for your view controller, then use instantiateViewControllerWithIdentifier to create an instance of that view controller (instantiateViewControllerWithIdentifier is the storyboard equivalent to initWithNibName:bundle:. Or I guess I should say UIViewController(_nibName:bundle:) for Swift. Still getting use to the syntax for function templates in Swift.)