Learn Objective-C on the Mac - Mark Dalrymple [150]
Now we just have to bind the table view’s columns to the new controller. Select the left column, and use the Bindings Inspector to bind its Value column to attrDict, using arrangedObjects as the controller key and key as the key path. That will make the left column show the keys for each key-value pair in the dictionary. Now select the right column, and bind its Value column to attrDict, using arrangedObjects as the controller key and value as the key path.
Next, grab an NSTextView from the Library. Use the Attributes Inspector to turn off the Editable checkbox (we just want to display a file here, not edit it) as well as the Rich Text checkbox. Now is as good a time as any to bring the NSSplitView into play, as we hinted at earlier. Make the table view and the text view (actually, the scroll view that contains it) are about the same size, and line them up one above the other. Then select both of them, and choose Layout➤Embed Objects In➤Splitview from the menu. Doing so will line them up tightly, and draw the little dimple between them. If you test your interface now by pressing ⌘R, you’ll see that you can drag the dimple up and down to resize both views; then press ⌘Q to get back to editing your interface (Figure 15-6).
Figure 15-6. After disabling Rich Text, this is what your text view will probably look like. No need to delete the exhilarating prose that appears here by default; it won’t appear in the running application.
Now select just the text view again. Remember, you can avoid the ambiguity and extra steps involved to select the correct object in the view hierarchy by Shift-Ctrl-clicking the text view and selecting the text view from the stack of objects that’s displayed. Switch to the Bindings Inspector, and bind the text view’s Value to the app delegate’s fileStringValue key path.
Finally, let’s provide a GUI for the list of string encodings that will be used to let us reinterpret the contents of the selected file. For this, you’ll need a label and a popup button from the Library. Lay them out as shown, including the extra-wide popup button, since some of the entries in the list will be quite long.
Figure 15-7. Really, who wouldn’t want their popup button wide enough to fully display “NSUTF32LittleEndianStringEncoding”?
The values for the popup list will be retrieved from the app delegate’s encodingNames method, so you don’t need to enter them in Interface Builder. Here we’ll use NSDictionaryController again, this time to display just the values from the encodingNames dictionary. In order to do that, you need to first of all drag another NSDictionaryController from the Library to the nib window, renaming it strEncs while you’re at it, and bind its Content Dictionary to the app delegate’s encodingNames key path. Then, bind the popup button’s Content to strEncs, using arrangedObjects as the Controller Key, and value as the Model Key Path.
Finally, we need to establish a binding so that the value the user sets in the popup is noticed by the app delegate, making the app delegate redisplay the text using the selected encoding. Do this by binding the popup button’s Selected Object to the app delegate, using stringEncodingName as the key path.
Now save all your changes, and Build & Run the app. You should see the GUI you’ve built, including a File➤Open menu item that lets you choose a file, and will show you the file’s attributes as well as its content. Choosing another encoding from the popup list will make the app redisplay the data in the text view using the chosen encoding.
Filing It All Away
In this chapter, you’ve seen how to use Cocoa to access files and their metadata. You’ve also learned a bit about string encodings, and how Cocoa deals with them. What’s more, you’ve seen another example of a GUI powered by Cocoa Bindings. Except for the menu item that opens a file, everything that happens here, happens through bindings, including