xcode 如何在iOS的静态库中添加xib文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17571366/
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
how to add xib file in static library in iOS
提问by Hymanslash
I am trying to add xib file or any other view controller file within static library but I can't do so. Can you please help me?
我正在尝试在静态库中添加 xib 文件或任何其他视图控制器文件,但我不能这样做。你能帮我么?
If possible please add the whole source code
如果可能,请添加整个源代码
their is button on first view. And when clicking on that button, new view controllers comes up with something (lets say changes in background color). How to create static library for this? so that I may use it in another project?
他们是第一个视图的按钮。当点击那个按钮时,新的视图控制器会出现一些东西(比如背景颜色的变化)。如何为此创建静态库?以便我可以在另一个项目中使用它?
Thanks in advance
提前致谢
采纳答案by Max MacLeod
Strictly speaking, it's correct that you cannot add a Xib to a static library. However, there are workarounds. Matt Galloway's iOS Library With Resources tutorial will show you how to do just that.
严格来说,您不能将 Xib 添加到静态库中是正确的。但是,有一些解决方法。Matt Galloway 的带有资源的 iOS 库教程将向您展示如何做到这一点。
See iOS Library With Resources.
请参阅带有资源的 iOS 库。
回答by Hymanslash
If you want to build your interfaces using interface builder when building a static library you will need to make a bundle and distribute it with your library.
如果您想在构建静态库时使用接口构建器构建接口,则需要制作一个包并将其与您的库一起分发。
In Xcode:
在Xcode中:
- File> New> Target
- Select "Bundle" from the OS X Framework and Library section
- Fill in the details. The bundle framework should be core foundation.
- 文件>新建>目标
- 从 OS X 框架和库部分选择“捆绑”
- 填写详细信息。bundle 框架应该是核心基础。
Then you need to get your bundle compiled at the same time as your framework. Add the bundle to the "Target Dependencies" build phase of your framework.
然后你需要在你的框架的同时编译你的包。将包添加到框架的“目标依赖项”构建阶段。
When you make your xibs you make their target this new bundle you have created.
当你创建你的 xib 时,你将他们的目标设置为你创建的这个新包。
Then when you compile your framework in the derived data directory, next to your framework binary you will find your compiled bundle. You give this to your third parties along with the framework binary.
然后,当您在派生数据目录中编译您的框架时,在您的框架二进制文件旁边,您将找到已编译的包。您将其与框架二进制文件一起提供给第三方。
So how do you reference this bundle in your code? In iOS bundles cant be loaded and your bundle will actually be inside the third party's iOS application main bundle. You can create a category on NSBundle for conveniently accessing your bundle from your code:
那么如何在代码中引用这个包呢?在 iOS 中无法加载捆绑包,您的捆绑包实际上位于第三方的 iOS 应用程序主捆绑包中。您可以在 NSBundle 上创建一个类别,以便从您的代码中方便地访问您的包:
@interface NSBundle (mybundle)
+(NSBundle *)myBundle;
@end
@implementation NSBundle (mybundle)
static NSBundle * _myBundle;
+(NSBundle *)myBundle{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSBundle * mainBundle = [NSBundle mainBundle];
NSString * pathToMyBundle = [mainBundle pathForResource:@"myBundle" ofType:@"bundle"];
NSAssert(pathToMyBundle, @"bundle not found", nil);
_myBundle = [NSBundle bundleWithPath:pathToMyBundle];
});
return _myBundle;
}
You can then access your bundle in code to load xibs like this:
然后,您可以在代码中访问您的包以加载 xib,如下所示:
UIViewController * controller = [[UIViewController alloc] initWithNibName:nil bundle:[NSBundle myBundle]];
Remember that if you use categories in your framework code you will need to make sure that your framework consumers add the -ObjC(or -all_loadif not using a recent Xcode) "other linker flag" to their projects
请记住,如果您在框架代码中使用类别,则需要确保您的框架使用者将-ObjC(或者-all_load如果不使用最新的 Xcode)“其他链接器标志”添加到他们的项目中
回答by BhushanVU
XIB is like resource and what static library is , a compiled file that contains ur classes. You can say it is your compiled code ready to be used. You can not add xib's to static library.
XIB就像资源和静态库是什么,一个包含你的类的编译文件。您可以说这是您可以使用的已编译代码。您不能将 xib 添加到静态库中。

