Command Signatures and Control IDs in OS X
Some controls, such as radio buttons and checkboxes, usually don't initiate any action when clicked. The control's state might changefor instance, a checkbox will become checked if it was uncheckedbut the act of clicking the control won't generally cause a change in the program. Instead, the program examines the state of such a control at a later time (such as when a Done button is clicked) and then an action might be initiated. It is this clicking of a button that more often causes an immediate action. These two different types of behavior that a control can exhibit explain why one control is assigned a command signature and another control is assigned a control ID.
Consider the window resource shown in Figure 1. It includes a static text item (the Select a beep option, then click the Beep button string), a push button control (the button titled Beep), and a radio button group control (all three radio buttons comprise a single control).
Figure 1 - A window that includes a push button and a radio button group.
The push button is the type of control usually assigned a command signature (also referred to as a command). You've worked with commands before, so you know that a command is a four-character constant assigned to a control. Assigning a command signature to an item in a resource file enables a program's code to respond to the user's selection of that item. It's important to note that assigning a command signature to a control doesn't give the program any indication of which control has been selected by the user. For instance, if you assign each of two buttons a command of beep, the program responds the same way to a click on either button, and the program won't know or care which button initiated the action.
You've also seen that you can assign a menu item the same command as a button, and again, the program properly responds in the same manner to either a click on the button or a selection of the menu item. Again, the program won't know, or care, which action generated the command. Figure 2 shows the push button being assigned a command signature of beep.
Figure 2 - Assigning a command signature to a push button control.
The radio button group control pictured in Figure 5.1 is the type of control that is usually assigned a control ID rather than a command. A control ID consists of two components: a signature and an ID. The signature is a four-character constant, and the ID is an integer value. Be aware that there's the potential for confusion here. The control ID is composed of two parts: a signature and an integer. The integer is referred to as the ID. Thus, a control ID is the combination of two identifiers, while the ID is one of those identifiers. Tricky wording, to be sure. Figure 3 shows a control ID with a signature of SPRB and an ID of 1 being assigned to the radio button group.
Figure 3 - Assigning a control ID to a radio button group control.
The four-character constant used for the signature is typically the same as the program's creator code, which is the application signature. Each program that's released to the public should have its own creator code, and that creator code should be registered with Apple. In such a case, any four characters can be used as the signature.
This book's examples haven't been assigned creator codesit's a bit unlikely that any of them will ship as commercial products!
The commonly used convention for assigning control IDs to a program's controls is to use the same four-character constant as the signature for every control ID and to use a unique integer value as the ID of every control ID. For instance, if I were to add two more controls that each required a control ID to the window pictured in Figure 5.1, their control IDs might each have a signature of SPRB, while their IDs would have values of 2 and 3. By giving each control that requires a control ID a unique value, your program can identify that particular control. This isn't possible if your program assigns only a command to a control.
When should you assign a control ID rather than a command to a control? The answer is this: when your program needs to access a particular control. When will your program need to access a control? Typically when the program needs to get, or set, the control's value.
Consider the window pictured in Figure 5.1. The value of a radio button group is a number that identifies which radio button is currently on (only one radio button in a group can be on at any given time). In this case, the program will need to get the value of the radio button group control when the user clicks the Beep button. When the user clicks the Beep button, the program accesses the radio button group control, determines which radio button is on, and then uses that information to sound the appropriate number of beeps. For the radio button group, a command won't do. The program needs a unique identifier for this control so that it can access the control. A control ID is unique; a command might not be.
A button is usually assigned a command. This chapter also covers radio button groups, text input fields, and checkboxes. Each type of control usually has a control ID assigned to it.