9

我一直在阅读有关 NSButtons 和单元格的 Apple 文档,但我似乎真的无法理解两者之间的区别。再加上这种复杂性,看起来它们都有大量的方法重叠,setTitle:我不确定我应该使用哪些方法。

谁能解释一下基本的区别是什么?

谢谢,
泰加

4

4 回答 4

10

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.

于 2011-05-24T19:12:30.593 回答
2

摘自Cocoa Programming for OS X:The Big Nerd Ranch Guide

“一个单元处理控件的实现细节。在大多数情况下,单元格的属性和方法被相应控件中的相同属性和方法“覆盖”。例如,当您更改标签上的标题时,标签的单元格正在工作,但您与标签的 stringValue 属性进行交互。

细胞在可可中有着悠久的历史。它们最初是为了解决性能问题而添加的:控件的许多基本任务都交给了单元,这可以比控件更有效地完成它们。

Mac 电脑比当时强大得多,而单元格已成为累赘。Apple 已声明它正在弃用单元格,但您仍会在文档大纲和旧代码中看到它们。”</p>

于 2016-01-19T03:18:35.240 回答
0

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

于 2016-01-28T12:54:05.517 回答
-2

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。

于 2011-05-24T15:41:54.203 回答