objective-c - 创建 IBOutlet 还是 UIView?

标签 objective-c ios cocoa-touch interface-builder iboutlet

据我所知,在 nib 文件中引用对象的常用方法是创建 IBOutlet对象,然后在 Interface Builder 中用蓝线连接它。

但是我刚刚在 Lynda.com 上看到了一个视频,培训师没有这样做,而是创建了一个 UIView头文件中的对象,并将其连接到他的UIView Interface Builder 中的对象。

所以我的问题是:使用 IBOutlet 有什么好处?对象而不是简单地使用与 IB 中的对象相同类的对象?有区别吗?

最佳答案

IBOutlet只是对 Interface Builder 的提示。 IBOutlets 可以是任何东西。 UIView , UIButton , UITextView , ETC。

@property (nonatomic, retain) IBOutlet UIView * Foo;

在上面的例子中,什么是 Foo? IBOutlet 还是 UIView?两者都是。但是您的问题不允许这样做。

您可能想在此处查看 Apple 文档:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCTutorial/06Controller/06Controller.html

他们进一步描述了 IBOutlet 的作用:

An object can communicate with other objects in an application by sending messages to outlets. An outlet can reference any object in an application: user interface objects such as text fields and buttons, model objects (usually instances of custom classes), and even the application object itself. Outlets are special instance variables because they can be set in Interface Builder.



和:

Note: IBOutlet is a null-defined macro that the C preprocessor removes at compile time. Interface Builder uses it to identify outlet declarations so that it can display them when connecting outlets visually.



总之:

IBOutlets 几乎可以引用任何东西。它们只是对 Interface Builder 的提示,它允许您在加载 XIB 时设置它们的值。其实文IBOutlet在 Interface Builder 世界之外意味着如此之少,以至于它被从最终产品中剥离出来,因为除了帮助 Interface Builder 之外,它对于任何目的都完全没有功能。

我会回去看看你连接的其他一些东西IBOutlets ,你肯定会发现它们确实有一些真实的类型。

关于objective-c - 创建 IBOutlet 还是 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6574383/

相关文章:

ios - 弹回 View Controller 时使用 ARC 的 EXC_BAD_ACCESS

ios - GKMatchMaker 邀请处理程序已弃用

ios - 如何将 ios "more"按钮的文本更改为我的语言版本?

iphone - 通知中心缓存设置?

ios - UISegmentedControl:如何在使用自定义图像时不突出显示已选择的片段

ios - 无法为多个单词提供单独的颜色

ios - SpriteKit 丢失纹理

ios - 在 iOS 上以编程方式向按钮添加操作

删除后iOS导航栏 subview 重新出现

objective-c - 在 sprite.runAction 之后调用 Block 时崩溃