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

21

60

In my AppDelegate there is a problem I do not understand. RootViewController initially called ViewController and I changed it name. The application is formed by many ViewController then I have introduced a UINavigationController. Why this error comes?

  NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle 
  /Users/XXXXXXXXXXXX/Library/Application Support/iPhone simulator/6.0/Applications/
  B7A7D461-1CFE-4B05-AF32-00B65FCFFF49/XXXXXXXXXX.app> (loaded)'with name 
 'RootViewController''
  
  *** First throw call stack:
  (0x1992012 0x1357e7e 0x1991deb 0x4bafac 0x37fe37 0x380418 0x380648 0x380882 0
  x380b2a  0x397ef5 0x397fdb x398286 0x398381 0x398eab 0x398fc9 0x399055 0x49e3ab 
  0x2ef92d 0x136b6b0 0x1f12fc0 0x1f0733c 0x1f12eaf 0x38e8cd 0x2d71a6 0x2d5cbf 
  0x2d5bd9 0x2d4e34 0x2d4c6e 0x2d5a29 0x2d8922 0x382fec 0x2cfbc4 0x2cfdbf 
  0x2cff55 0x2d8f67 0x2b30 0x29c7b7 0x29cda7 0x29dfab 0x2af315 0x2b024b 0x2a1cf8 
  0x1dbedf9 0x1dbead0 0x1907bf5 0x1907962 0x1938bb6 0x1937f44 0x1937e1b 0x29d7da
  0x29f65c 0x269d 0x25c5) 
  ibc++abi.dylib: terminate called throwing an exception
  (lldb) 

This is the code in AppDelegate.h

  #import <UIKit/UIKit.h>

  @class RootViewController;

  @interface AppDelegate : UIResponder <UIApplicationDelegate>{

       UINavigationController *navigationController;
   }

  @property (strong, nonatomic) UIWindow *window;

  @property (strong, nonatomic) RootViewController *viewController;
  @property (strong, nonatomic) UINavigationController * navigationController;



  @end

This is the code of AppDelegate.m

  #import "AppDelegate.h"

  #import "RootViewController.h"



  @implementation AppDelegate

  @synthesize navigationController;
  @synthesize viewController;
  @synthesize window;



  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
 {
         self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.

         if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
               self.viewController = [[RootViewController alloc]      initWithNibName:@"RootViewController_iPhone.xib" bundle:nil];
   } else {
    self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController_iPad.xib" bundle:nil];
   }

    RootViewController *rootMenu=[[RootViewController alloc]initWithNibName:@"RootViewController" bundle:nil];

    self.navigationController =[[UINavigationController alloc]initWithRootViewController:rootMenu];

    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}
Brighten answered 4/10, 2012 at 7:42 Comment(5)
Try cleaning your project from Product-> Clean. Worked for me.Saltire
Re- running the project worked for me,Walton
I had force language feature, so I changed the bundle. after changing the language, I could load localized strings and HTML files. But for a custom xib file, i'm facing this issue("Could not load NIB in bundle"). Pls advice what I missed?Gnosticism
for storyboard this is what worked for me https://mcmap.net/q/166352/-could-not-load-nib-in-bundleDodds
Cleaning and building worked for me, thanks!Baltic
B
136

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.

Babettebabeuf answered 18/12, 2012 at 10:38 Comment(5)
For me the files in question weren't even in the project any more. I essentially followed this advice, copying them back in. However, it then threw up key-coding compliance errors. I deleted every outlet from the re-added views and voila it worked.Getter
This happens even if you RENAME INSIDE Xcode. Xcode 5.x is truly vile.Berghoff
This just happened to me the other day where I received the NSInternalInconsistencyException and could not load NIB in bundle error. I was only able to get around it by following the advice of Daniel C. to remove the reference and then put it back.Whaling
Doesn't work for me. I am trying to restructure my codes. This makes no sense at all, I could not even find where the old file location was mentioned, but it just cannot find them.Monkish
Ahah, I forgot the same filename limitation…Monkish
I
18

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

Intaglio answered 15/3, 2013 at 20:17 Comment(0)
S
16

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"..?

Sizar answered 4/10, 2012 at 8:8 Comment(6)
Yes, I have a nib file named RootViewControllerBrighten
k.. so is your nib file included in the project structure..?? I mean, is it visible in the project navigator..? If it is, then try deleting it (use 'remove references only' while deleting) and drag it to xcode again from the finder. clean and build it.Sizar
and one more thing, make sure that your nib file is present in the "copy bundle resources" under "build phases".Sizar
Well, one more thing, just double check the spelling of the name of the nib file.. coz some times its the simplest thing that v over look..:pSizar
You don't need to specify the .xib extension. If you do it won't be foundFlew
I normally get this error when the NIB is in the project structure but I forget to add it to the target.Shippee
S
14

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.

Stripe answered 30/9, 2015 at 5:33 Comment(1)
Solved app crash issue.Flair
I
10

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

Indissoluble answered 1/8, 2013 at 9:29 Comment(1)
This helped me with my issue. In my case, in Swift, I was trying to reference a nibName and a reuse identifier using the same static and I had the reuse identifier typed incorrectly.Wallywalnut
A
10

I've got some subprojects.

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

Alyssaalyssum answered 5/6, 2014 at 7:50 Comment(0)
T
9

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.
Tacy answered 25/1, 2014 at 21:44 Comment(1)
"Make sure "Build Phases -> Copy Bundle Resources" shows your SecondView.xib" This! This was making my app crash... Thank you very much!Protomartyr
B
5

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.

Blacking answered 6/8, 2013 at 5:7 Comment(0)
S
1

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

Superinduce answered 23/10, 2013 at 21:3 Comment(0)
P
1

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.

Potter answered 7/5, 2015 at 8:36 Comment(0)
O
1

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.

Ornie answered 14/9, 2017 at 11:2 Comment(0)
C
1

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.

Chaunce answered 30/5, 2018 at 12:26 Comment(0)
C
1

You can check file xib is same name with your file class. Default XCode will get name xib file from name file class, so if their names are different, XCode can't detect from storyboard It's work with me

Cartilaginous answered 6/10, 2019 at 0:32 Comment(0)
B
1

In my case, this happen because of I deleted the Main.storyboard and in general tab Main Interface property cleared and added custom one as Start. After I Change the Main Interface to Start.storyboard except Start it's working.

Bettis answered 30/10, 2019 at 14:13 Comment(0)
B
1

enter image description here

In Identity inspector change class name with the corresponding identifier and also In Attributes inspector modify Indentifier then it should work as expected.

Belfry answered 20/2, 2020 at 13:30 Comment(0)
P
1

The solution worked for me :

  1. Just copy the .xib file to desktop.
  2. Delete the .xib file from the project via finder
  3. Then again paste the .xib file which we copied to desktop in 1st step
  4. Now drag and drop the .xib file from finder to xcode folder

It will work now

changes in .xib file

tableViewCell contentMode="scaleToFill" restorationIdentifier="<YOUR CELL NAME>" selectionStyle="default" indentationWidth="10" rowHeight="48" id="KGk-i7-Jjw" customClass="<YOUR CELL NAME>" customModule="Enphase" customModuleProvider="target"
Platitudinize answered 16/11, 2023 at 19:4 Comment(0)
B
0

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.

Belleslettres answered 11/8, 2013 at 6:45 Comment(0)
W
0

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.

Whitener answered 14/10, 2013 at 21:39 Comment(0)
P
0

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.

Putupon answered 30/8, 2017 at 9:13 Comment(0)
U
0

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.

Unmeaning answered 26/11, 2018 at 18:39 Comment(0)
P
0

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 :)
}
Pola answered 1/5, 2019 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.