NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle

元气小坏坏 提交于 2019-11-26 17:28:29
Daniel C.

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.

Simply try removing the ".xib" from the nib name in "initWithNibName:". According to the documentation, the ".xib" is assumed and shouldn't be used.

Well, I think the error says that it can't find a nib file named "RootViewController" in your project.

You are writing these lines of code,

self.viewController = [[RootViewController alloc]      initWithNibName:@"RootViewController_iPhone.xib" bundle:nil];

self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController_iPad.xib" bundle:nil];

At the same time you are asking it to load a nib file named "RootviewController"..!! Where is it..? Do you have a xib file named "Rootviewcontroller"..?

I've gone through the same issue quite a number of times. What I've found for myself is that the xib file i'm linking would always be misspelled. In my case, this is the line of code for me that lead to the exception:

*NSArray nib = [[NSBundle mainBundle] loadNibNamed:@"shelfcell" owner:self options:nil];

The "shelfcell" was my xib file name. But i had mis spelled it as "ShelfCell", "shelfCell" etc, which lead to the exception. So dont bug your head much. Check the lines of code and the spellings. Thank you

I've got some subprojects.

I solved this problem via adding xib file to Copy Bundle Resources build phase of main project.

This can also occur if you change the name of your xib file, and you forget to change all the places it is referenced.

Example: MainWindow.xib is a TabBarController that contains a custom ViewController named FirstViewController with associated xib file FirstView. You change the name to SecondViewController and associated xib file to SecondView.

Solution:

Open MainWindow.xib (that contains the TabBarController):

  • Select the tab that is for the ViewController you changed.
  • On the Identity inspector, Change the Custom Class name to SecondViewController.
  • On the attributes inspector, Change the xib name to SecondView.

Open SecondView.xib (this was copied from FirstView.xib):

  • On the Identity Inspector, change the custom class to SecondViewController.

Optionally you may need to:

  • Right click SecondView.xib, verify that "Identity and Type->Target Membership" is enabled.
  • Make sure "Build Phases -> Copy Bundle Resources" shows your SecondView.xib
  • I also found I occasionally needed to build clean and delete the app from the simulator to get the xib file to actually update. The simulator has an internal cache for xib files that is a little inconsistent.

For me,

I did not install that nib in required target.

Step 1: Click on your nib in Project Navigator (i.e. In First left tab)

Step 2: Go to File Inspector (i.e. In First right tab)

Step 3: Go to Target membership and tick on target in which you want to use your nib.

Imran

There are so many reasons for this error.I also got one and solved it.

It may be possible that you are adding a third party framework and not including it in the Copy Bundle Resources.That solved the problem for me.

Do this as follows. Go to Target -> BuildPhases -> CopyBundleResources -> Drag and drop your framework and run the code.

I had renamed a xib and had this problem, the solution for me was to edit the main window.xib manually and change the offending text

search for the offending xib name in the relevant xib ( view as source code)

changed

<viewController nibName="OldNibName" id="116" customClass="SomeViewController">

with

<viewController nibName="NewNibName" id="116" customClass="SomeViewController">

and it worked

Also make sure that the XIB-file is included in your target. Check the file inspector when the XIB-file is selected (first tab on the right, in the middle there is section "target membership". The target you build for must be checked, otherwise the XIB-file won't be included.

I was setting an optional self.collectionView?.backgroundColor in the init(coder: constructor of my collection vc.

Once I will have moved that setup in viewDidLoad the exception had somehow disappeared. puzzling.

I spend a hour finding out what was wrong.. But Clean Project did the trick.

Build -> Clean All

The accepted solution is probably also working.. but this was enough for me.

While developing the ios app i too face similar kind of issue. Simple Restart of the xcode works for me. Hope this will help some one.

I tried several things, finally what worked for me was to delete (and move to trash) the .xib file in question. Then re-create it. To make things easier, I copied the stuff in the .xib temporarily to another .xib, then copied it back into the newly created one.

Rahul Panzade

I have faced this error a lot of time, also research why this error occurs and finally, I caught this error, this error a silly error which occurs when we misspelled .xib file name.

NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];

At this point, I misspelled TableViewCell as TabelViewCell , because the perception of some time we read it as correct, this is the most common problem. Just copy your .xib file name, and paste it.

I was getting this error when I was trying to add gestureRecognizer to my ViewController's view and the target was view, instead of being self:

Instead of:

self.view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(handleTap(_:))))

Fixed to:

self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap(_:))))

And the error was gone.

Note that if you are using [self class] nibName] can return different name if you are using swift... might be different than the name you are expecting it to return.

Especially if you have multiple targets for your code.

In that case try to override:

override var nibName: String?{
        return "YourXibFileName" // No .xib at the end :)
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!