Cocoa:NIB 文件创建的实例的 var 名称是什么?

发布于 2024-08-19 21:25:27 字数 222 浏览 3 评论 0原文

当 Cocoa NIB 文件实例化自定义控制器对象的实例时,该自定义控制器实例分配给的变量的名称是什么?

如果不清楚,如果您手动创建该类的实例,您将执行以下操作:

MyControllerClass *myVar = [[MyControllerClass alloc] init];

在幕后执行此操作时,NIB 使用了“myVar”的等效项吗?

When a Cocoa NIB file instantiates an instance of a custom controller object, what is the name of the variable that that custom controller instance is assigned to?

In case that isn't clear, if you manually created an instance of that class you would do:

MyControllerClass *myVar = [[MyControllerClass alloc] init];

What equivalent of "myVar" has the NIB used when doing this behind the scenes?

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

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

发布评论

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

评论(5

别想她 2024-08-26 21:25:27

一旦应用程序被编译,就没有变量名这样的东西了,所以这个问题没有多大意义。在您的示例中,myVar 对于您(程序员)来说只是一个方便的标签,一旦您的源代码编译为二进制代码,它就不再以任何方式存在。

当您将对象放入 nib 文件中时,它会被存档,然后在运行时取消存档。如果您希望能够获取对已存档在 nib 文件中的对象的引用,则需要使用插座,这意味着您在存在的类中声明一个 IBOutlet 实例变量在 nib 文件中,然后将该插座连接到要在 Interface Builder 中引用的 nib 中的对象。实例变量与您在示例中声明的堆栈变量不同,可以在运行时引用。

通常,您会拥有一个“拥有”笔尖的对象。通常,nib 由 NSWindowControllerNSViewController 的实例加载,窗口或视图控制器在 nib 文件中表示为文件所有者。如果您在窗口/视图控制器中声明出口,则可以将出口从文件所有者连接到界面生成器中的对象。

因此,为了澄清,您需要从同一笔尖中的其他对象引用笔尖中的对象。第二个对象在实例变量上使用 IBOutlet 关键字声明一个插座,如下所示:

@interface SomeOtherObject : NSObject
{
    IBOutlet SomeObject* anObject;
}
@end

在 Interface Builder 中,您可以连接 SomeOtherObjectanObject 插座code> 实例到第一个 SomeObject 实例。您可以通过按住 Control 键从一个对象拖动到另一个对象来完成此操作,也可以在 Interface Builder 检查器的连接面板中完成此操作。

然后,您可以通过 SomeOtherObject 代码中的变量名称 anObject 引用您的 SomeObject 实例。

There is no such thing as a variable name once the app is compiled, so this question doesn't make much sense. In your example, myVar is just a convenient label for you, the programmer, and does not exist in any way once your source code is compiled into binary code.

When you place an object into a nib file, it is archived and then unarchived at runtime. If you want to be able to get a reference to an object that has been archived in a nib file, you need to use an outlet, which means you declare an IBOutlet instance variable in a class that is present in the nib file and then connect that outlet to the object in the nib you want to reference in Interface Builder. Instance variables are different to the stack variable that you declared in your example and can be referred to at runtime.

Typically you would have an object that "owns" a nib. Normally nibs are loaded by an instance of NSWindowController or NSViewController and window or view controller is represented in the nib file as File's Owner. If you declare outlets in your window/view controller, you can then connect the outlets from File's Owner to your object in Interface Builder.

So, to clarify, you need a reference to your object in the nib from some other object in the same nib. That second object declares an outlet using the IBOutlet keyword on an instance variable like so:

@interface SomeOtherObject : NSObject
{
    IBOutlet SomeObject* anObject;
}
@end

In Interface Builder, you can then connect the anObject outlet of the SomeOtherObject instance to the first SomeObject instance. You can do this by control-dragging from one object to another or you can do it in the connections panel in the Interface Builder inspector.

You can then refer to your SomeObject instance by the variable name anObject inside the code for SomeOtherObject.

_失温 2024-08-26 21:25:27

实现 awakeFromNib 控制器类中的方法 - 在笔尖加载完成后立即调用它,并且可以在“self”变量中找到控制器的实例。

Implement the awakeFromNib method in your controller class - it's called immediately after the nib has finished loading, and your controller's instance can be found in the "self" variable.

记忆消瘦 2024-08-26 21:25:27

@ tedge(我无法对你的答案发表评论):

你能为可可初学者澄清一下吗?学习 Apple 货币转换器教程。

我在现有 ConverterController 类中实现 awakeFromNib 方法。 (我很快就会学习做的事情!)

应用程序启动并自动实例化 ConverterController 的实例。

awakeFromNib 会告诉我关于该正在运行的实例的什么信息(除了它已准备好使用之外)——以及“self”的什么语法让它泄露该信息?

@ tedge (I can't make comments to your answer):

Can you clarify for a beginning Cocoa learner a bit. Take the Apple Currency Converter tutorial.

I implement the awakeFromNib method in the existing ConverterController class. (Something I will be learning to do shortly!)

The app starts up and an instance of ConverterController is automatically instantiated.

What will awakeFromNib tell me about that running instance (other than that it's ready to use) -- and what syntax with "self" gets it to divulge that information?

痴者 2024-08-26 21:25:27

...该自定义控制器实例分配给的变量的名称是什么?

这是您在声明变量时为该变量指定的任何名称。

IB 不会为您创建变量。听起来您正在寻找插座< /a>,这是您创建的 IB 知道的变量,并允许您将对象插入其中,从而设置该变量。

(您实际上可以从 IB 创建出口,并且在现代运行时,这应该真正创建出口,而不仅仅是在笔尖中声明一个不存在的出口。尽管如此,创建了[在 IB 中] 的插座,给它一个名字。)

… what is the name of the variable that that custom controller instance is assigned to?

It's whatever name you gave that variable when you declared it.

IB doesn't create variables for you. It sounds like you're after an outlet, which is a variable you create that IB knows about and lets you plug objects into, thereby setting the variable.

(You actually can create outlets from IB, and in the modern runtime, this should really create the outlet, not just declare a non-existent outlet in the nib. Even this way, though, you create the outlet [in IB] and you give it a name.)

成熟的代价 2024-08-26 21:25:27

我认为 Nibbles 感到困惑的是如何从代码中引用仅在 NIB 文件中定义的变量。

答案是,通常你在代码和 NIB 中有一个自定义控制器类(或委托类)A,如果你有另一个类或控制器 B 仅在 NIB 中定义,只需在 A 中设置一个指向 B 的出口。可以在代码中的任何地方使用,然后也可以通过 A 访问 B。

我也有这个疑问。

I think what's Nibbles is confused about is that how to reference the variable defined only in NIB file from code.

the answer to that is, normally you have a custom controller class (or delegate class) A in code and NIB, and if you have another class or controller B only defined in NIB, just setup a outlet in A pointing to B. Since A can be used anywhere in your code, B can be accessed as well through A then.

I had this question as well.

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