【发布时间】:2019-07-12 19:39:28
【问题描述】:
我正在开发一个作为 Cocoa Pod 的 iOS 库。在这个库中,我有一个自定义视图:
@interface PlayerView : UIView
@property (strong, nonatomic) IBOutlet UIView *contentView;
@end
@implementation PlayerView
- (instancetype) initWithCoder:(NSCoder*)coder
{
self = [super initWithCoder:coder];
[self initialize];
return self;
}
- (instancetype) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
[self initialize];
return self;
}
- (void)initialize
{
[[NSBundle mainBundle] loadNibNamed:@"PlayerView" owner:self options:nil];
[self addSubview:contentView];
}
@end
在这种情况下,contentView 是.xib 中定义的整个视图,其中包括图像和标签。
当我构建我的演示应用程序(和库)时,应用程序会运行,但在显示此视图时出现以下错误:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/stevenbarnett/Library/Developer/CoreSimulator/Devices/EC244A5F-CE57-4CCD-9BB4-CDC834D74812/data/Containers/Bundle/Application/446EB609-B663-4BD5-8F8D-F22D03BC0B18/bfsdk_Example.app> (loaded)' with name 'PlayerView''
手动检查.app 文件,可以发现PlayerView.nib 文件隐藏在Frameworks/bfsdk.framework 目录中,但我猜是因为它不在根目录下,所以没有找到:
我已验证:
- 文件所有者设置为
PlayerView -
PlayerView.xib被添加到“复制捆绑资源”构建阶段
在运行仅将我的库作为@987654336 包含我的库的应用程序时,如何从我的库内 的类中加载我的库中内 的.xib 文件@?
如果我手动指定文件的路径,我相信我可以让它工作:
NSString* resourcePath = [[NSBundle mainBundle] pathForResource:@"bfsdk" ofType:@"framework"];
[[NSBundle mainBundle] loadNibNamed:[resourcePath stringByAppendingPathComponent:@"PlayerView"]];
但是我不喜欢这样,我将框架的名称硬编码到我的一个类中。如果我在一年内更改名称,我不希望一切都因为这一行无人记得的隐藏代码而中断。
【问题讨论】:
标签: ios objective-c xib nsbundle