![](/img/trans.png)
[英]How to load a app with new nib file when app is running on main window.xib
[英]How to load a `.xib` / `.nib` file within a framework?
我正在以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
定义的整个 View,其中包括图像和标签。
当我构建演示应用程序(和库)时,该应用程序运行,但是当需要显示此视图时,出现以下错误:
*** 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
文件,可以在Frameworks/bfsdk.framework
目录中找到PlayerView.nib
文件,但我猜是因为它不在根目录中,所以没有找到它:
我已验证:
PlayerView
PlayerView.xib
已添加到“副本捆绑资源”构建阶段 我怎样才能加载.xib
文件,我的图书馆内,这是从一个类中运行的应用程序,仅包括了我的图书馆的时候,我库内这.framework
?
我相信,如果我手动指定文件的路径,例如:
NSString* resourcePath = [[NSBundle mainBundle] pathForResource:@"bfsdk" ofType:@"framework"];
[[NSBundle mainBundle] loadNibNamed:[resourcePath stringByAppendingPathComponent:@"PlayerView"]];
但是我不喜欢这样使我将我的框架名称硬编码到我的一个类中。 如果我在一年内更改名称,我不希望一切都因为这个没人记得的代码而被破坏。
应该足够使用
[NSBundle bundleForClass:[self class]]
而不是[NSBundle mainBundle]
因此,基本上[NSBundle mainBundle]返回当前模块(您的应用程序)的包,而不是实际框架的包
在这里您可以找到更多详细信息[NSBundle bundleForClass:[self class]]]这是什么意思?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.