Are view controllers with nib files broken in ios 8 beta 5?

I can’t tell whether it’s a bug or not, but it is definitely a change. Of course they might change it back… In any case, the rule in seed 5 is:

The view controller will not automatically find its .xib just because it has the same name. You have to supply the name explicitly.

So in your case any attempt to initialize this view controller must ultimately call nibName:bundle: with an explicit nib name ("TestVC"), I assume.

If you want to be able to initialize by calling

let vc = TestVC()

as you are doing in your presenting view controller, then simply override init() in the presented view controller to call super.init(nibName:"TestVC", bundle:nil) and that’s all you need (except that I presume you will also need the init(coder:) stopper discussed here).

EDIT You are absolutely right that this is a Swift-only problem. Well spotted. In Objective-C, initializing with init (or new) still works fine; the view controller finds its eponymous .xib file correctly.

ANOTHER EDIT The determining factor is not whether Objective-C or Swift calls init. It is whether the view controller itself is written in Objective-C or Swift.

FINAL EDIT The workaround is to declare your Swift view controller like this:

@objc(ViewController) ViewController : UIViewController { // ...

The name in parentheses gets rid of the name mangling which is causing the problem. It is likely that in the next release this will be fixed and you can take the @objc away again.

ANOTHER FINAL EDIT Bad news: the bug report I filed on this came back “works as intended”. They point out that all I have to do is name the .xib file after the surrounding module, e.g. if my app is called NibFinder, then if I name my .xib file NibFinder.ViewController.xib, it will be found automatically when instantiating ViewController().

That is true enough, but in my view it merely restates the bug; the Swift lookup procedure is prepending the module name. So Apple is saying I should knuckle under and prepend the same module name to my .xib file, whereas I am saying that Apple should knuckle under and strip the module name off as it performs the search.

EDIT THAT IS TRULY TRULY FINAL This bug is fixed in iOS 9 beta 4 and all these workarounds become unnecessary.

Leave a Comment