简体   繁体   中英

Could not load NIB in bundle

I am trying to integrate Janrain Engage as custom module with Appcelerator Titanium. I have created a sample module and dragged the JREngage folder to the sample module xcodeproj as indicated in the Jainrain's documentation.

Now I give build command to this project, then execute the ./build.py and then finally I execute the titanium run command. It launches the application in simulator with a blank screen and immediately crashes throwing the following error.

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/abhilash/Library/Application Support/iPhone Simulator/4.2/Applications/CA167346-4091-4E16-B841-955D1D391713/test.app> (loaded)' with name 'JRProvidersController''

Why could this error be occurring?

在文件检查器中访问 .xib 文件的属性,在选择框中选中“Target Membership”属性,然后您的 xib 文件与您的目标相关联

In Targets -> Build Phases

Make sure the .xib is added to Copy Bundle Resources , if it is not present then add .xib file.

try to find out all

XXXController = [[XXXControlloer alloc] initWithNibName:@"XXXController" bundle:nil];

in your code, and make sure that XXXController are spelled correctly

I had the same problem (exception 'Could not load NIB in bundle: ..') after upgrading my xcode from 3.2 to 4.02. Whereas deploying of my app with Xcode 3.2 worked fine it crashes with xcode 4 raising the exception mentioned above - but only when I tried to deploy to the IOS Simulator (v.4.2). Targeting the IOS device (v.4.1) acted also with Xcode 4.

It turned out (after hours of desperately scrabbling around) that the reason was an almost "hidden" setting in the .xib-file:

Visit the properties of the .xib files in the file inspector: The property 'Location' was set to 'Relative to Group' for all .xib files. I changed it to 'Relative to project' and voila: all .xib files now are correctly loaded in IOS simulator !

I have no clue what's the reason behind that for this odd Xcode4 behavior but maybe it's worth to make an attempt ?

In my case it was very weird (use a storyboard): For some reason it changed from "Main storyboard file base name" to "Main nib file base name" in the plist.

Changing back to "Main storyboard file base name" (UIMainStoryboardFile) solved the issue

the error means that there is no .xib file with "JRProvidersController" name. recheck whether JRProvidersController.xib exists.

you will load .xib file with

controller = [[JRProvidersController alloc] initWithNibName:@"JRProvidersController" bundle:nil];

Using a custom Swift view in an Objective-C view controller (yes, even after importing the <<PROJECT NAME>>-Swift.h file), I'd tried to load the nib using:

MyCustomView *customView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([MyCustomView class]) owner:self options:nil] objectAtIndex:0];

...but NSStringFromClass([MyCustomView class]) returns <<PROJECT NAME>>.MyCustomView and the load fails. TL;DR All worked well loading the nib using a string literal:

MyCustomView *customView = [[[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil] objectAtIndex:0];

Have look at the project

Target -> Build Phases -> Copy Bundle Resources

You will find your xib/storyborad with red color.

Just remove it.Also remove all references of missing file from project.

Now drag this storyboard/xib file again to this Copy Bundle Resources .It will still show you file with red color but dont worry about it.

Just clean and build project.

Now you will get your project running again successfully!!

I had this problem with a storyboard and the nib was called something like 'bKD-J3-fhr-view-ZSR-8m-2da'.

It was because I was trying to add a subview to self.view in a view controller's init (withCoder). Self.view doesn't exist yet.

Moved it to viewDidLoad and all better!

DO NOT PUT .xib WHEN YOU INSERT IN THE XIB NAME! IT IS ALREADY IMPLIED!

Dont do this:

 UIView *viewFromNib = [[NSBundle mainBundle] loadNibNamed:@"ResultsAssessmentView.xib" owner:self options:nil][0];

Do this:

 UIView *viewFromNib = [[NSBundle mainBundle] loadNibNamed:@"ResultsAssessmentView" owner:self options:nil][0];

For Storyboard

I tried every single solution posted here but nothing worked for me as I am using Storyboard with Swift 5.

Just started to build fresh controllers and Views in storyboard test app, what I found is I was missing My View Controller's Storyboard ID.

So here is my solution, If you want to Go from ViewController A --> View Controller B

Step 1. In storyboard : Just make sure your ViewControllerA is embedded in Navigation Controller

Step 2. In storyboard : Now check have you mentioned Storyboard ID for your ViewControllerB which you will use in code as Identifier.

在此处输入图片说明

Step 3. and finally make sure you are pushing controller this way in your ViewControllerA with Button click.

if let viewControllerB = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB {

            if let navigator = navigationController {
                navigator.pushViewController(viewControllerB, animated: true)
            }
        }

With the build target being one of my iOS devices, I right-clicked the Product file (xyzw.app) and selected the Show in Finder popup item. It opened a window with the xyzw.app inside; I opened the bundle using Show Package Contents and saw all the files I expected, except one file, the one it was complaining about, with a capital I instead of a lower-case i in the name (zoomieVIew.nib instead of zoomieView.nib). I had noticed the uppercase I in the file name of the xib, and changed it and rebuilt; apparently Xcode left the generated .nib name the way it was. I deleted zoomieVIew.nib in the bundle, rebuilt, and Xcode duly recreated the file as zoomieView.nib. The app started to work on the device.

It happens when you rename the nib file. If you have already, create new nib(meaning copy current nib file contents to new nib), delete old nib file and it will solve your problem.

Edit: With new Xcode starting version 4.6.3, If you rename(with refactor feature) Controller class, it will rename nib file too and you need not worry about nib loading problem.

Swift4 example if your MyCustomView.swift and MyCustomView.xib in a framework. Place this in the MyCustomView 's init:

let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "MyCustomView", bundle: bundle)
if let nibView = nib.instantiate(withOwner: self, options: nil).first as? UIView {
    self.aViewInMyCustomView = nibView
    self.aViewInMyCustomView.frame = self.frame
    self.addSubview(self.aViewInMyCustomView)
    // set constraints
    self.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint(item: self.aViewInMyCustomView, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1.0, constant: 0).isActive = true
    NSLayoutConstraint(item: self.aViewInMyCustomView, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1.0, constant: 0).isActive = true
    NSLayoutConstraint(item: self.aViewInMyCustomView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0).isActive = true
    NSLayoutConstraint(item: self.aViewInMyCustomView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0).isActive = true
} 

I also found that it failed when I tried to load the XIB with a name like @"MyFile.xib". When I just used @"MyFile" it worked - apparently it always adds the extension. But the error message just said it couldn't find MyFIle.xib in the bundle - if it had said MyFile.xib.xib, that would have been a big clue.

Your XIB file is probably outside your project folder. This leads to not showing up the Target Inspector. However, moving the XIB file into your project folder should fix it.

Be careful: Xcode is caseSensitive with file names. It's not the same "Fun" than "fun".

Working on Xcode 4.6.3 and localizing my NIB files I also run into this issue.

For me nothing else helped besides changing "Document Versioning" in File Inspector to Deployment 5.0 instead of 6.1.

I just had an interesting experience using Xcode 6.3.

I kept getting this error also, despite trying everything you would normally think of with spelling, target membership, etc. as suggested above. I also tried cleaning, deleting derived data, and also deleting the app from the simulator several times to ensure the bundle was being built correctly but to no avail.

Finally, following Brian Michael Bentley's answer, I finally decided to inspect my .app file in my simulator's folder on my HD. I found that all of my nibs were there but with a abc~ipad.nib instead of the expected abc.nib. I manually renamed all of these files to remove the ~ipad part, built and it worked!

Trying to see why these have been appended with the ~ipad keyword, I looked at my project settings and in fact, in my General>Deployment Info tab, I had only iPad selected. I was trying to run on an iPhone simulator. I believe that in the past, Xcode would give an error indicating that the binary did not support iPhone and you would not succeed in running the app.

I deleted the app from the simulator and did the same thing again - again with only iPad supported. This time, the .app contained abc~iphone.nib AND abc~ipad.nib for each expected storyboard and it ran on the iPhone simulator just fine. Again - If we choose iPad only in our Deployment Info settings, it shouldn't run on iPhone Simulator. This is an Xcode bug.

So, there is some inconsistent behavior here on the part of Xcode and unfortunately it's an intermittent bug and this may be difficult to reproduce, but I put this here so that it may help others in the future.

Had this same issue nothing worked, so I figured something weird with derived data.

In xcode 6.3.2 I had to:

In XCode Menu --> Preferences --> Locations Tab --> In Locations Change Derived Data to Relative and then toggle back to Default

Then the nib loaded fine.

In case you are using frameworks in your project, you need to make sure you are loading from the correct bundle:

NSBundle *bundle = [NSBundle bundleWithIdentifier:@"<your bundle id here>"];
ABCViewController *vc = [[ABCViewController alloc] initWithNibName:@"<your nib name>" bundle:bundle];

I've got same issue and my .xib file has already linked in Target Membership with the Project. Then I unchecked the file's target checkbox and checked again, then Clean and Build the project. Interestingly it has worked for me.

for me, it solved just by changing the name of my cell's file into the same as its class.

In Attributes Inspector(Third tab on the right side bar in story board):

  1. Set the cell's Identifier( for example: "MyCellId"),
  2. Set the cell's Class (for example: "MyCellClass" when the file is "MyCellClass") ,
  3. Register the cell in your view controller as follow:

    tableView.register(UINib(nibName: "MyCellClass", bundle: nil), forCellReuseIdentifier: "MyCellId")

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.

Every time I refactor a view controller's name that's in my appDelegate I waste time on this. Refactoring doesn't change the nib name in initWithNibName:@"MYOldViewControllerName".

我有同样的问题,在故事板中重命名我的视图控制器标识符对我有用。

I noticed that it may happen if you switch between branches in git and forget to make a clean. So xib is there and everything is find, but the exact build may have problems. So just in case don't forget to make a clean

SecondViewController *secondViewController = [[SecondViewController alloc]initWithNibName:@"SecondView.xib" bundle:nil];
    [self.navigationController pushViewController:secondViewController animated:YES];

In the above code, if you also give a file extension like "SecondView.xib", then it's wrong and will give you above error. Use "SecondView" instead. I have made that mistake.

I am trying to integrate Janrain Engage as custom module with Appcelerator Titanium. I have created a sample module and dragged the JREngage folder to the sample module xcodeproj as indicated in the Jainrain's documentation.

Now I give build command to this project, then execute the ./build.py and then finally I execute the titanium run command. It launches the application in simulator with a blank screen and immediately crashes throwing the following error.

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/abhilash/Library/Application Support/iPhone Simulator/4.2/Applications/CA167346-4091-4E16-B841-955D1D391713/test.app> (loaded)' with name 'JRProvidersController''

Why could this error be occurring?

将我的旧代码从 XCode 3x 转换为 XCode 4 时遇到此问题,只需将 wwwwwwww.xib 重命名为 RootViewController.xib 即可解决此问题

另一个原因可能是当您弄乱本地化时,文件在错误的语言特定文件夹中查找。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM