gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:35:22 26 4
gpt4 key购买 nike

我一直在按照本指南创建 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com