Types of Button
The button type determines how the button acts: how it highlights when pressed and whether it shows its state. The button types fall into three categories:
You set the button type with setButtonType:
.
Push Buttons
These buttons are most useful for triggering actions, since they don’t show their state. They change their appearance when the mouse button is held down and return to their original appearance when the mouse button is released.
To let
NSButton
control the appearance of a button being pressed, useNSMomentaryPushInButton
(called “Momentary Push” in Interface Builder’s Button Inspector). When the mouse button is down, the button appears to be pushed in.Here’s an example of a
NSMomentaryPushInButton
button with a bezel style ofNSRoundedBezelStyle
, in both the normal and the pushed-in appearance:And here’s a sample of a
NSMomentaryPushInButton
button with a bezel style ofNSThickerSquareBezelStyle
. The bezel stylesNSRegularSquareBezelStyle
andNSThickSquareBezelStyle
are similar.To control the appearance of a button being pressed yourself, use
NSMomentaryChangeButton
(called “Momentary Change” in Interface Builder’s Button Inspector). When the mouse button is down, it displays the alternate image and alternate title. When the mouse button is released, it displays the normal image and title. If you haven’t set an alternate image or name for the button, its appearance doesn’t change.
Sticky Buttons
These buttons show their state and appear to stick when pressed. After you click one, it appears to stay pressed until you click it again.
To let
NSButton
control the appearance of a pressed button, useNSPushOnPushOffButton
(called “Push On/Push Off” in Interface Builder’s Button Inspector). After being clicked once, the button appears to be pushed in. After being clicked again, the button appears to pop back up. The popped-up appearance is for the off state (NSOffState
), and the pressed-in appearance is for the on and mixed states (NSOnState
andNSMixedState
). This is useful for a button that displays the state of something in your application (for example, a button that displays whether the selected text is in boldface).To control the appearance of a button being pressed, use
NSToggleButton
(called “Toggle” in Interface Builder’s Button Inspector). After being clicked once, the button displays its alternate image and title. After being clicked again, the button displays its normal image and title. If there’s no alternate image or title, the button’s appearance doesn’t change. The normal image and title are for the off state (NSOffState
), and the alternate image and title are for the on and mixed states (NSOnState
andNSMixedState
). This is useful for a button that toggles between two actions (for example, Stop and Start).
If you want a button to display different appearances for all three states, you must subclass NSButton
.
Radio Buttons and Checkboxes
These buttons display the state of something in your application. They’re specialized versions of NSToggleButton
that have system-defined images.
To choose between two choices, use
NSSwitchButton
, which looks like a check box. This type of button is available as a separate palette item in Interface Builder.To choose among more than two choices, use a matrix of
NSRadioButton
buttons. The matrix and the radio buttons work together to make sure that only one button is on at a time. This type of button is available as a separate palette item in Interface Builder.
Changing the images used for these buttons could lead to unpredictable results. If you want a switch or radio button with a customized appearance, either customize a toggle button or subclass NSButton
.
Although checkboxes and radio buttons can display different images for all three states, other types of buttons cannot.
Copyright © 2014 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2014-10-16