Online Book Reader

Home Category

Cocoa Programming for Mac OS X - Aaron Hillegass [10]

By Root 772 0

For the More Curious: XIBs and NIBs

A XIB file is an XML representation of user interface objects and their connections. When you build your application, the XIB file is compiled into a NIB file. The XIB file is easier to work with, particularly for source control, but the NIB file is smaller and easier to parse, which is why the file that ships with your application is a NIB. Generally speaking, you will manipulate only XIB files, and your application will use only NIB files, but most developers use the words XIB and NIB interchangeably. (Trivia: “NIB” stands for “NeXT Interface Builder”; “NS” stands for “NeXTSTEP.”)

Lay Out the Interface


We are going to walk you through it, but keep in mind that your goal is to create a user interface that looks like Figure 2.9.

Figure 2.9. Completed Interface

Select Cocoa in the library selector bar. Drag a button from the Library window (as shown in Figure 2.10) and drop it onto the blank window. (To make it easier to find, you can either select the Cocoa -> Controls group in the pop-up at the top of the Library panel or type button in the search field.)

Figure 2.10. Dragging a Button

Double-click on the button to change its title to Seed random number generator using time.

Copy and paste the button. Relabel the new button Generate random number. Drag out a Label text field (as shown in Figure 2.11) and drop it onto the window.

Figure 2.11. Dragging a Text Field

To make the text field as wide as the buttons, drag the left and right sides of the text field toward the sides of the window. (You may notice that blue lines appear when you are close to the edge of the window. These guides are intended to help you conform to Apple’s GUI guidelines.)

Make the window smaller by dragging the transparent handles surrounding it.

To make the text field center its contents, you will need to use the Attributes Inspector. Select the text field, and select the Attributes Inspector tab at the top of the inspector panel. Click on the center-justify button (Figure 2.12).

Figure 2.12. Text Field Attributes Inspector

The Dock


In your XIB file, some objects, such as buttons, are visible, and others (like your custom controller objects), are invisible. The icons that represent the invisible objects appear in the dock.

The dock contains icons representing the main menu and the window. First Responder is a fictional object, but it is a very useful fiction. It will be fully explained in Chapter 21. File’s Owner in this XIB is the NSApplication object for your application. The NSApplication object takes events from the event queue and forwards them to the appropriate window. We will discuss the meaning of File’s Owner in depth in Chapter 12.

Create a Class


In Objective-C, every class is defined by two files: a header file and an implementation file. The header file, also known as the interface file, declares the instance variables and methods your class will have. The implementation file defines what those methods do.

In Xcode, use the File->New->New File... menu item to create a new Cocoa -> Objective-C class. Name the class RandomController and set it to be a subclass of NSObject. (Figure 2.13).

Figure 2.13. Create a New Class

The files RandomController.h and RandomController.m will appear in your project. If they don’t appear in the Random group, drag them there (Figure 2.14).

Figure 2.14. RandomController.h and .m in the Random Group

In RandomController.h, you will add instance variables and methods to your class. Instance variables that are pointers to other objects are called outlets. Methods that can be triggered by user interface objects are called actions.

Edit RandomController.h to look like this:

#import

@interface RandomController : NSObject {

IBOutlet NSTextField *textField;

}

- (IBAction)seed:(id)sender;

- (IBAction)generate:(id)sender;

@end

What can an Objective-C programmer tell from this file?

1. RandomController is a subclass of NSObject.

2. RandomController

Return Main Page Previous Page Next Page

®Online Book Reader