I get a'Could not load NIB in bundle:' error when using xib inside my Xcode6 framework

Question: Question:

I created my own framework with Xcode6.
I have code using xib in a framework project.
When I create a demo app, add my own framework to it, and call it, the following error occurs.

2014-11-27 19:09:25.323 SampleApp[5709:1563355] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </private/var/mobile/Containers/Bundle/Application/6FDD2A14-A4A8-47FC-88B2-5DF48546F72D/SampleApp.app> (loaded)' with name 'SampleView''
*** First throw call stack:
(0x1824cde48 0x192c140e4 0x1824cdd88 0x1870874f0 0x1000d7360 0x1000d72dc 0x1000d7fb8 0x100100df0 0x100101cfc 0x1000d7f48 0x1000d80e8 0x10006d130 0x186f0b54c 0x186f0f724 0x186f13a6c 0x186f12f48 0x186f12ea0 0x186f067d4 0x18a729640 0x182486124 0x18248522c 0x182483850 0x1823b11f4 0x18b53f5a4 0x186ce2784 0x10006e504 0x193282a08)
libc++abi.dylib: terminating with uncaught exception of type NSException

Probably because the nib file cannot be found.
Does anyone know a good way to solve this?

So far, there are two solutions I'm thinking of

  • Write in code without using xib
  • Add xib to the demo project itself (in this case you will likely distribute the nib file separately when you distribute the framework)

Answer: Answer:

Within the Framework you need to find the NIB from the Framework Bundle. So, in the class in Framework, if it is Swift, code as follows.

public class ViewLoader {

    public class func myView() -> UIView {
        let nib = UINib(nibName: "MyView", bundle: NSBundle(forClass: self))
        let view = nib.instantiateWithOwner(nil, options: nil).first as UIView
        return view
    }

    public func myView() -> UIView {
        let nib = UINib(nibName: "MyView", bundle: NSBundle(forClass: ViewLoader.self))
        let view = nib.instantiateWithOwner(nil, options: nil).first as UIView
        return view
    }

    public init() {

    }

}
Scroll to Top