Reputation: 6372
My xib files are in the Assets folder of my Pod like this.
s.resources = 'Pod/Assets'
The xib files show up in my workspace in the Development Pods
group of the cocoa pod. I'm trying to access the xib like this.
[[NSBundle mainBundle] loadNibNamed:@"LabeledTextFieldView" owner:self options:nil];
But I get the following crash
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/jeff.wolski/Library/Application Support/iPhone Simulator/7.1/Applications/775EB653-519B-4FCF-8CD9-92311E205598/LT-Components-iOS.app> (loaded)' with name 'LabeledTextFieldView''
Why is the xib file not found in the bundle?
Upvotes: 15
Views: 11786
Reputation: 567
you need to all all nibs in {podName}.podspec
s.resource_bundles = {
'{podName}' => ['{podName}/Classes/*.xib']
}
and when you need to declare new view
NSBundle *podBundle = [NSBundle bundleForClass:[viewController class]];
id data = [podBundle URLForResource:@"{podName}" withExtension:@"bundle"];
NSBundle *bundle = [NSBundle bundleWithURL:data];
viewController *view = [[viewController alloc]initWithNibName:@"viewController" bundle:bundle];
replace {podName} to your project name
Upvotes: 7
Reputation: 8453
You can use s.resources
with in your library podspec. Add s.resources = 'FOLDER_INCLUDES_XIB_FILES/*.xib'
. And to initiate your class with xib file
Objective-C
NSBundle *bundle = [NSBundle bundleForClass:self.classForCoder];
UINib *nib = [[bundle loadNibNamed:@"YOUR_XIB_FILE_NAME" owner:nil options:nil] firstObject];
Swift 3 and Swift 4
let bundle = Bundle(for: self.classForCoder())
return bundle.loadNibNamed("YOUR_XIB_FILE_NAME", owner: nil, options: nil)?.first
Upvotes: 6
Reputation: 7434
In here you need to add this into your pod spec.
s.resources = "YourProjectName/*.xib"
s.resource_bundles = {
'YourProjectName' => [
'Pod/**/*.xib'
]
}
Then retrieve the xib file from the bundle like this:-
let podBundle = Bundle(for:YourClassName.self)
if let bundleURL = podBundle.url(forResource: "YourProjectName", withExtension: "bundle") {
if let bundle = Bundle(url: bundleURL) {
let cellNib = UINib(nibName: "nibFileName", bundle: bundle)
navigationTableView.register(cellNib, forCellReuseIdentifier: "nibFileReuseIdentifierName")
} else {
assertionFailure("Could not load the bundle")
}
} else {
assertionFailure("Could not create a path to the bundle")
}
Upvotes: 0
Reputation: 26906
and refer to Keith Smiley's answer, I go to docs have a look:
I finnally change the bundle init method from Class
like this:
NSBundle *bundle = [NSBundle bundleForClass:[EBBannerView class]];
NSArray *banners = [bundle loadNibNamed:@"EBBannerView" owner:nil options:nil];
Then it works fine.
Upvotes: 2
Reputation: 6372
The s.resources
folder will not automatically recurse to subdirectories. I had to specify the full relative path to the xib files.
Upvotes: 8
Reputation: 63954
The issue is that your resource file is not being included in the mainBundle
you should look at some of the NSBundle
docs and use something like bundleForClass:
to get the correct bundle to load the xib
from. The output of [NSBundle allBundles
may also be informative.
Upvotes: 6