Nib load failure when presenting its view controller

276 Views Asked by At

I have this function in a class that inherits from NSObject:

open func showCustomDialogInView(vc: UIViewController) {
    let bundle = Bundle(for: CustomDialogViewController.self)
    let customDialog = CustomDialogViewController(nibName: "CustomDialogViewController", bundle: bundle)
    customDialog.delegate = vc
    customDialog.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
    vc.present(customDialog, animated: true, completion: nil)
}

I had this working in one of my iOS projects before updating to Xcode 8 and Swift 3 language, but now when I run the app I get a crash when vc.present(customDialog, animated: true, completion: nil) line is reached. I get this message in log console:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle /MyApp.app> (loaded)' with name 'CustomDialogViewController''

I don't understand what it is happening, since let customDialog = CustomDialogViewController(nibName: "CustomDialogViewController", bundle: bundle) line doesn't crash and it seems that I get a CustomDialogViewController object.

Does somebody could help me with this issue? Thanks

2

There are 2 best solutions below

0
AppsDev On BEST ANSWER

It looks like in this case this error was because for some reason the xib file was missing the appropriate target membership.

Thanks for your answers

0
Annie Gupta On

This error can occur when you rename files outside of XCode. To solve it you can just remove the files from your project (Right Click - Delete and "Remove Reference").

Then after you can re-import the files in your project and everything will be OK.