Cocoa Programming for Mac OS X - Aaron Hillegass [46]
// of smarts to it in the next chapter.
if (a == employees)
return;
employees = a;
}
RMDocument.xib
Click on RMDocument.xib to open it in the Interface Builder editor.
Delete the text field that says Your document contents here. Drop a table view and two buttons onto the window. Relabel and arrange them as shown in Figure 8.6. Use the Attributes Inspector to make sure that the table view’s Content Mode is set to Cell Based.
Figure 8.6. Document Window
Drag out an NSArrayController (from Cocoa->Objects & Controllers) and drop it into the editor area. An NSArrayController icon will appear in the Interface Builder dock.
Ensure that the array controller is selected in the dock. Open the Attributes Inspector for the NSArrayController instance; under Object Controller, set the Class Name to Person. Add the keys personName and expectedRaise as shown in Figure 8.7.
Figure 8.7. Configure NSArrayController’s Class Name and Keys
With the array controller still selected, change to the Bindings Inspector (its icon resembles a knot). Find the Content Array binding in the list and expand it. Check Bind to; in the pop-up; select File’s Owner. Leave the Controller Key blank and enter employees for the Model Key Path (Figure 8.8). Cocoa programmers would say that they are binding the Content Array of the array controller to the employees array of File’s Owner (which is the instance of RMDocument).
Figure 8.8. Bind the Array Controller’s Content Array
Now that we have configured the array controller, we will bind the table-view columns to display the contents of the array controller. In Chapter 6, we implemented the NSTableViewDataSource protocol to populate the table view; bindings will allow us to skip that code.
The first column of the table view will display each employee’s name. Click and double-click the column to select it. (At no time in this book will you bind a scroll view or cell; doing so is a common mistake, so keep an eye on the jump bar above the editor.) In the Bindings Inspector, find the Value binding. Check Bind to and select the Array Controller in the pop-up. Set the Controller Key to arrangedObjects and the Model Key Path to personName (Figure 8.9). Again, Cocoa programmers would say that they are binding the column’s value to the personName of the arrangedObjects of the array controller.
Figure 8.9. Bind the First Column to the Array Controller
The second column of the table view displays each employee’s expected raise. Find the Number Formatter in the Library (in Library->Cocoa->Controls) and drag it onto the second column’s cell (Figure 8.10).
Figure 8.10. Add a Number Formatter
In the Inspector, with the number formatter selected, set the formatter (a 10.4+ formatter) to display the number as a percentage and enable Lenient, which will make it less picky about input, as shown in Figure 8.11.
Figure 8.11. Number Formatter Attributes
Reselect the second column. In the Bindings Inspector, bind value to the expectedRaise of the arrangedObjects of the Array Controller, just as you did with the first column (Figure 8.12).
Figure 8.12. Bind the Second Column to arrangedObjects.expectedRaise
Control-drag from the Add Employee button to the array controller to set the target of the button. Set the action to add:.
Control-drag to make the array controller become the target of the Remove button. Set the action to remove:. Also, in the Bindings Inspector, bind the button’s enabled binding to the canRemove attribute of the NSArrayController, as shown in Figure 8.13.
Figure 8.13. Binding the Enabled Attribute of the Remove Button
The user will also want to remove the selected employees by pressing the delete key on his or her keyboard. Select the Remove button; in the Attributes Inspector, set the keyboard equivalent to the Delete key (Figure 8.14). To set the key equivalent, click in the text field and then type the key you wish to associate with this button. Xcode will record the keystroke used and display it in the text field.