Objective-C Programming_ The Big Nerd Ranch Guide - Aaron Hillegass [65]
Figure 28.2 Creating a new Cocoa Application
Edit BNRDocument.h
Open BNRDocument.h and add a method and two instance variables: todoItems will be a mutable array of strings and itemTableView will be a pointer to the NSTableView object that will display the strings in todoItems. Also, declare that BNRDocument conforms to the NSTableViewDataSource protocol.
#import @interface BNRDocument : NSDocument { NSMutableArray *todoItems; IBOutlet NSTableView *itemTableView; } - (IBAction)createNewItem:(id)sender; @end Note that there is no instance variable for the Insert button. (You’ll see why later.) There is, however, an action for the button – the createNewItem: method. In the last chapter, the target of the button’s action was the instance of the application delegate class, BNRAppDelegate. A document-based application doesn’t have an application delegate object and instead is built around a subclass of NSDocument. For TahDoodle, that class is BNRDocument. In a document-based application, the user can have multiple instances of document objects open at the same time. So when TahDoodle is running, you can have multiple instances of BNRDocument (multiple to-do lists). Each instance will have its own table view, button, tasks array, and window. Each instance will respond to messages independently of the others, and each instance will be its own button’s target. In the declarations you entered, there are also two new terms: IBOutlet and IBAction. These tell Xcode “This is a pointer (IBOutlet) or an action method (IBAction) that the developer will use Interface Builder to connect rather than doing so programmatically.” A look at Interface Builder Right now, there is only one view object on the layout grid – a window object. That’s an instance of NSWindow, to which you’ll add other view objects shortly. Now, in the upper-right corner of the Xcode window, click on the righthand View button to reveal the Utilities. At the top of Utilities, click the button to reveal the inspector. The inspector is subdivided into separate inspectors. In this chapter, you’ll use the attributes, size, and connections inspectors. Figure 28.3 Attributes, size, and connections inspectors At the bottom of the Utilities pane, below the inspector, is the library, which is also divided into tabs. Select the button to reveal the object library. This library presents a list of all of the different object types that Interface Builder knows about, and it’s where you get the interface elements to drag and drop on the window object. At the bottom of the library is a search field. Type table to filter the list of objects. The first item, Table View, represents an instance of the NSTableView class. You can click on it to see its details. Edit BNRDocument.xib Figure 28.4 Adding an NSTableView Notice that the object’s edges snap to guides when they approach the edges of the window or other objects. These guides align view objects in accordance with Apple’s Human Interface Guidelines, or HIG. These are the rules that any developer should follow when designing user interfaces for the Mac. There are also HIGs for the iPhone and iPad. You can find all of the HIGs in the developer documentation. Now you’re going to set some of the table view’s attributes in the attributes inspector. Normally, you click on an object on the layout grid, and the inspector changes context to show you the attributes of that object. Getting to the attributes of an instance of NSTableView is trickier. The table view object that you dragged onto the window is actually a collection
In the project navigator, find and select a file named BNRDocument.xib. When you select a file in the project navigator that ends in .xib (XML Interface Builder document), Interface Builder appears in the editor pane, displaying a layout grid.
Drag an instance of NSTableView from the object library onto the window object. Resize the table to fill most of the window but leave room on the bottom for a button.