Objective C 术语:outlet &代表们
我无法理解 iPhone 如何处理事件的插座概念。帮助!代表们也让我困惑。请问有人愿意解释一下吗?
I'm having issues understanding the concept of outlets how the iPhone deals with events. Help! Delegates confuse me too. Would someone care to explain, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Outlet(在 Interface Builder 中)是类中的成员变量,设计器中的对象在运行时加载时会被分配。
IBOutlet
宏(这是一个空的#define
)向 Interface Builder 发出信号,将其识别为要在设计器中显示的插座。例如,如果我拖出一个按钮,然后将其连接到
aButton
插座(在我的界面 .h 文件中定义),则在运行时加载 NIB 文件将分配aButton
code> 指向由 NIB 实例化的UIButton
的指针。然后在实现中:
这消除了在运行时编写代码来实例化和分配 GUI 对象的需要。
至于委托,它们是另一个对象(通常是通用API类,例如表视图)使用的事件接收对象。它们本身并没有什么特别之处。它更多的是一种设计模式。委托类可以定义一些预期的消息,例如:
...并且当 API 对象想要向委托通知事件时,它会在委托上调用此消息。例如:
委托定义了消息:
这种用法可能是:
Outlets (in Interface Builder) are member variables in a class where objects in the designer are assigned when they are loaded at runtime. The
IBOutlet
macro (which is an empty#define
) signals Interface Builder to recognise it as an outlet to show in the designer.For example, if I drag out a button, then connect it to the
aButton
outlet (defined in my interface .h file), the loading of the NIB file at runtime will assignaButton
the pointer to thatUIButton
instantiated by the NIB.Then in the implementation:
This eliminates the need to write code to instantiate and assign the GUI objects at runtime.
As for Delegates, they are event receiving objects used by another object (usually a generalised API class such as a table view). There's nothing inherently special about them. It's more of a design pattern. The delegate class may define several of the expected messages such as:
...and the API object calls this message on the delegate when it wants to notify it of the event. For example:
And the delegate defines the message:
Such use might be:
委托是另一个对象可以向其转发消息的对象。换句话说,就像你妈妈叫你打扫房间,你却把它抵押给了你的弟弟。你的弟弟知道如何做这项工作(因为你懒得学),所以他会为你做这件事。
A delegate is a object that another object can forward messages to. In other words, it's like when your mom told you to clean your room and you pawned it off on your little brother. Your little bro knows how to do the job (since you were too lazy to ever learn) and so he does it for you.