How do I load a xib file from my cocoapod?

荒凉一梦 提交于 2020-01-02 01:09:15

问题


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?


回答1:


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




回答2:


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.




回答3:


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



回答4:


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




回答5:


I run to this issue,

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.




回答6:


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


来源:https://stackoverflow.com/questions/25433110/how-do-i-load-a-xib-file-from-my-cocoapod

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!