如何使用顶级ViewController加载XIB? [英] How to load a XIB with a top-level ViewController?

查看:145
本文介绍了如何使用顶级ViewController加载XIB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有视图控制器根元素的笔尖,如下所示:

I've got a nib with a view controller root element like this:

所以我可以使用自动布局相对于顶部和底部布局指南定位元素。

so I can position elements relative to the top and bottom layout guides using auto-layout.

当我第一次尝试使用

SearchViewControllerPro* searchViewController = [[SearchViewControllerPro alloc]initWithNibName:@"SearchViewControllerPro" bundle:[NSBundle mainBundle]];

我得到以下运行时异常:

I got the following run-time exception:

终止应用程序由于未捕获的异常
'NSInternalInconsistencyException',原因: - [UIViewController中
_loadViewFromNibNamed:束:]加载的 SearchViewControllerPro 笔尖但视图出口是没有设置。'

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "SearchViewControllerPro" nib but the view outlet was not set.'

搜索我指出的错误,需要将xib的文件所有者设置为我的视图控制器的类和视图插座必须设置为xib中的视图对象。如果我这样做,那么我得到以下运行时错误:

Googling the error it was pointed out to me, that the file owner of the xib needed to be set to the class of my view controller and the view outlet had to be set to the view object in the xib. If I do so, then I get the following run-time error:


由于未捕获的异常而终止应用程序
'UIViewControllerHierarchyInconsistency' ,原因:'视图一次最多只能与一个视图控制器关联
!视图>与...相关联。在将此视图与
相关联之前清除此关联。'

Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'A view can only be associated with at most one view controller at a time! View > is associated with . Clear this association before associating this view with .'

由于视图与两者相关联,因此不会出人意料文件所有者和笔尖的顶级视图控制器。但是,我怎么能告诉运行时它们实际上是同一个东西而不是两个独立的实体呢?

Does not come as a surprise since the view is associated to both the file owner and the top-level view controller of the nib. But how can I tell the run-time that they are both in fact the very same thing instead of two separate entities?

编辑:
当我尝试从像这样的笔尖unpck所述的ViewController,

When I try to unpck the ViewController from the nib like so,

NSArray* xibContents = [[NSBundle mainBundle] loadNibNamed:@"SearchViewControllerPro" owner:nil options:nil];
SearchViewControllerPro* mapSearchViewController = [xibContents lastObject];

,它也不好:


由于未捕获的异常'NSUnknownKeyException'终止应用程序,
原因:'[setValue:forUndefinedKey:]:此类
不是键视图的键值编码兼容。

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key view.

临时解决方案:

我找到了一个解决方案,但它并不漂亮。尽管IB中显示了结构,但视图控制器不是xib中的最后一个对象。所以我有:

I found a solution, but it is not pretty. Despite the structure as shown in IB, the view controller is not the last object in the xib. So I have:

__block SearchViewControllerPro* mapSearchViewController = nil;
[xibContents enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if ([obj isKindOfClass:[SearchViewControllerPro class]]) {
        mapSearchViewController = obj;
    }
}];

这似乎没有运行时崩溃。但是,它只是干净的代码。

and this seems to work without run-time crashes. However, it's everything but clean code.

推荐答案


我怎么能告诉他们的运行时间实际上是
的东西,而不是两个独立的实体?

how can I tell the run-time that they are both in fact the very same thing instead of two separate entities?

你不能因为它们不是一样。您已经创建了两个 SearchViewControllerPro intstances。

You can't because they are not the same thing. You have created two SearchViewControllerPro intstances.

你需要在一个nib上为alloc-init一个SearchViewControllerPro实例或unarchive一个。

You need to either alloc-init a SearchViewControllerPro instance or unarchive one from a nib.

如果你决定了在nib中创建ViewController的常用方法是访问它,就像访问你在笔尖中创建的任何其他项目(视图,按钮,文本字段)一样。

If you decide to create the ViewController in the nib the usual way to access it is the same way you would access any other item (view, button, textfield) that you create in a nib.

向FilesOwner对象添加一个出口,在接口构建器中挂接连接,并确保在取消归档nib时传递该对象。

Add an outlet to the FilesOwner object, hook up the connection in interface builder and be sure to pass the object when you unarchive the nib.

例如,如果您希望将nib取消归档的Object为FilesOwner:

eg If you want the Object that unarchives the nib to be FilesOwner:

[[NSBundle mainBundle] loadNibNamed:@"SearchViewControllerPro" owner:self options:nil];

这篇关于如何使用顶级ViewController加载XIB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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