IBOutlet何时初始化? [英] When does an IBOutlet initialize?

查看:120
本文介绍了IBOutlet何时初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过Interface Builder为文本视图设置了出口.文本视图可以很好地加载,但是我不能以编程方式访问它的任何属性,因为出口始终为nil.

I set up an outlet for a text view via Interface Builder. The text view loads fine, however I can't access any of the properties of it programmatically because the outlet is always nil.

何时实例化?即使在我的applicationDidFinishLoading被调用之后,它仍然不是活动"或未存档的.

When does it instantiate? Even after my applicationDidFinishLoading gets called, it's still not "alive" or unarchived.

推荐答案

插座不会实例化,因为插座是变量(或属性).

An outlet doesn't instantiate because an outlet is a variable (or property).

在装入笔尖时实例化了笔尖中的对象,并且在创建对象之后但将awakeFromNib发送到所有相关对象之前,将它们尽快分配给每个出口.

The objects in a nib are instantiated when that nib is loaded, and they are assigned to each outlet as immediately as possible afterward, after the objects are created but before awakeFromNib is sent to all relevant objects.

换句话说,笔尖加载按以下顺序执行以下所有操作:

In other words, nib loading does all of the following, in this order:

  1. 创建或重新创建存储在笔尖中的所有对象. (此排除文件的所有者,第一响应者以及其他外部和虚构的对象.)
  2. 设置与笔尖相连的同一个笔尖中,笔尖中连接到对象的每个插座属性. (例如:视图控制器的view到顶级视图的出口.)
  3. 发送awakeFromNib到笔尖中的对象,并且(在Cocoa中)还发送到文件的所有者(例如,窗口控制器).
  1. Creates or re-creates all of the objects that are stored in the nib. (This excludes File's Owner, First Responder, and other external and imaginary objects.)
  2. Sets every outlet property that is connected in the nib to the object in the same nib that the nib has it connected to. (E.g.: A view controller's view outlet to a top-level view.)
  3. Sends awakeFromNib to objects in the nib, and (in Cocoa) also to the File's Owner (e.g., window controller).

您的问题已由#2回答.

Your question is answered by #2.

资源编程指南提供了更多信息.在Cocoa和Cocoa Touch之间,细节有细微的差别,特别是在哪些对象发送awakeFromNib消息而哪些对象没有发送消息时.

The Resource Programming Guide has more information. The details are subtly different between Cocoa and Cocoa Touch, particularly as regard which objects are sent awakeFromNib messages and which ones aren't.

何时实例化?即使在我的applicationDidFinishLoading被调用之后,它仍然不是活动"或未存档的.

When does it instantiate? Even after my applicationDidFinishLoading gets called, it's still not "alive" or unarchived.

不是文本视图?

可能是,您只是没有连接电源插座.在笔尖中检查一下.

It probably is and you just didn't connect the outlet. Check this in the nib.

另一种可能性:您在另一个笔尖中创建了文本视图,而不是在创建应用程序委托的那个视图中创建的文本视图(如果您甚至根本不在一个笔尖中创建了应用程序委托),并且您没有将视图连接到插座正确的对象.也许您在文本视图的笔尖中创建了第二个应用程序委托;这个应用程序委托人实际上不是应用程序的委托人,这就是为什么真正的应用程序委托人看不到文本视图的原因-您将文本视图交给了冒名顶替者,而不是真正的McCoy.

Another possibility: You created the text view in another nib, not the one where the app delegate is created (if you even created the app delegate in a nib at all), and you didn't connect the view to the outlet of the right object. Perhaps you created a second app delegate in the text view's nib; this app delegate is not actually the app's delegate, which is why the real app delegate does not see the text view—you gave the text view to the impostor, not the real McCoy.

这篇关于IBOutlet何时初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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