Learn Objective-C on the Mac - Mark Dalrymple [87]
Now you’ll have a fresh new GUI in your window, very similar to what was created for the MythicalPerson entity in Chapter 7. This window is going to be focused on the bands, and even has Mythical Bands as its title, so let’s lose the box surrounding all the stuff that was created for us: Click on the box title (which is also Mythical Bands) and select Layout➤Unembed Objects from the menu. Then select the window by clicking its title bar, and select Layout➤Size To Fit from the menu, which shrinks the window to make it precisely fit the content, and finally delete the unnecessary Fetch button. The resulting window should now look like Figure 8-8.
Figure 8-8. The beginnings of the Mythical Band window
Now save your changes, switch to Xcode, and Build & Run the app. You’ll see that the new window appears, and that you can add some bands, edit their names directly in the table view, and save your changes.
Giving Useful Names to Array Controllers
So, you now have two windows, each focused on a single entity, but so far have no way of connecting the two together. We’re going to address this in a moment, but before we do, there’s a change you should make in your nib file that will be helpful when configuring the new bindings we’re about to create.
Each time we’ve created a piece of the GUI by dragging in an entity from Xcode’s data modeler, the “assistant” creates an NSArrayController whose visible name (the name that appears within Interface Builder) defaults to something like Figure 8-9. Lengthy names for our array controllers make it harder to see what we’re dealing with. This is the Value binding for one of the sliders in our GUI. You can improve this situation by changing the names of your controllers. Changing the names won’t modify any of the bindings you’ve configured, or change anything for your application’s users. The only one who’ll see the change at all is you, but remember that you’re important, too. Just as it’s important to format your source code in such a way that it’s easy for you to scan through it and find what you need, doing what you can to streamline your editing experience in Interface Builder will save you time and frustration. So, go to the main nib window, make sure you’re looking at the icon view (the left-most icon in the row of small icons at the upper left should be selected), and find your Mythical Person Array Controller. Click on its name, and change it to just Persons. Do the same for Mythical Band Array Controller, changing its name to Bands. Doing this will eliminate a lot of words (e.g. “Mythical,” “Array,” and “Controller”) that are otherwise spread a bit too liberally throughout your nib file, and leave you with a display that’s hopefully easier for your eyes and brain to scan and comprehend quickly (see Figure 8-10). Figure 8-10. Renaming our array controllers lets us see the information we need in a slightly more compact way. This is the same binding that was shown in Figure 8-9, after changing the array controller’s name. Putting People in Bands
Now let’s make it possible to attach a person to a band by adding a simple popup button to the MythicalPerson display, which will let you pick a MythicalBand for the selected MythicalPerson. The popup button will be connected through Cocoa Bindings, which means that besides always being automatically updated to show the correct value for the selected person (just like the other controls do), it will also be automatically