Online Book Reader

Home Category

Learn Objective-C on the Mac - Mark Dalrymple [149]

By Root 908 0
Builder, and start off by making a connection so that a menu item can call our app delegate’s chooseFile: method. Open the menu inside the main nib window, go into the File menu, and Ctrl-drag a connection from the Open item to the icon representing the app delegate in the main nib window. Then select chooseFile: from the small menu that appears.

Now it’s time to get started on the window itself. This GUI is completely powered by Cocoa Bindings. Our controller doesn’t have any outlets pointing at anything in this window, and nothing in this window calls any action methods in our controller. We’ll go through all the bindings piece by piece, but first, Figure 15-2 shows a view of the complete window, as seen in Interface Builder, with all objects selected.

Figure 15-2. All our window components, highlighted for visibility

Note that between the table view and the large text view there’s a small “dimple.” This is actually the draggable control of an NSSplitView, which lets you stack two views vertically or horizontally, and resize both of them at once with a single drag. You’ve surely seen this used in Xcode and elsewhere, and later you’ll see how to set it up here.

For now, let’s start off creating this window one section at a time, hooking up all the bindings as we go. At the top, put a couple of labels, and place an NSImageView between them. You’ll find this class by searching the Library for NSImageView” or “image well,” but not “image view,” which actually gives you another class entirely, IKImageView. Make the label on the left appear in bold text by selecting it and pressing ⌘B, and stretch out the label on the right so that it extends almost to the right edge of the window. We want the file icon displayed by the image view to appear to float right over the window background, so use the Attributes Inspector to remove its border by selecting None from the Border popup. While you’re in there, change the Scaling popup to Proportionally Up or Down as well, so that whether the system gives us a tiny icon or a huge one, it will be scaled to fit the available space. Your GUI should look like Figure 15- 3 at this point.

Figure 15-3. GUI for displaying the selected file’s icon and path

Now it’s time to set up the bindings for the image view and the right-hand label. Select the image view, open the Bindings Inspector, and bind its Value to the app delegate using the fileIcon key path. Then select the label on the right, and bind its Value to the app delegate using the filePath key path.

The next portion of the window looks just like what we created above. In fact, the quickest way to add these objects is to select the three shown above by dragging a box over them, pressing ⌘D to duplicate the selected object, and then dragging the new ones down, lining them up above the others. Change the title of the new left-side label and adjust its position if you need to. Figure 15-4 shows what this looks like.

Figure 15-4. This GUI will let us see with which app the file will launch.

Reconfigure the new image view’s binding, connecting its Value to the app delegate with the opensAppIcon key path, and then the right-side label’s Value to the app delegate using the opensAppName key path.

Next, let’s move on to the table view. Drag one out from the Library, and resize it to match Figure 15-5.

Figure 15-5. Here we’ll display all the file’s attributes.

This table view is going to contain all the file attributes that the system gives us. Those attributes come to us in the form of a dictionary, which is a perfect use of Apple’s NSDictionaryController class. Using NSDictionaryController, we’ll be able to display all of the selected file’s attributes as key-value pairs in the table view, just by hooking them up with Cocoa Bindings.

Search for NSDictionaryController in the Library, and drag one to the main nib window. Rename it attrDict so that we can easily identify it when binding to it later on. Like the other included controller classes, the dictionary controller class is able to get its content via

Return Main Page Previous Page Next Page

®Online Book Reader