Can’t Load UIViewController XIB file in Storyboard in Swift

What’s happened is that Seed 5 broke the mechanism whereby a view controller can find its xib by name, if the view controller is a Swift class. The reason is that the name of the class, in Swift’s mind, is not the same as the name you gave it (and the name you gave the xib file); the name has been “mangled”, in particular by prepending the module name (i.e. Swift classes have namespacing).

I offer three workarounds:

  • Your workaround is a good one (load the .xib file by name explicitly)

  • Name the .xib file MyModule.TestViewController.xib, where MyModule is the name of your bundle (i.e. the name of the project) (this is what Apple advises, but I hate it)

  • Use @objc(TestViewController) before the view controller’s class declaration to overcome the name mangling which is what’s breaking the mechanism (this is the approach I favor)

See my discussion here: https://stackoverflow.com/a/25163757/341994 and my further discussion linked to from there: https://stackoverflow.com/a/25152545/341994

EDIT This bug is fixed in iOS 9 beta 4. If the nib file search fails, iOS 9 now strips the module name off the view controller class name and performs the nib file search a second time.

Leave a Comment