无法使用在内部使用.xib文件的iOS框架 [英] Unable to use iOS framework which is internally using .xib file

查看:401
本文介绍了无法使用在内部使用.xib文件的iOS框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过以下网址中指定的步骤为iOS制作通用框架: Universal -framework-iOS



我有一个viewController类,该框架内部加载一个.xib文件。



下面是代码的一部分,显示了如何初始化viewController并显示相关视图:

  *** SomeViewController类的一部分实现,这是框架之外的*** / 

- (void)viewDidLoad
{super bd
[super viewDidLoad];
//在加载视图之后执行任何其他设置,通常来自nib。

self.viewControllerWithinFramework = [[ViewControllerWithinFramework alloc] initWithTitle:@我的自定义视图];
}
- (IBAction)showSomeView:(id)sender {
[self.viewControllerWithinFramework showRelatedView];
}

/ ***部分实现ViewControllerWithinFramework类,这是框架内部*** /

- (id)initWithTitle:(NSString * )aTitle
{
self = [super initWithNibName:@ViewControllerWithinFrameworkbundle:nil]; // ViewControllerWithinFramework.xib在框架内

if(self)
{
_viewControllerTitle = aTitle;
}

return self;
}

创建框架时,我包括所有.xib文件,包括ViewControllerWithinFramework.xib在其复制捆绑资源构建阶段。



现在我的问题是当我尝试集成该框架在其他项目,它崩溃与以下堆栈跟踪:

 示例[3616:a0b] ***由于未捕获异常终止应用程序'NSInternalInconsistencyException' ,原因:'Could not load NIB in bundle:'NSBundle< / Users / miraaj / Library / Application Support / iPhone Simulator / 7.0 / Applications / 78CB9BC5-0FCE-40FC-8BCB-721EBA031296 / Sample.app> (
0)

0 CoreFoundation 0x017365e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x014b98b6 objc_exception_throw + 44
2 CoreFoundation 0x017363bb + [NSException raise:format:] + 139
3 UIKit 0x004cc65c - [UINib instantiateWithOwner:options:] + 951
4 UIKit 0x0033ec95 - [UIViewController _loadViewFromNibNamed:bundle:] + 280
5 UIKit 0x0033f43d - [UIViewController loadView] + 302
6 UIKit 0x0033f73e - [UIViewController loadViewIfRequired] + 78
7 UIKit 0x0033fc44 - [UIViewController view] + 35

任何想法,我如何解决这个问题?



>

解决方案

如果你使用通用框架 - iOS所有资源(包括Nib和图像)将被复制到一个单独的包(文件夹),如 MyApp.app/Myframework.bundle/MyNib.nib



您需要通过传递 NSBundle 对象而不是 nil 。你可以如下获得你的包对象:

  NSString * path = [[NSBundle mainBundle] pathForResource:@MyframeworkofType: @束]; 
NSBundle * resourcesBundle = [NSBundle bundleWithPath:path];

至于图片,您可以在之前加上 Myframework.bundle / 到他们的名字:

  [UIImage imageNamed:@Myframework.bundle / MyImage



这也适用于Interface Builder。



更新框架是一个痛苦,特别是资源,所以更好地尝试使用 CocoaPods


I am trying to make a universal framework, for iOS by following steps specified in this URL: Universal-framework-iOS

I have a viewController class within, that framework which internally loads a .xib file.

Below is a part of code which shows, how I am initializing that viewController and showing related view:

/*** Part of implementation of SomeViewController class, which is outside the framework ***/

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.viewControllerWithinFramework = [[ViewControllerWithinFramework alloc] initWithTitle:@"My custom view"];
}
- (IBAction)showSomeView:(id)sender {
    [self.viewControllerWithinFramework showRelatedView];
} 

/*** Part of implementation of ViewControllerWithinFramework class, which is inside the framework ***/

- (id)initWithTitle:(NSString *)aTitle
{
   self = [super initWithNibName:@"ViewControllerWithinFramework" bundle:nil]; // ViewControllerWithinFramework.xib is within the framework

   if (self)
   {
       _viewControllerTitle = aTitle;
   }

   return self;
}

While creating the framework, I included all .xib files, including ViewControllerWithinFramework.xib within its Copy Bundle Resources build phase.

Now my problem is when I try to integrate that framework within other project, it crashes with below stack trace:

Sample[3616:a0b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/miraaj/Library/Application Support/iPhone Simulator/7.0/Applications/78CB9BC5-0FCE-40FC-8BCB-721EBA031296/Sample.app> (loaded)' with name 'ViewControllerWithinFramework''
*** First throw call stack:
(
    0   CoreFoundation                      0x017365e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x014b98b6 objc_exception_throw + 44
    2   CoreFoundation                      0x017363bb +[NSException raise:format:] + 139
    3   UIKit                               0x004cc65c -[UINib instantiateWithOwner:options:] + 951
    4   UIKit                               0x0033ec95 -[UIViewController _loadViewFromNibNamed:bundle:] + 280
    5   UIKit                               0x0033f43d -[UIViewController loadView] + 302
    6   UIKit                               0x0033f73e -[UIViewController loadViewIfRequired] + 78
    7   UIKit                               0x0033fc44 -[UIViewController view] + 35

Any ideas, how could I resolve this problem?

Note: It works fine if there is no any xib within the framework.

解决方案

If you're using Universal-framework-iOS all resources (including Nibs and images), will be copied inside a separate bundle (folder) such as MyApp.app/Myframework.bundle/MyNib.nib.

You need to specify this bundle by passing a NSBundle object instead of nil. Your can get your bundle object as follows:

NSString *path = [[NSBundle mainBundle] pathForResource:@"Myframework" ofType:@"bundle"];
NSBundle *resourcesBundle = [NSBundle bundleWithPath:path];

As for images you can just prepend Myframework.bundle/ to their names:

[UIImage imageNamed:@"Myframework.bundle/MyImage"

This also works in Interface Builder.

Finally your users to install/update a framework is a pain, specially with resources, so better try to use CocoaPods.

这篇关于无法使用在内部使用.xib文件的iOS框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆