NSButtonCell 与 NSButton

发布于 2024-11-09 03:05:14 字数 173 浏览 0 评论 0原文

我一直在阅读有关 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

-残月青衣踏尘吟 2024-11-16 03:05:14

您可以将控件视为单元格的代表或“代理”。1 该控件是一个 NSView,在这种情况下,这意味着两件重要的事情。首先,它代表要在其中绘制的窗口区域。其次,它接受用户交互。2

不过,该控件本身并没有做太多事情。细胞是完成所有工作的东西——例如,请注意,细胞可以将自己绘制到给定的框架中。这是视图的责任,但控件将其推迟到单元格,只是提供一个用于绘图的区域。

同样,当用户单击控件时,控件会接收事件并弄清楚其含义,但响应该事件而执行操作的工作将传递给单元格。

如果您查看各种控件/单元对的文档(NSButtonNSButtonCell 就是这样的一对),您会看到提到“覆盖”方法。这意味着该控件具有与其对应单元格相同名称的方法,这些方法只是调用该单元格的方法。这就是你提到的重复的根源。当您想使用其中一种方法时,请在控件上调用它 - 作为该对的公共面孔,它很可能只是简单地询问单元格。

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.2

The 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 and NSButtonCell 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 which NSView inherits.

百合的盛世恋 2024-11-16 03:05:14

摘自 OS X 的 Cocoa 编程:大书呆子牧场指南

“单元格处理控件的实现细节。对于
大多数情况下,细胞的特性和方法被“涵盖”
相应控件中的相同属性和方法。为了
例如,当您更改标签上的标题时,标签的单元格是
完成工作,但您与标签的 stringValue 进行交互
属性。

细胞在可可中有着悠久的历史。他们原本是
添加以解决性能问题:a 的许多基本任务
控制权被交给了细胞,这可以让他们做更多的事情
比控制有效。

Mac 电脑更强大
与那时相比,细胞已成为一种累赘。
苹果已表示正在弃用 Cells,但
您仍然会在文档大纲和旧代码中看到它们。”

Excerpted from Cocoa Programming for OS X: The Big Nerd Ranch Guide

“A cell handles the implementation details of the control. For the
most part, the cell’s properties and methods are “covered” by
identical properties and methods in the corresponding control. For
instance, when you change the title on a label, the label’s cell is
doing the work, but you interact with the label’s stringValue
property.

Cells have a long history in Cocoa. They were originally
added to address performance issues: many of the basic tasks of a
control were handed off to the cell, which could do them more
efficiently than the control.

Mac computers are much more powerful
than they were in those days, and cells have become an encumbrance.
Apple has stated that it is in the process of deprecating cells, but
you will still see them in your document outline and in older code.”

屋檐 2024-11-16 03:05:14

NSButtonCellNSActionCell 的子类,用于实现按钮、开关和单选按钮的用户界面。它还可用于视图的任何其他区域,该区域旨在在单击时向目标发送消息。
NSControlNSButton 子类使用单个 NSButtonCell。要创建开关组或单选按钮组,请使用包含一组 NSButtonCellNSMatrix

NSButtonCell is a subclass of NSActionCell 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 of NSControl uses a single NSButtonCell. To create groups of switches or radio buttons, use an NSMatrix holding a set of NSButtonCells.

半透明的墙 2024-11-16 03:05:14

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文