Online Book Reader

Home Category

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

By Root 885 0
work, Build & Run in Xcode, and verify that you can add venues and edit their names.

Adding a Gig List to the Band Window


The final piece of functionality we need to add is the ability to create MythicalGigs. MythicalGigs sit between MythicalBands and MythicalVenues, each of which may have multiple gigs. Gigs, meanwhile, each have exactly one band and one venue. We’ll add another table view to contain the needed functionality, placed beside the members table view in the Mythical Band window. Each row of the table view will contain a text field for displaying and entering the gig’s performanceDate, and a popup button for selecting the venue (see Figure 8-14).

Figure 8-14. This is what the final Mythical Bands window will look like.

Start by finding an NSArrayController in the Library, dragging it to the main nib window, and naming it Gigs. Open the Attributes Inspector, set Mode to Entity and enter MythicalGigs for the Entity Name, then click to turn on the Prepares Content checkbox.

Now switch to the Bindings Inspector. Bind the new array controller’s Managed Object Context to MythBase_AppDelegate’s managedObjectContext, and bind its Content Set through Bands, with Controller Key set to selection, and Model Key Path set to gigs.

Next, we’re going to set up the GUI itself. Drag another table view from the Library to the Mythical Bands window, along with a Gigs label, and lay them out as shown in Figure 8- 14. This table view is going to display one column of dates, and one column of popup lists showing the selected venue for each gig. Let’s start with the left column. First, change its title to Date, then select the column itself (below the header) and bring up the Bindings Inspector again. Bind the column’s Value through the Gigs controller, with Controller Key set to arrangedObjects and Model Key Path set to performanceDate.

Now, this column contains dates, so it would be nice to use a date picker here, like we used in Chapter 4, but unfortunately the NSDatePickerCell isn’t quite up to the task (it’s not really meant for use in a table view). Fortunately, we can get some satisfaction here by using an NSDateFormatter. Find one of those in the Library, and drag it out over the Date column you’re working on (in fact, you need to drag it precisely over the NSTextFieldCell at the top of the column). The date formatter will do the work of converting between dates and strings for you, but we want to configure it a bit. Because we’re dealing with mythological events, some of the dates are BC-era, and by default the date formatter doesn’t show BC or AD. To change this, click on the text field cell in the column until you see a small bronze icon appear just below it. That icon represents the formatter attached to the column. Click on it, and then open the Attributes Inspector.

At the top of the Inspector, click on the Behavior popup and change it to Mac OS X 10.4+ Custom, which will allow us to define the formatting. The format is shown as a row of pale blue “pills,” each containing an element of a date (month, year, and so on), and below that are shown more date elements that can also be shown. Drag the Era box out to the end of the format, and that column is done.

The final column is for specifying the venue, and here we’ll configure a popup button, similar to what we did for picking a band in the Mythical Person window. First set the column’s title to Venue, then find an NSPopupButtonCell in the Library and drag it out over the column. You’ll now need to configure three bindings for the column. First, bind Content to the Venues controller’s arrangedObjects. Next, bind ContentValues to the Venues controller’s arrangedObject, this time specifying name for the Model Key Path. And last but not least, bind the Selected Object to the Gigs controller’s arrangedObjects, using venue as the Model Key Path.

The final step is setting up Add and Remove buttons, so the user can create and delete gigs. Duplicate the two buttons in the upper part of the window, and drag the new ones down below the table view of gigs.

Return Main Page Previous Page Next Page

®Online Book Reader