Online Book Reader

Home Category

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

By Root 1027 0
updated to always show the current list of bands, changing its content automatically as the user makes changes in the Mythical Bands window. This will be done by configuring a few bindings on the popup button, using both the Persons and Bands controllers.

Let’s start off by making a change to our original window, so that it better matches the new one. Open up the original window (the one with MythBase in the title), select the box (entitled Mythical Person) that contains all the window’s controls and views, and then select Layout➤Unembed Objects from the menu, just like we did for the Mythical Bands window. Then select the window by clicking its title, and use the Attributes Inspector to rename it Mythical People. This way, the two windows have the same general appearance. Each is centered around a particular entity, but neither of them has any distinction as the “main window.”

Now, select all the views in the upper part of the window (all the sliders and their labels, and everything above them) and drag them up a bit, then select everything below the sliders and drag it all down a bit. In the space between, drag out a label (name it Member of Band:) and a popup button from the library. Line it all up so it looks roughly like Figure 8-11.

Figure 8-11. The new and improved Mythical People window

All that’s left now is to configure some bindings for the popup button. Unlike a text field or a slider, the popup button’s display needs several bindings in order to make it behave in a useful way. Consider that a popup button needs to have an array of strings to display (we’re going to display all available band names here), an array of underlying objects that those strings belong to (the bands themselves), and something to indicate which item in the list is selected (through the selected person’s band relationship). The first two of those will be bound via the Bands controller, through which we can easily bind to an array of all bands, or all band names. The third will be bound via the Persons controller.

Start by selecting the popup button, and opening the Bindings Inspector. Open the configuration for the Content binding at the top of the list, select Bands in the popup list, arrangedObjects for the Controller Key, and click the checkbox. This tells the popup button where to find the list of underlying objects.

Next, open the configuration for the Content Values binding. Once again select Bands in the popup list, arrangedObjects for the Controller Key, and this time pick name for the Model Key Path. This tells the popup button that it should populate its displayed values by grabbing the same array (arrangedObjects) and using the result of asking each for its name.

Finally, open the Selected Object binding configuration. Here, you should choose Persons from the popup list, selection for the Controller Key, and band for the Model Key Path. The popup list will automatically connect things so that when the Persons controller’s selection changes, the popup will notice the new selection’s band, look that up in the array of Content (which it picks up from the Bands controller), and display the corresponding string from the Content Values array (also from the Bands controller). Verify that this works by saving your changes, hitting Build & Run in Xcode, and trying it out. You should see that for each MythicalPerson you pick, you can select a MythicalBand to attach them to (see Figure 8-12).

Figure 8-12. Setting Hercules as a member of the band Grecian Formula

Showing the Bands’ Members


Now that we can add people to a band, how about being able to see a list of all the members in a band? Let’s add that to the Mythical Bands window. We won’t use any sort of assistant for this, instead we’ll manually add a table view and an array controller and hook it all up.

Start by grabbing an NSArrayController from the Library, and dragging it to your main nib window. Just like we’ve done with the other array controllers, we want to give this controller a name that is short but meaningful. This controller is going to handle

Return Main Page Previous Page Next Page

®Online Book Reader