xcode iOS8:如何从动态框架加载 xib

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27813100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:27:39  来源:igfitidea点击:

iOS8: How to load xib from Dynamic Framework

iosxcodecocoapods

提问by Chris Conover

I am using Cocoapods pre-release 0.36 to build and deploy a private dynamic frameworkinto an app bundle. I am using the resources attribute to copy xibs, and they show up in the app bundle.

我正在使用 Cocoapods 预发布 0.36 来构建私有动态框架并将其部署到应用程序包中。我正在使用资源属性来复制 xib,它们出现在应用程序包中。

s.subspec 'Views' do |ss|
    ...
    ss.resources = ['All/My/Folders/**/*.{xib,png}']
    ...

I am trying to load reusable controls from the xibs like this:

我正在尝试从 xib 加载可重用控件,如下所示:

NSBundle.mainBundle().loadNibNamed("MyXib", owner:loader, options:nil)

NSBundle.mainBundle().loadNibNamed("MyXib", owner:loader, options:nil)

but this is failing as it cannot find the xib in my embedded sub framework. The actual xib (nib) paths follow the pattern:'

但这失败了,因为它在我的嵌入式子框架中找不到 xib。实际的 xib (nib) 路径遵循以下模式:'

./Frameworks/MyPrivateFramework.framework/MyXib.nib

./Frameworks/MyPrivateFramework.framework/MyXib.nib

My question is, is there a simple, code agnostic way of loading the xib that will work when packaged in an iOS 8 framework - or do I have to explicitly hardcode in the above path? The above loading code worked until I think the dynamic framework change.

我的问题是,是否有一种简单的、代码不可知的加载 xib 的方式,当打包在 iOS 8 框架中时可以工作 - 或者我是否必须在上述路径中明确硬编码?上面的加载代码一直有效,直到我认为动态框架发生了变化。

Thanks

谢谢

回答by Chris Conover

From this post here:

从这里的帖子:

http://artsy.github.io/blog/2015/01/04/cocoapods-and-frameworks/

http://artsy.github.io/blog/2015/01/04/cocoapods-and-frameworks/

I arrived at the agnostic solution of getting the framework bundle from a class within, ie:

我找到了从类中获取框架包的不可知解决方案,即:

NSBundle(forClass: ClassInFramework.self)

and then loading the xib from that:

然后从中加载xib:

NSBundle(forClass: ClassInFramework.self)
    .loadNibNamed("ClassInFramework", owner:loader, options:nil)

and that worked.

这奏效了。