Goje87
Goje87

Reputation: 2869

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?

Upvotes: 138

Views: 215302

Answers (30)

Void Asif
Void Asif

Reputation: 145

I did not made cell xib and by mistake i registered xib and that was giving me error. Removing register xib code works for me.

Upvotes: 0

Chirag Lukhi
Chirag Lukhi

Reputation: 1546

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!!

Upvotes: 8

sevenpounds
sevenpounds

Reputation: 2490

Visit the properties of the .xib files in the file inspector,the property "Target Membership" pitch on the select box, then your xib file was linked with your target

Upvotes: 249

Muhammad Essa
Muhammad Essa

Reputation: 329

In my case the spellings of XIB was incorrect. Please make sure the spellings are correct. Bundle.main.loadNibName("XIB_Name" , owner : self , options :nil)

Upvotes: 1

Ronaldo Albertini
Ronaldo Albertini

Reputation: 1339

In my case, I was creating a framework with Cocoapods. The problems was this line:

s.static_framework = true

in my framework.podspec file

After commented this line above all problems to access storyboards or XIBs went out.

Upvotes: 0

Oletha
Oletha

Reputation: 7649

For me, the case was that I was searching the main bundle for a nib which wasn't in the main bundle. My nib was in an imported module so I needed to retrieve the bundle for that module and use it to init the UINib, rather than using Bundle.main.

Upvotes: 0

Marjan Basiri
Marjan Basiri

Reputation: 214

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")

Upvotes: 2

swiftBoy
swiftBoy

Reputation: 35783

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.

enter image description here

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)
            }
        }

Upvotes: 5

kunalg
kunalg

Reputation: 1580

In Targets -> Build Phases

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

Upvotes: 90

norbDEV
norbDEV

Reputation: 5345

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
} 

Upvotes: 7

oskarko
oskarko

Reputation: 4178

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

Upvotes: 4

eemmrrkk
eemmrrkk

Reputation: 1700

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.

Upvotes: 1

coolcool1994
coolcool1994

Reputation: 3804

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];

Upvotes: 7

Tom Howard
Tom Howard

Reputation: 4908

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>>.MyCustomViewand 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];

Upvotes: 8

hhamm
hhamm

Reputation: 1571

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.

Upvotes: 3

Will Larche
Will Larche

Reputation: 3149

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!

Upvotes: 8

chings228
chings228

Reputation: 1889

try to find out all

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

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

Upvotes: 23

Kevin
Kevin

Reputation: 1903

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.

Upvotes: 2

david72
david72

Reputation: 7297

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];

Upvotes: 1

Narasimha Nallamsetty
Narasimha Nallamsetty

Reputation: 1263

This is worked for me.. Make sure you typed nib name correctly.

[[NSBundle mainBundle]loadNibNamed:@"Graphview" owner:self options:nil];

Graphview(nib name)

Upvotes: 0

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.

Upvotes: 2

NSPratik
NSPratik

Reputation: 4846

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.

Upvotes: 0

Ezeki
Ezeki

Reputation: 1519

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

Upvotes: 0

Dave
Dave

Reputation: 4436

I had same problem, renaming my view controller identifier in storyboard worked for me.

Upvotes: 0

Hanuman
Hanuman

Reputation: 642

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];

Upvotes: 14

Tibidabo
Tibidabo

Reputation: 21571

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

Upvotes: 14

NetDeveloper
NetDeveloper

Reputation: 519

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.

Upvotes: 5

krafter
krafter

Reputation: 1432

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.

Upvotes: 2

cloudsurfin
cloudsurfin

Reputation: 2577

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

Upvotes: 0

Jonathan Starr
Jonathan Starr

Reputation: 254

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.

Upvotes: 3

Related Questions