Cocoa Programming for Mac OS X - Aaron Hillegass [11]
3. RandomController has two methods: seed: and generate: are action methods.
By convention, the names of methods and instance variables start with lowercase letters. If the name would be multiple words in English, each new word after the first one is capitalized—for example, favoriteColor. Also by convention, class names start with capital letters—for example, RandomController.
Save RandomController.h.
Create an Instance
Next, you will create an instance of the class RandomController in your XIB file. Select MainMenu.xib to return to Interface Builder. From the Library panel, drag a blue Object (under Cocoa -> Objects & Controllers), and drop it onto the Interface Builder dock (Figure 2.15).
Figure 2.15. Drag an Object onto the Interface Builder dock.
In the Identity Inspector, set its class to RandomController (Figure 2.16). (Your actions and outlets should appear in the Connections Inspector. If they do not, check RandomController.h. You have a mistake in it, or it hasn’t been saved.)
Figure 2.16. Setting the Class
Make Connections
A lot of object-oriented programming has to do with which objects need to know about which other objects. Now you are going to introduce some objects to each other. Cocoa programmers would say, “We are now going to set the outlets of our objects.” To introduce one object to another, you will Control-drag from the object that needs to know to the object it needs to know about. The object diagram in Figure 2.17 shows which objects need to be connected in your example.
Figure 2.17. Object Diagram
You will set RandomController’s textField instance variable to point to the NSTextField object on the window that currently says Label. Right-click (or Control-click if you have a one-button mouse) on the icon that represents your instance of RandomController. The Connection panel will then appear. Drag from the circle beside textField to the text field that says Label (Figure 2.18).
Figure 2.18. Set the textField Outlet
This step is all about pointers: You have to just set the pointer textField in your RandomController object to point to the text field.
Now you will set the Seed button’s target outlet to point to your instance of RandomController. Furthermore, you want the button to trigger RandomController’s seed: method. Control-drag from the button to your instance of RandomController. When the panel appears, select seed: (Figure 2.19).
Figure 2.19. Set the Target and Action of the Seed Button
Similarly, you will set the Generate button’s target outlet to point to your instance of RandomController and set its action to the generate: method. Control-drag from the button to RandomController. Choose generate: in the Received Actions panel (Figure 2.20).
Figure 2.20. Set the Target and Action of the Generate Button
A Look at Objective-C
If this is the first time that you are seeing Objective-C code, you may be alarmed to discover that it looks quite different from C++ or Java code. The syntax may be different, but the underlying concepts are the same. For example, a class in Java would be declared like this:
import com.megacorp.Bar;
import com.megacorp.Baz;
public class Rex extends Bar implements Baz {
...methods and instance variables...
}
This says, “The class Rex inherits from the class Bar and implements the methods declared in the Baz interface.”
The analogous class in Objective-C would be declared like this:
#import #import @interface Rex : Bar ...instance variables... } ...methods... @end If you know Java, Objective-C really isn’t so strange. Note that like Java, Objective-C allows only single inheritance; that is, a class has only one superclass. Types and Constants in Objective-C • id is a pointer to any type of object. • BOOL is the same as char but
Objective-C programmers use a few types that are not found in the rest of the C world.