Cocoa Programming for Mac OS X - Aaron Hillegass [64]
Bindings on cell-based table views are not as obvious but are simpler: Bind the table column only. Bindings on view-based table views are completely different but more logical: Bind the table view’s contents and then bind individual controls within the cell views directly to the desired property. This has the advantage of allowing for relatively trivial compound cells (cells containing multiple controls).
Challenge
After studying the code for createNewEmployee: in the RaiseMan application, make the CarArrayController select the Make/Model column of the table view when a new record is created. Hint: You will need to add an outlet to CarArrayController.
Chapter 12. NIB Files and NSWindowController
In RaiseMan, you are already using two NIB files: MainMenu.nib and RMDocument. nib. (Recall that the compiler converts our XIB files to NIB files during the build process.) MainMenu.nib is automatically loaded by NSApplication when the application first launches. RMDocument.nib is automatically loaded each time an instance of RMDocument is created. In this chapter, you will learn how to load NIB files by using NSWindowController.
Why would you want to load a NIB file? Most commonly, your application will have several windows (such as a Find panel and a Preferences panel) that are used only occasionally. By putting off loading the NIB until the window is needed, your application will launch faster. Furthermore, if the user never needs the window, your program will use less memory.
NSPanel
In this chapter, you will create a Preferences panel. The panel will be an instance of NSPanel, which is a subclass of NSWindow. There are not that many differences between a panel and a general window, but because a panel is meant to be auxiliary (as opposed to a document window), it acts a little differently.
• A panel can become the key window without becoming the main window. For example, when bringing up a Print panel, the user can type into it (it is key), but the document the user was looking at remains the main window (that is what will be printed). NSApplication has a mainWindow outlet and a keyWindow outlet. Both outlets point at the same window unless a panel is involved; panels do not typically become the main window.
• If it has a close button, you can close a panel by pressing the Escape key. Panels do not appear in the window list in the Window menu. After all, a user who is looking for a window is probably looking for a document, not a panel.
All windows have a Boolean variable called hidesOnDeactivate. If it is set to YES, the window will hide itself when the application is not active. Most document windows have this variable set to NO; most auxilary panels have it set to YES. This mechanism reduces screen clutter. You can set hidesOnDeactivate by using the Attributes Inspector in Interface Builder.
Adding a Panel to the Application
The Preferences panel that you are going to add will not do anything except appear for now. In Chapter 13, however, you will learn about user defaults and will make the Preferences panel do something.
The Preferences panel will be in its own NIB file. You will create a subclass of NSWindowController called PreferenceController. An instance of PreferenceController will act as the controller for the Preferences panel. When creating an auxiliary panel, it is important to remember that you may want to reuse it in the next application. Creating a class to act just as a controller and a NIB that contains only the panel makes it easier to reuse the panel in another application. Hip programmers would say, “By making the application more modular, we can maximize reuse.” The modularity also makes it easier to divide tasks among several programmers. A manager can say, “Rex, you are in charge of the Preferences