ios - 如何在不使用单独的包的情况下将我的资源文件包含在我的框架中?

标签 ios objective-c xcode

我一直在按照本指南创建 iOS 静态库:https://github.com/jverkoey/iOS-Framework#walkthrough .我设法创建了一个可以导入到另一个 Xcode 项目中的框架。

现在,我希望我的框架能够显示 Storyboard。我想 .storyboard 文件算作一种资源。之前的指南指出我应该为我的资源文件(例如图像)创建一个 Bundle,而不是将它们放在框架本身中。

但是,我确实想将它们放在框架本身中。我不想使用单独的 bundle 。

我找到的所有指南都告诉我同样的事情。我了解使用单独 bundle 的优势,但我现在不想这样做。

现在,我不知道如何将我的 .storyboard 文件包含在我的框架中,以便我可以用这个来展示它:

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"NameOfStoryboard" bundle:[NSBundle bundleForClass:[self class]]];
UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"NameOfViewController"];
[otherView presentViewController:vc animated:YES completion:NULL];

上面的代码似乎不起作用(找不到 Storyboard文件)。我想这是因为 .storyboard 当前未包含在框架中。遗憾的是,我不知道如何包含它。

在框架的 Xcode 项目中,在构建阶段下,我看到一个标签为“复制文件”的选项卡,它看起来像我需要的。

enter image description here

现在,我不确定这是在做什么,但无论如何,它似乎并没有起作用。

我也是这样做的:

enter image description here

如何在不使用单独的包的情况下将我的 .storyboard 文件包含在我的框架中?

最佳答案

为什么你不想为一个框架使用单独的包?因此,如果一个应用程序正在使用您的框架,它将能够使用它的资源(Xibs、 Storyboard等)

例如,来自使用您的框架的应用程序的调用可能如下所示:

FrameworkViewController *frameworkView = [[FrameworkViewController alloc] initWithNibName:@"FrameworkViewController"
                                                                          bundle:[NSBundle yourFrameworkBundle]];

并且在您的框架中,您可以编写一个辅助类 NSBundle+YourFrameworkBundle 来访问框架包:

NSBundle+YourFrameworkBundle.h

@interface NSBundle (YourFrameworkBundle)

+ (NSBundle *)yourFrameworkBundle;

@end

NSBundle+YourFrameworkBundle.m

#import "NSBundle+YourFrameworkBundle.h"

@implementation NSBundle (YourFrameworkBundle)

+ (NSBundle *)yourFrameworkBundle
{
static NSBundle *frameworkBundle = nil;
static dispatch_once_t predicate;

dispatch_once(&predicate, ^{
    NSString *mainBundlePath = [[NSBundle mainBundle] resourcePath];
    frameworkBundle = [NSBundle bundleWithPath:[mainBundlePath stringByAppendingPathComponent:@"YourFrameworkBundle.bundle"]];
});

return frameworkBundle;
}

@end

要为框架捆绑资源,请创建一个单独的目标,您可以在其中链接所有资源。 enter image description here

关于ios - 如何在不使用单独的包的情况下将我的资源文件包含在我的框架中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35067172/

相关文章:

ios - Xcode 6 为 iPhone 6 和 6 plus 自动调整应用程序大小

ios - UIView animateKeyframes 不适用于 NSLayoutConstraints

iphone - UITextField SecureTextEntry 字段将键盘从数字键盘更改为通用键盘

objective-c - 保留、重用、释放?

Xcode 想要访问钥匙串(keychain)中的 "Xcode-AlternateDSID"

ios - awakeFromNib 与 drawRect 中的 CALayer

ios - 我怎样才能驳回第一个 vc,第二个 vc 在第一个 vc 上以模态方式呈现

具有默认参数的Objective-C函数?

objective-c - 单击按钮时无法加载窗口 nib 文件(testwindow.xib)?

objective-c - 什么是 NSFrozenDictionaryM?