25

I'm trying to test an app I'm developing on my iPhone. To do that I changed the target from Simulator to Device on Xcode. The application is correctly uploaded to the device and it works. The main view is shown but if I try to open a secondary view, the application crashes.

On the iPhone log (I installed the iPhone configuration utility to see the console [is the only way to see the log from iPhone?]) I can see this error:

Could not load NIB in bundle

But, on the simulator it works fine. What's wrong? Any ideas?

jvperrin
  • 3,368
  • 1
  • 23
  • 33
Andrea Girardi
  • 4,337
  • 13
  • 69
  • 98

16 Answers16

62

I had the same problem and fixed it like so:

  1. Open XCode Target
  2. Click the "Build phases" tab
  3. Click the "Copy bundle resources" section
  4. Click the +
  5. Add the missing Nib file
Iftach Orr
  • 621
  • 1
  • 5
  • 2
  • you can drag .xib file from Resources to the Copy Bundle Resources – efeyc Apr 25 '12 at 11:38
  • 1
    For me, the .xib is included as is (not compiled to nib), therefore it crashed, on another project (where it doesn't crash), it is compiled as .nib... any idea how to get it compiled? I've tried this way but xcode just package the .xib file :( – Zennichimaro Apr 05 '13 at 07:12
22

I've found that sometimes the device is case sensitive and the simulator is not.

What's the filename of your xib?


or

Try uninstalling the app from the simulator and installing it again - the simulator might have an old file left over from a previous run of the app - have you renamed / moved the xib at all during development?

deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • The file name is singleView.xib (.h and .m) and I've never renamed or moved the file but I created it on another folder (not class) – Andrea Girardi Nov 11 '10 at 16:09
  • Can you edit your question and put the line of code where you try to load the nib file? – deanWombourne Nov 12 '10 at 15:44
  • 4
    Your answer about the device being case sensitive was spot on. I had a a class named XYZViewController.h/.m and a xib called XYZviewController - the disparity of the case caused the app to run fine on the simulator but crash when I attempted to open the xib on the device. Changing the name (of the xib in this case) cured the issue. – FractalDoctor Jan 27 '11 at 11:48
  • 2
    Annoying isn't it! Glad I could help. – deanWombourne Jan 27 '11 at 13:17
  • +1 for uninstalling and installing again... thanks! I didn't get an error, but it was loading a non-localized (older) version of the NIB/XIB. – beetstra Dec 09 '11 at 16:52
  • Thanks, this was my problem in my particular case. – Shizam Feb 22 '12 at 22:01
  • Oh dear Jesus, thank you @deanWombourne you have just saved me possibly hours of banging my head against the wall. Had a case-sensitivity error. – n13 Mar 28 '12 at 15:27
  • Case sensitivity applies to the ~ipad/~iphone extensions too. MyViewController~iPad.xib loads fine on the simulator, but fails with exception on the device. The correct name is MyViewController~ipad.xib. – Vladimir Grigorov Aug 14 '12 at 08:35
  • in my case, it was the difference between aboutMe.xib and AboutMe.xib! THANK YOU for pointing this out – software evolved Aug 20 '12 at 15:09
18

I had a similar problem and was getting the same error. Turns out I was using the full name of the xib file in the attributes panel under "NIB Name:"

Don't use "SomeViewController.xib", just use "SomeViewController" without the ".xib" extension.

markquezada
  • 8,444
  • 6
  • 45
  • 52
10

I had the same problem when invoking initWithNibName:@"MyViewController"

Changing the invocation to initWithNibName:NSStringFromClass([MyViewController class]) worked well

jvperrin
  • 3,368
  • 1
  • 23
  • 33
yggdraa
  • 2,002
  • 20
  • 23
  • I was encountering this exception even while running on the simulator. This answer was exactly what I needed to do to solve the problem – Dan F Oct 14 '11 at 18:36
9

You can try these things:

  1. Make sure case is correct
  2. Use xib filename without the ".xib" extension
  3. Remove any -(dash) or other special chars, use _(underscore)
  4. Remove the xib file from the project and add back to the Xcode project
  5. Check the build settings and make sure deployment is changed to currect SDK
Sharjeel Aziz
  • 8,495
  • 5
  • 38
  • 37
darshansonde
  • 491
  • 1
  • 8
  • 13
5

In my case, the xib simply wasn't being copied into the bundle. Once I added the xib into the "Copy Bundle Resources" step for the application target, everything went fine.

FreeAsInBeer
  • 12,937
  • 5
  • 50
  • 82
3

For me, the problem was exactly what FreeAsInBeer said: the nib somehow was missing from the bundle, whereas it had been there before and worked properly in previous versions of the app.

To fix the problem, I did the following:

  1. Select the project in the Project Navigator.
  2. In the project settings select Build Phases.
  3. Expand Copy Bundle Resources.
  4. Scroll down (if necessary) and click on the + button.
  5. In the dialog that appears, click on the Add Other... button.
  6. Browse for the missing .XIB file in the file system.
  7. Build and Run... the missing nib should appear when you run it.

This is just what worked for me when I got that message. Your mileage may vary, since circumstances are not always the same for everyone, but I hope this helps someone who runs into this issue.

-Evan

Evan K. Stone
  • 1,169
  • 11
  • 15
3

I finally managed to solve that issue with these two steps:

  1. In the view controller files inspector, remove any localization (just for testing)
  2. Ensure that you have checked the correct target membership

I don't know why it works on the simulator :(

Alex
  • 31
  • 1
2

A simple mistake that might cause this error is trying to initWithNibName on an imported viewController when you are using a storyboard rather than individual Nib files. If this is the case then just init the controller instead of trying to init with a nib that doesn't exist, or init those fields with nil.

If Storyboard and no nib, change initWithNibName: bundle: to just be init OR initWithNibName:nil bundle:nil

Chris Klingler
  • 5,258
  • 2
  • 37
  • 43
2

I ran into the same problem. In my case the nib name was "MyViewController.xib" and I renamed it to "MyView.xib". This got rid of the error.

I was also moving a project from XCode 3 to 4.2. Changing the Path type did not matter.

vesselhead
  • 331
  • 4
  • 13
  • Can you be careful when posting exact duplicate answers. These tend to be flagged as spammy. – Kev Jun 30 '11 at 07:53
  • Hmm.. Is it a dup? I don't see it as a dup. I didn't see another answer where the name of the nib need to not have the "Controller" in it. Happy to delete if the answer was in here already. – vesselhead Jul 01 '11 at 21:14
2

I had the same error message. My problem, however was that my language settings on my phone were set on "English" and the Region on "United Kingdom". However, the file that could not be loaded was placed in the de.lproj directory. Moving the file into the root directory solved it.

Burny
  • 21
  • 1
1

I also had this problem when I loaded a child view controller from initWithCoder, which is a good place to initialise, but keep in mind you have to add it as an actual child AFTER the view has loaded.

So shift something like this:

[self addChildViewController: self.pageViewController];
[self.view addSubview: self.pageViewController.view];
[self.pageViewController didMoveToParentViewController:self];

...out of your init method and into viewDidLoad. Not saying this is your problem in particular but this may be helpful to others.

PostCodeism
  • 1,070
  • 1
  • 12
  • 20
1

For what its worth, I received this error when one of my tab bar buttons had the wrong class assigned to it.

bbeckford
  • 4,467
  • 6
  • 34
  • 48
1

I've got it to run with delete all of the localization from the xib In the right window. Maybe the file is in the localization folder.

Michael
  • 41
  • 3
1

I've the same problem. And my solution is remove all Localizations for the view.

JSanchez
  • 35
  • 6
1

I had exactly the behavior you described: works on simulator but get the "Could not load NIB in bundle" when running on the device, and the app remain stuck on the launch image.

In my case the problem was about the MainWindow.xib file that Xcode automatically created with English localization. I am supporting English and Italian in my app and realized that I was missing the localized version of MainWindow.xib for the Italian language.

Because I had no need to localize this file (it's Xcode default to create it localized) I fixed the problem by simply removing the English localization, so the same file is used independently of the localization. Another way to fix the problem would be to add the missing localized version, if you need it.

The app was crashing on the device because my device is configured for Italian language. The simulator instead was set to English and this is why the app run correctly. Just to verify, I set the simulator to Italian language and the app crashed confirming the localization problem.

Gengis
  • 171
  • 1
  • 9