NSButtonCell vs NSButton
Asked Answered
B

4

9

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

Brigantine answered 24/5, 2011 at 15:25 Comment(0)
G
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.

Goldshell answered 24/5, 2011 at 19:12 Comment(1)
I see that it is the same as UIView and CALayer?Fitzsimmons
F
2

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

Fitzsimmons answered 19/1, 2016 at 3:18 Comment(0)
H
0

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.

Hydroelectric answered 28/1, 2016 at 12:54 Comment(0)
E
-2

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.

Eisenhower answered 24/5, 2011 at 15:41 Comment(1)
I'm not sure I follow you. Isn't NSButtonCell a part of NSButton? I think allocating NSButton and calling [button cell] returns the NSButtonCell associated with it. That was what I was confused about.Brigantine

© 2022 - 2024 — McMap. All rights reserved.