NSButtonCell 与 NSButton
我一直在阅读有关 NSButtons 和 cells 的 Apple 文档,但我似乎真的无法理解两者之间的区别。除了这种复杂性之外,它们看起来都有大量的方法重叠,例如 setTitle:
等,而且我不确定应该使用哪些方法。
谁能解释一下基本区别是什么?
谢谢,
泰贾
I've been reading through the Apple documentation about NSButtons and cells and I really can't seem to understand the distinction between the two. Adding to this complexity, it looks like both of them have a large overlap of methods like setTitle:
etc. and I'm not sure which ones I should use.
Can anyone explain what the basic differences are?
Thanks,
Teja
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将控件视为单元格的代表或“代理”。1 该控件是一个
NSView
,在这种情况下,这意味着两件重要的事情。首先,它代表要在其中绘制的窗口区域。其次,它接受用户交互。2不过,该控件本身并没有做太多事情。细胞是完成所有工作的东西——例如,请注意,细胞可以将自己绘制到给定的框架中。这是视图的责任,但控件将其推迟到单元格,只是提供一个用于绘图的区域。
同样,当用户单击控件时,控件会接收事件并弄清楚其含义,但响应该事件而执行操作的工作将传递给单元格。
如果您查看各种控件/单元对的文档(
NSButton
和NSButtonCell
就是这样的一对),您会看到提到“覆盖”方法。这意味着该控件具有与其对应单元格相同名称的方法,这些方法只是调用该单元格的方法。这就是你提到的重复的根源。当您想使用其中一种方法时,请在控件上调用它 - 作为该对的公共面孔,它很可能只是简单地询问单元格。Apple 提供的最佳交互描述是 控制和单元编程主题指南中的“控件和单元如何交互”。
1从演员有经纪人来获得演出的意义上来说。
2并非所有视图都严格如此;它实际上是
NSResponder
的一个功能,NSView
继承自该功能。You can think of a control as a cell's representative, or "agent".1 The control is an
NSView
, which means two important things in these circumstances. First, that it represents an area of a window to be drawn in. Second, that it accepts user interaction.2The control doesn't do very much itself, though. The cell is the thing that does all the work -- notice, for example, that cells can draw themselves into a given frame. This is the responsibility of a view, but the control defers that to the cell, simply providing an area for drawing.
Likewise, when a user clicks on a control, the control receives the event and figures out what it means, but the work of performing an action in response to the event is passed on to the cell.
If you look at the docs for various control/cell pairs (
NSButton
andNSButtonCell
being one such pair), you will see mention of "cover" methods. This means that the control has methods with the same names as its counterpart cell, which simply call through to the cell's. That's the source of the duplication that you mentioned. When you want to use one of these methods, call it on the control -- as the public face of the pair, it will most likely simply ask the cell anyways.The best Apple-provided description of the interaction is "How Controls and Cells Interact" in the Control and Cell Programming Topics guide.
1In the sense of a actor having an agent who procures gigs.
2This is not strictly true of all views; it's actually a feature of
NSResponder
from whichNSView
inherits.摘自 OS X 的 Cocoa 编程:大书呆子牧场指南
Excerpted from Cocoa Programming for OS X: The Big Nerd Ranch Guide
NSButtonCell
是NSActionCell
的子类,用于实现按钮、开关和单选按钮的用户界面。它还可用于视图的任何其他区域,该区域旨在在单击时向目标发送消息。NSControl
的NSButton
子类使用单个NSButtonCell
。要创建开关组或单选按钮组,请使用包含一组NSButtonCell
的NSMatrix
。NSButtonCell
is a subclass ofNSActionCell
used to implement the user interfaces of push buttons, switches, and radio buttons. It can also be used for any other region of a view that's designed to send a message to a target when clicked.The
NSButton
subclass ofNSControl
uses a singleNSButtonCell
. To create groups of switches or radio buttons, use anNSMatrix
holding a set ofNSButtonCell
s.NSButton Cell 有更多可定制的绘图和行为选项。从它们继承的类中可以更好地看出差异(NSButtonCell 继承自 ActionCell 类,而 NSButton 继承自 NSControl 类。
更好地查看文档:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference /ApplicationKit/Classes/NSButton_Class/Reference/Reference.html
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSButtonCell_Class/Reference/Reference.html
您应该选择基于如果您想要一个简单的按钮,请使用 NSButton。
NSButton Cell has alot more cutomizable options for drawing and behaviour. The differences are better seen in the classes they inherit from (NSButtonCell inherits from the ActionCell class, while the NSButton inherits from the NSControl class.
Take a look at the documentation better:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSButton_Class/Reference/Reference.html
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSButtonCell_Class/Reference/Reference.html
You should choose based on how you want the buttons to be drawn and behave. If its a simple button you want, use NSButton.