实施(.m)文件中的IBOutlet实例变量 [英] IBOutlet instance variables in implementation (.m) files

查看:104
本文介绍了实施(.m)文件中的IBOutlet实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个视图控制器或窗口控制器,它(通常)是相应的XIB文件中的文件所有者。

Let’s say I have a view controller, or a window controller, which is (as usual) the "File’s Owner" in a corresponding XIB file.

(众所周知),在控制器类中具有 IBOutlet 很常见,然后可以使用Interface Builder在XIB中进行连接。

It is (as you all know) very common to have IBOutlets in the controller class that you can then connect in the XIB using Interface Builder.

直到现在,我一直在接口(.h)文件中创建 IBOutlet 实例变量。但是(众所周知) IBOutlet s通常是控制器类的私有机制。

Until now, I have been creating IBOutlet instance variables in my interface (.h) files. But (as you all know) IBOutlets are very often a private mechanism of the controller class; outsiders shouldn’t even know about them.

这就是为什么我现在(因为Objective-C最近开始提供这样做的功能)想要将我所有的 IBOutlet 放入我的实现(.m)文件中。

This is why I now—since Objective-C recently started offering the capability to do so—want to put all of my IBOutlets into my implementation (.m) files.

我尝试执行此操作,但这似乎正常。 我的问题是这些:为什么这样做有效?给我的印象是Interface Builder只能查看类的头文件,而不能查看它们的实现文件。我错了吗? Interface Builder如何才能查看实现文件?将 IBOutlet 实例变量放入实现文件是否有潜在危险?

I tried doing this, and this seems to work fine. My questions are these: Why does this work? I was under the impression that Interface Builder could only view the class’ header files—not peek into their implementation files. Am I wrong? How come that Interface Builder can "see into" implementation files? Could it be potentially dangerous to put IBOutlet instance variables into the implementation files?

推荐答案

来自 Xcode 4用户指南


注意:由于Xcode 4会解析头文件和实现
文件以建立索引,因此您可以定义操作和出口放在
实施(.m)文件中,而无需将它们放在标头
文件中,您可以直接从笔尖文件到
实施文件建立连接。因此,您不需要向可能正在使用您的类的客户端公开
界面或操作的一部分。

Note: Because Xcode 4 parses both your header files and implementation files for indexing, you can define actions and outlets in implementation (.m) files without needing to place them in the header file and you can make connections directly from the nib file to the implementation file. Therefore, you do not need to expose parts of your interface or actions to clients who might be using your classes.

对于将IBOutlet实例变量放入实现文件中是否有潜在危险?

是的,但没有声明的那么多由于缺少运行时访问范围,因此可以访问该方法。即使无法访问该方法,解析也会在运行时完成,并且没有访问范围信息附加到该方法。
可以实现,但是Objective-C缺少私有受保护的等。 C ++或Java。
注意,实现这种行为在运行时甚至会慢得多。

Yes it is, but not that much as any declared method can be accessed because of the lack of access-scope at runtime. Even if the method isn't accessible, the resolution is done at runtime and no access-scope information is attached to the method. That could have been implemented, but Objective-C lacks of private, protected and so on as in C++ or Java. Note that implementing such a behavior would be even much slower at runtime.

这篇关于实施(.m)文件中的IBOutlet实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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