How to download xib file from my cocoapod?

My xib files are in the Assets folder of my Pod, like this.

s.resources = 'Pod/Assets' 

xib files are displayed in my workspace in the Development Pods group of the Development Pods module. I am trying to access xib like this.

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

But I get the next 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?

+6
source share
6 answers

The s.resources folder s.resources not automatically return to subdirectories. I had to specify the full relative path to xib files.

+4
source

The problem is that your resource file is not included in the mainBundle , you should look at some of the NSBundle docs and use something like bundleForClass: to get the correct package to load xib from. Conclusion [NSBundle allBundles may also be informative.

+6
source

I ran into this problem, enter image description here

and refer to Keith Smiley, I refer to the documents:

I am finally modifying the package initialization method from Class as follows:

 NSBundle *bundle = [NSBundle bundleForClass:[EBBannerView class]]; NSArray *banners = [bundle loadNibNamed:@"EBBannerView" owner:nil options:nil]; 

Then it works great.

+1
source

you need all all the nibs in {podName} .podspec

 s.resource_bundles = { '{podName}' => ['{podName}/Classes/*.xib'] 

}

and when you need to declare a 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} with your project name

+1
source

You can use s.resources in your podspec library. Add s.resources = 'FOLDER_INCLUDES_XIB_FILES/*.xib' . And start 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 
0
source

Here you should add this to your pod specification.

  s.resources = "YourProjectName/*.xib" s.resource_bundles = { 'YourProjectName' => [ 'Pod/**/*.xib' ] } 

Then extract the xib file from the package as follows: -

  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") } 
0
source

Source: https://habr.com/ru/post/974173/


All Articles