如何从NIB加载UIView? [英] How to load a UIView from a NIB?

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

问题描述

我一直在使用 UIViewController initWithNibName 取得了很大的成功,基本上使用它们作为一种方便的设计方式Interface Builder的视图。不幸的是,我在 UIViewController 文档中注意到这一行之前构建了一个视图层次结构:

I have been using UIViewControllers and initWithNibName with much success, basically using them as a convenient way to design the view with Interface Builder. Unfortunately I have built a hierarchy of views before noticing this line in the UIViewController documentation:


注意:您不应该使用view
控制器来管理仅在其窗口的一部分填充
的视图

Note: You should not use view controllers to manage views that fill only a part of their window

我的问题是:除了默认的First Responder和Owning Object之外,还有一个非常简单的NIB只有 UIView ,将 UIView 加载到我的代码中的最简单方法是?

My question is this: Having a very simple NIB that only has a UIView in addition to the default First Responder and Owning Object, what is the simplest way to load the UIView into my code?

我无法获得 loadNibNamed:owner:options:此时工作,但怀疑答案会以某种方式涉及它。

I have not been able to get loadNibNamed:owner:options: to work at this point, but suspect the answer will involve it somehow.

推荐答案

是的,只需致电

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

您通常从您在NIB中设置为文件所有者的视图控制器执行此操作。这样,您可以在视图控制器中声明视图的插座,该插座将在您加载NIB文件时自动连接。在这种情况下,您甚至不必使用方法的返回值。

You normally do this from the view controller you have set as File's Owner in the NIB. That way, you can declare an outlet for the view in the view controller which will automatically get connected when you load the NIB file. You don't even have to work with the return value of the method in this case.

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

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