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
A control that defines an area on the screen that a user clicks to trigger an action. macOS 10.0+
Add A Solid Background Next, the backgroundColor property on layer is set to the configured backgroundColor . To make the button interactive, the background color is darkened when isHighlighted is true. This will darken the background color when a user clicks on the button.
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.
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.”
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With