Learn Objective-C on the Mac - Mark Dalrymple [40]
If you now open the Connections Inspector (⌘5) you will see a list of all the outlets and actions that we previously defined in the header file, placed conveniently in alphabetical order. Now let’s hook it up.
Making All the Connections
You’ll need to connect each of the VillainTrackerAppDelegate outlets to the correct object in the window, connect each control’s action back to the correct method in VillainTrackerAppDelegate, and make a few delegate connections to VillainTrackerAppDelegate. Let’s start with the VillainTrackerAppDelegate outlets.
Leave the Connections Inspector open, so you have a nice view of all the VillainTrackerAppDelegate’s connections and can see on the fly which outlets are already hooked up. Start with the first outlet in the list (evilnessView, unless you’ve named things differently than we’ve described), and drag a connecting line from VillainTrackerAppDelegate to the correct view in the window, either by holding down ctrl while you click or by clicking using the right mouse button instead of the left. While dragging the line around, you’ll see that it highlights as you drag it over each object in the window. Actually, it doesn’t highlight over every object, but only over the objects for which the source of the drag (Villain_Tracker_AppDelegate in this case) has an outlet of the same type. This is a handy feature that lets you home in on just the objects that might be relevant.
When you finish dragging to the evilnessView and release the button, a small contextual window appears, showing you the outlets that can be connected to the target object. In this case, since we only have one outlet for a level indicator, that outlet (evilnessView) is the only one in the list; Click on it to make the connection.
Continuing going through the VillainTrackerAppDelegate’s remaining outlets one by one, connecting each to the correct object in the window. You shouldn’t encounter any surprises along the way; The only thing to watch out for is that when you’re making a connection for an outlet whose type is represented more than once in the class declaration (for example, powersView and primaryMotivationView, both of which are pointers to an NSMatrix), make sure you click on the correct item in the contextual window after dragging. Figure 4-15 shows a view of Interface Builder running with the VillainTrackerAppDelegate selected, and displaying all its connections in the Connection Inspector.
Figure 4-15. The completed GUI and all its connections
Now that the VillainTrackerAppDelegate’s “outbound” connections are done, it’s time to set up its “inbound” connections. You’ll do this by dragging a connection from each control back to the VillainTrackerAppDelegate, and then selecting the appropriate action method. Start with the first text field, the one labeled Name. Drag a connecting line from the text field to the VillainTrackerAppDelegate in the nib window, and release the mouse button. You’ll see the contextual window again, but now with more items in it, and you’ll see that it’s split up into two sections, Outlets and Received Actions. Text fields and some other objects have a delegate outlet that can be used to extend their functionality in certain ways, but we’re going to skip that for now and just connect to the takeName: method listed under Received Actions.
Now go on to the Last known location text field, and connect it to the VillainTrackerAppDelegate’s takeLastKnownLocation: method. Go on to connect the Last seen date picker to the VillainTrackerAppDelegate’s takeLastSeenDate: method, and continue connecting each of the controls in the upper part of the window (basically everything except for the large text view) to its corresponding action method in the VillainTrackerAppDelegate.
The text view is somewhat of a different beast. Unlike the other editable objects in our window, NSTextView is not a subclass of NSControl, which means that it doesn’t know anything about the target/action pattern. However, it does expose a lot of functionality through calls to methods in its