创建 IBOutlet 还是 UIView?
据我所知,引用 nib 文件中的对象的常用方法是创建一个 IBOutlet 对象,然后在 Interface Builder 中用蓝线连接它。
但我刚刚在 Lynda.com 上看到了一个视频,培训师并没有这样做,而是在头文件中创建了一个 UIView 对象,并将其连接到 Interface 中的 UIView 对象建设者。
所以我的问题是:使用 IBOutlet 对象而不是简单地使用与 IB 中对象相同类的对象有什么好处?有区别吗?
The usual method, as far as I know, of referring to an object in a nib file is creating an IBOutlet
object and then connecting it in Interface Builder with the blue line.
But I just saw a video in Lynda.com where instead of doing that, the trainer created an UIView
object in the header file, and connected it to his UIView
object in Interface Builder.
So my question is: what's the benefit of using an IBOutlet
object instead of simply using an object which is the same class of the object in IB? Is there a difference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
IBOutlet
只是 Interface Builder 的一个提示。 IBOutlets 可以是任何东西。UIView
、UIButton
、UITextView
等。在上面的例子中,Foo是什么? IBOutlet 还是 UIView? 两者。但你的问题不允许这样做。
您可能想在此处查看 Apple 文档: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCTutorial/06Controller/06Controller.html
他们进一步描述IBOutlet的作用:
和:
总之:
IBOutlets 几乎可以引用任何内容。它们只是 Interface Builder 的一个提示,让您在加载 XIB 时设置它们的值。事实上,文本
IBOutlet
在 Interface Builder 世界之外的意义很小,以至于它被从最终产品中删除,因为除了帮助 Interface Builder 之外,它完全没有任何功能。我会回去检查一下您作为
IBOutlets
连接的其他一些东西,您肯定会发现它们实际上确实有一些真实的类型。IBOutlet
is just a hint to Interface Builder. IBOutlets can be anything.UIView
,UIButton
,UITextView
, etc.In the example above, what is Foo? An IBOutlet or a UIView? It's both. But your question doesn't allow for that.
You might want to check out the Apple documentation here: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCTutorial/06Controller/06Controller.html
Where they further describe the role of IBOutlet:
And:
In summary:
IBOutlets can reference pretty much anything. They are just a hint to Interface Builder which lets you set their value when you load the XIB. In fact, the text
IBOutlet
means so little outside of the Interface Builder world, that it gets stripped from the final product because it's completely non-functional for any purpose other than helping out Interface Builder.I'd go back and check out some of the other things you were hooking up as
IBOutlets
, and you'll surely discover they actually do have some real type.IBOutlet 只是一个向 IB 指示的关键字,“嘿,这应该是一个插座!”它并不是真正的对象类型——它实际上是被预处理器删除的。但是,如果您指定插座的类型为 UIVIew,IB 将确保它只能连接到 UIView 类型的事物。
IBOutlet is just a keyword to indicate to IB, "Hey, this should be an outlet!" It's not really an object type — it's literally erased by the preprocessor. If you specify that the outlet is of type UIVIew, though, IB will ensure that it can only be hooked up to UIView-type things.