Cocoa Interface Builder 对象初始化

发布于 2024-10-12 03:31:22 字数 281 浏览 6 评论 0原文

根据我浏览过的文档和示例代码,我得到的印象是,当在 Interface Builder 中读入并配置 xcode 中定义的类时,会有效地创建基于该类的对象并将其存储在 xib 或 nib 文件中。因此,当相应的应用程序启动时,该对象就可以使用了。

或者,对于 Interface Builder 尚未处理的类,必须在 xcode 中显式编写诸如“new”语句之类的代码,以便创建和使用关联的对象。

如果有比我更有知识的人来确认或纠正我对 Interface Builder 非常幼稚的理解,那将是非常好的......

Base on the documentation and sample code that I have gone through, I got an impression that when a class defined in xcode is read into and configured in Interface Builder, an object based on the class is effectively created and stored in an xib or nib file. So the object is ready to be used when the corresponding application is launched.

Alternatively, for classes that have not been handled by Interface Builder, code such as the "new" statements have to be written in xcode explicitly in order for the associated objects to be created and used.

It will be very nice to have people who are more knowledgable than me to confirm or to correct my very naive understanding of Interface Builder ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉风有信 2024-10-19 03:31:22

你的理解是正确的,但不完整。是的,Interface Builder 实例化类并将它们序列化到 NIB 中。但是,这些对象不会自动可供您的代码使用。

对于要通过 Xcode 访问的每个 IB 对象,您需要声明一个 IBOutlet 变量。示例:

IBOutlet NSWindow* mainWindow;  // A Reference to your main window

将此代码放入通过 Interface Builder 实例化的自定义对象的头文件中(将通用对象拖到类列表中,然后在检查器的“标识”选项卡中,将自定义对象设置为类的实例) 。然后,在 Interface Builder 中右键单击您的自定义对象。您应该在弹出的窗口中看到 IBOutlet 的条目。从它旁边的小圆圈拖动到(在本例中)您的主窗口。您现在在 Xcode 中拥有了对 IB 对象的引用。

通过建立这些连接(使用 IBOutlet 作为引用,使用 IBActions 作为方法),您可以定义应用程序的大部分行为。

Your understanding is correct, but incomplete. Yes, Interface Builder instantiates classes and serializes them into the NIB. However, those objects are not automatically available to your code.

For each IB object you want to access through Xcode, you need to declare an IBOutlet variable. Example:

IBOutlet NSWindow* mainWindow;  // A Reference to your main window

Put this code in the header file of a custom object that you instantiate through Interface Builder (drag a generic Object to your class list, then in the Identity tab of the inspector, set the custom object to be an instance of your class). Then, right-click on your custom object in Interface Builder. You should see an entry for your IBOutlet in the window that pops up. Drag from the little circle next to it to (in this example) your main window. You now have a reference to the IB object in Xcode.

It is through making these connections (with IBOutlets for references and IBActions for methods) that you define much of the behavior of your application.

撕心裂肺的伤痛 2024-10-19 03:31:22

Nib 文件包含对象图的存档副本; NSNib 或 NSBundle 需要知道的所有内容,以便创建存储在 nib 文件中的所有对象的新实例,按照 nib 的绑定描述连接在一起,并将它们的属性设置为 nib 中指定的值。

完整的过程记录在对象加载过程中

对象不是从 nib 自动创建的,而是根据需要创建的,以响应加载该 nib 文件的某些对象。区别很重要,因为您可以让单个对象多次加载 nib,或者让多个对象加载同一个 nib 文件。

例如,我可能会创建一个“文件所有者”为 UIViewController 的笔尖,它将单个 UIView 对象绑定到文件所有者的“view”属性。然后,我可以让两个不同的 UIViewController 子类(或同一 UIViewController 子类的两个实例)加载相同的 nib 文件,以分别获取自己的 UIView 副本。同样,每当我需要一个新的表格单元格时,我可能会从笔尖加载一个新的 UITableViewCell 。

Nib files contain an archived copy of an object graph; everything NSNib or NSBundle needs to know in order to create new instances of all the objects stored in the nib file, connected together as described by the nib's bindings, and with their properties set to the values specified in the nib.

The full process is documented in The Object Loading Process

Objects are not automatically created from a nib but rather created on demand in response to some object loading that nib file. The difference is important because you can have a single object load a nib more than once or have multiple objects load the same nib file.

For example I might create a nib whose "File's Owner" is UIViewController which binds a single UIView object to the File's Owner's 'view' property. I could then have two different UIViewController subclasses (or two instances of the same UIViewController subclass) load that same nib file to each get their own copy of the UIView. Similarly I might load a new UITableViewCell from a nib every time I need a new cell for a table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文