Cocoa Programming for Mac OS X - Aaron Hillegass [32]
To understand the AppKit framework, a good place to start is with the class NSControl. NSButton, NSSlider, NSTextView, and NSColorWell are all subclasses of NSControl. A control has a target and an action. The target is simply a pointer to another object. The action is a message (a selector) to send to the target. Recall that you set the target and action for two buttons in Chapter 2. There you set your Foo object to be the target of both buttons, and you set the action on one to seed: (Figure 5.1) and the action on the other to generate:.
Figure 5.1. A Button Has a Target and an Action
When the user interacts with the control, it sends the action message to its target. For example, when the button is clicked, the button sends the target its action message (Figure 5.2).
Figure 5.2. The Button Sends a Message
The action methods take one argument: the sender. This enables the receiver to know which control sent the message. Often, you will call back to the sender to get more information. For example, a check box will send its action message when it is turned on and when it is turned off. After getting the action message, the receiver might call back to the button to find out whether it is currently on or off:
- (IBAction)toggleFoo:(id)sender
{
BOOL isOn = [sender state];
...
}
To better understand NSControl, you should become acquainted with its ancestors: NSControl inherits from NSView, which inherits from NSResponder, which inherits from NSObject. Each member of the family tree adds some capabilities (Figure 5.3).
Figure 5.3. Inheritance Diagram for NSControl
At the top of the class hierachy is NSObject. All classes inherit from NSObject, and this is where they get the basic methods: retain, release, dealloc, and init. NSResponder is a subclass of NSObject. Responders have the ability to handle events with such methods as mouseDown: and keyDown:. NSView is a subclass of NSResponder and has a place on a window, where it draws itself. You can create subclasses of NSView to display graphs and allow the user to drag and drop data. NSControl inherits from NSView and adds the target and the action.
Some Commonly Used Subclasses of NSControl
Before using some controls, let’s take a brief look at the three most commonly used controls: NSButton, NSSlider, and NSTextField.
NSButton
Instances of NSButton can have appearances: oval, square, check box. They can also have various behaviors when clicked: toggle (like a check box) or momentarily on (like most other buttons). Buttons can have icons and sounds associated with them. Figure 5.4 shows the Attributes Inspector for an NSButton in Interface Builder.
Figure 5.4. Button Inspector
Here are three messages that you will frequently send to buttons.
- (void)setEnabled:(BOOL)yn
Enabled buttons can be clicked by the user. Disabled buttons are grayed out.
- (NSInteger)state
Returns NSOnState (which is 1) if the button is on, or NSOffState (which is 0) if the button is off. This method allows you to see whether a check box is checked or unchecked.
- (void)setState:(NSInteger)aState
This method turns the button on or off. It allows you to check or uncheck a check box programmatically. Set the state to NSOnState to check the check box and to NSOffState to uncheck it.
NSSlider
Instances of NSSlider can be vertical or horizontal. They can send the action to the target continuously while being changed, or they can wait to send the action until the user releases the mouse button. A slider can have markers, and it can prevent users from choosing values between the markers (Figure 5.5). Circular sliders are also possible.
Figure 5.5. Slider Inspector
Here are two