简体   繁体   English

如何在iOS中的静态库中添加xib文件

[英]how to add xib file in static library in iOS

I am trying to add xib file or any other view controller file within static library but I can't do so. 我试图在静态库中添加xib文件或任何其他视图控制器文件,但我不能这样做。 Can you please help me? 你能帮我么?

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 提前致谢

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中:

  1. File> New> Target 文件>新建>目标
  2. Select "Bundle" from the OS X Framework and Library section 从OS X Framework和Library部分选择“Bundle”
  3. Fill in the details. 填写详细信息。 The bundle framework should be core foundation. 捆绑框架应该是核心基础。

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. 将包添加到框架的“Target Dependencies”构建阶段。

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. 在iOS捆绑包中无法加载,您的捆绑包实际上将在第三方的iOS应用程序主捆绑包内。 You can create a category on NSBundle for conveniently accessing your bundle from your code: 您可以在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_load if not using a recent Xcode) "other linker flag" to their projects 请记住,如果在框架代码中使用类别,则需要确保框架使用者将-all_load (或-all_load如果不使用最新的Xcode) -ObjC到其项目中的“其他链接器标志”

Strictly speaking, it's correct that you cannot add a Xib to a static library. 严格来说,无法将Xib添加到静态库是正确的。 However, there are workarounds. 但是,有一些解决方法。 Matt Galloway's iOS Library With Resources tutorial will show you how to do just that. Matt Galloway的iOS Library With Resources教程将向您展示如何做到这一点。

See iOS Library With Resources . 请参阅iOS资源库

XIB is like resource and what static library is , a compiled file that contains ur classes. XIB就像资源和静态库一样,是一个包含ur类的编译文件。 You can say it is your compiled code ready to be used. 您可以说这是您准备好使用的已编译代码。 You can not add xib's to static library. 您无法将xib添加到静态库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM