Cocoa Programming for Mac OS X - Aaron Hillegass [123]
In DepartmentView.xib, put two buttons, two table views, and a pop-up button on the view. Place a label that says Manager above the pop-up. Embed the label, the pop-up, and one table view in a box. Create three array controllers and label them Depts, ManagerPopUp, and EmployeeList (Figure 32.5).
Figure 32.5. Basic Layout
Set the target of the two buttons to be the Depts array controller. The Add button’s action should be add:. The Remove button’s action should be remove:.
The Depts array controller should be in Entity mode and pulling from the Department entity. Check the box that says Prepares Content, forcing it to fetch from the object store as soon as the NIB file is loaded (Figure 32.6).
Figure 32.6. Depts Array Controller Attributes
Use Table 32.1 to set the bindings. Bindings are set in the Bindings Inspector.
Table 32.1. Bindings for DepartmentView.xib (AC = Array Controller)
On the last binding, use No selection for the No Selection Placeholder. Use Unnamed Department as the Null Placeholder.
You can build and run your app. You should be able to add and remove departments.
EmployeeView.xib
Open EmployeeView.xib, and remove anything on the view. Add a table view with three columns. Drop a pop-up cell (from the Library’s Cocoa->Views & Cells->Cells) on the third column. Add Add and Remove buttons. Add two array controllers and label them Employees and DeptPopUp. See Figure 32.7.
Figure 32.7. Basic Layout
The Employees array controller should be in Entity Name mode and pulling from the Employee entity. The DeptPopUp array controller should be in Entity mode and pulling from the Department entity. Both should automatically prepare content.
Make the Employees array controller the target of the two buttons. The Add button should trigger add:. The Remove button should trigger remove:.
Set the bindings according to Table 32.2.
Table 32.2. Bindings for EmployeeView.xib
Build and run your app. You should now be able to add and remove employees. You should be able to set the manager of a department as well.
Events and nextResponder
The event methods (such as mouseDown: and keyDown:) defined in NSResponder typically just forward the event on to the nextResponder. Thus, unhandled events flow up the responder chain.
So, for example, when someone selects a row in a table view and presses the Delete key, that flows up the responder chain until it is handled. Let’s handle this case for the EmployeeView of the Departments project. The NSViewController is a subclass of NSResponder, so we can put it in the responder chain and it can handle unhandled keyboard events. Add these lines to the end of displayViewController: in MyDocument.m:
[box setContentView:v];
// Put the view controller in the responder chain
[v setNextResponder:vc];
[vc setNextResponder:box];
}
Add these two methods to EmployeeViewController.m:
// Accept key events
- (void)keyDown:(NSEvent *)e
{
[self interpretKeyEvents:[NSArray arrayWithObject:e]];
}
// Take care of the delete key
- (void)deleteBackward:(id)sender
{
[employeeController remove:nil];
}
Now, if it gets a keyDown: event, the view controller will ask that the event be interpreted. If the key event was a Delete key press, deleteBackward: will be called, in which we send the remove: message to the array controller.
Now you need an outlet named employeeController. Add it in EmployeeViewController.h:
IBOutlet NSArrayController *employeeController;
Open EmployeeView.xib. Control-click on the File’s Owner. Drag from the employeeController outlet to the Employees array controller.
Build and run the application. Select an employee and press the Delete key. The employee should disappear.
Chapter 33. Core Animation
As Mac OS X has evolved, it has used OpenGL more and more to utilize the power of modern graphics processors. To make some of these capabilities convenient for all programmers, Apple created Core Animation in Mac OS X 10.5. Core Animation is extremely