Online Book Reader

Home Category

Learn Objective-C on the Mac - Mark Dalrymple [22]

By Root 876 0
have here, the File’s Owner icon will always point to an instance of NSApplication, which is the class that represents the entire application, which receives input and makes sure the appropriate code gets called as a result of that input. For other nib files, File’s Owner could be a different class, such an instance of a document class, or the class that represents a plug-in.

The second icon in this and any other nib file is called First Responder. We’ll talk more about responders in Chapter 10, but the first responder is the object with which the user is currently interacting. If, for example, the cursor is typing in a text field, that text field is the current first responder. The first responder changes as the user interacts with the interface and the First Responder icon gives you a convenient way to interact with whatever control or view currently has the focus without having to write code to determine which control or view that is.

The third icon, called Application (or the application proxy) is a relatively new addition to Cocoa nib files. This object points to this application’s one and only instance of NSApplication. In a MainMenu.xib file, the application proxy, and the File’s Owner proxy will always point to exactly the same thing. The application proxy gives you access to your application’s NSApplication instance from any nib file, even ones whose File’s Owner is not NSApplication. For this chapter, we can forget about the application proxy, because this nib’s File’s Owner already gives us access to that object.

Creating the Controller Instance


In the library, look under Objects & Controllers for an object called NSObject. Drag that from the library to the nib’s main window. As your cursor gets over the window, it will change so it has a plus icon on it (Figure 3-5) indicating that letting go of the mouse now will add this object to the nib. Let go of the mouse button to put a new object instance into the nib.

Figure 3-5. As you drag an object from the library over to the nib, if the nib can accept the dragged object, the cursor will get a green plus sign on it to indicate that fact.

At this point, we’ve added an instance of NSObject to our nib. That’s not exactly what we want, but the library doesn’t contain an instance of ButtonsAppDelegate, the class that we just created, so we have to grab the closest match (in this case, our class’ superclass), and then change the underlying class to be correct. We can do that by single-clicking the new icon called Object and pressing ⌘6 to bring up the identity inspector (Figure 3-6).

The identity inspector allows us to specify the underlying class for this object instance. Because what we want is to create an instance of ButtonsAppDelegate, we need to change the value in the Class field from NSObject to ButtonsAppDelegate, as in Figure 3-6. Once you change the class, you should see the action and outlet we declared earlier in the Class Actions and Class Outlets sections of the inspector.

Once you hit return to commit the change, look back in the nib’s main window, and you’ll notice that the new icon’s name has changed from Object to Main Window…. Interface Builder automatically changed the name of the icon to reflect the new underlying class. The new name is longer than can be displayed in the current view, so Interface Builder has truncated it and placed an ellipsis at the end to let us know that it isn’t displaying the full name. If you hold your cursor over the icon for a few seconds, a tool tip will appear with the full name.

Figure 3-6. The identity inspector is where you can change the underlying class of an object in your nib. This window is somewhat different in Snow Leopard, where the sections for actions and outlets have been moved to a different part of the GUI, namely the Library panel. We think Apple does this just to keep book authors on their toes.

The name of the icon here in the nib can be changed to anything we want. If we had three instances of the same class, it would be very hard to tell them apart if they all had the

Return Main Page Previous Page Next Page

®Online Book Reader