11

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!

Michael Ninh
  • 772
  • 2
  • 10
  • 23
  • `nib` files are resource files in which you design your view/view controllers. You can read more here : https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html – Losiowaty Aug 26 '15 at 23:14
  • I've read this but was still left unclear what the exact name is. Is the nib name just the same as the controller? – Michael Ninh Aug 26 '15 at 23:24
  • It is the name of the file in your project. It needs to physically exist. If you are building your views entirely in code, there is no need to use this initializer of `UIViewController`. A plain old `init()` will suffice. – Losiowaty Aug 26 '15 at 23:26

1 Answers1

12

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.)

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Thanks this clears it up. Here is a helpful link for any newbies after me...[link](https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIStoryboard_Class/index.html#//apple_ref/doc/uid/TP40010909-CH1-SW2) – Michael Ninh Aug 26 '15 at 23:54
  • 7
    The term "nib" is historical and is an acronym for "NextSTEP Interface Builder". "xib" is an XML version of a "nib". A "nib" only represents a single viewController instead of multiple ones like a Storyboard does. – vacawama Aug 26 '15 at 23:57
  • Yup. Just like the "NS" prefix in all the Foundation classes stands for NextStep. NSObject is short for NextStepObject, for example. – Duncan C Aug 27 '15 at 00:19
  • In iOS land, XIB is pronounced "NIB" – CuriousRabbit Aug 27 '15 at 00:20