Learn Objective-C on the Mac - Mark Dalrymple [100]
• An NSPanel can be made to “float” in front of all the other windows in its application, including the main window.
• An NSPanel can be easily configured to not become the key window unnecessarily, so a user can click a button in an auxiliary panel and then continue typing in the main window.
Window Attributes
In the WindowLab project in Xcode, navigate to the MainMenu.xib file and double-click to open it in Interface Builder. One of the objects included in the application nib file is, as always, a window. Click to select it in the main nib window, then open the Attributes Inspector so that we can explore it a bit (see Figure 10-2). In earlier chapters, you’ve seen this inspector for setting a window’s title, but of course there’s more we can do here.
Figure 10-2. The Attributes Inspector for an NSWindow
You’re already familiar with the Title text field. Just below that is the Autosave field, which provides a very nice bit of functionality: enter a text string in this field, and the window will use that string as a key in the NSUserDefaults system, storing and retrieving its position and location based on that key. That means that as a user rearranges the windows in your application, the window positions are saved in the user’s preferences, and the windows will appear in the same locations the next time they run your app. And all you have to do to make it happen is fill in this text field for each window in your app, entering a unique value for each window.
Next, there are some checkboxes that let you turn off some of the standard window controls. Note that turning off these checkboxes won’t remove the corresponding buttons from the window’s title bar; it will just make them permanently grey and inactive while your app is running. Clicking to turn off the Resize checkbox will, however, make the resizing handle at the lower right disappear when your app runs (it’s always there in Interface Builder however, so that you can still modify your window even when you won’t allow the users to do so).
Below that are some checkboxes that for tweaking a window’s appearance, including enabling textured mode, disabling the drop-shadow behind the window, and more. The options that mention Toolbar here are referring to the optional NSToolbar that can be attached to a window, which we’ll explain a little later in this chapter.
The next chunk of checkboxes lets you fine-tune the window’s behavior in a number of ways, most of them self-explanatory. The exception is the Auto Recalculates View Loop checkbox, whose name is puzzling at best. Here’s the idea: each window maintains a list of all the view objects it contains, which the user can shuffle between by using the Tab key. If you turn this checkbox on, then any views that are added to the window while the app is running will automatically be inserted somewhere into this “loop.”
Finally, there are options related to a window’s memory usage. You should normally leave Deferred on, because this skips the allocation of some internal memory for the window until it’s about to actually display the window. You should normally leave One Shot turned off. If it’s on, that same internal memory is freed as soon as the window is closed, so this should only be turned on in case the window in question is a temporary window that can only be shown once per session, such as a splash screen. Finally, a popup window offers you the ability to switch the window’s “backing store” from Buffered to Retained or Unretained. Never do that! The Retained and Unretained options are only there to support specific sorts of legacy code, and all new Cocoa applications should leave this set to Buffered for every window.
Now pull an NSPanel out from the Library window, and look again at the Attributes Inspector, where you’ll see a few more checkboxes than you saw when inspecting an NSWindow. The most important ones here are Utility, which if enabled gives the panel a distinctive appearance, as well as making it float above the application