Learn Objective-C on the Mac - Mark Dalrymple [72]
Now you’ll be presented with the New Core Data Entity Interface assistant (see Figure 7- 5). This assistant lets you choose from a few different GUI templates, each with some options of its own. Select Master/Detail View from the popup list, and click to enable each of the Search Field, Details Fields, and Add/Remove checkboxes.
Figure 7-5. Making choices in the New Core Data Entity Interface assistant
Then click Next, and the assistant will ask you which attributes should be included in the GUI. We want to show all of the entity’s attributes in our GUI, so leave them all checked and click Finish. Now the assistant will automatically create an NSArrayController in your nib file, and add several views to the window where you dragged the entity. The new NSArrayController will be pre-configured using Cocoa Bindings to retrieve its content through the Core Data entity you dragged, and the controls in the window will be configured with Cocoa Bindings to access their values through the NSArrayController. Figure 7-6 shows the new content of your window.
Figure 7-6. The automatic GUI, created entirely by the assistant
At this point, you can save the nib file, switch back to Xcode, and compile and run your app. You’ll see the GUI that the assistant created for you, now connecting to real data storage (a file accessed via Core Data, which we’ll explain in more detail later in this chapter), and almost fully functional. You can add new mythical people, set their name, details, power, divinity, and goodness values. You can save the objects by selecting File➤Save from the menu (or by pressing ⌘S), and you can delete them, edit them, and search for them right in the window’s search field. Note that the numeric values honor the limits you configured in the data model file. If you try to save an object whose divinity exceeds 100, for example, the app will show you a warning panel and the save will be cancelled. The one part of the app that doesn’t actually work is the depiction attribute. Trying to set a value for the depiction attribute in either the table column or the text field won’t actually work until we configure things a bit more in the next section.
MythBase is obviously not very polished at this point. There’s a lot that needs fixing, but as you can see it’s pretty functional already, and we haven’t even written a line of code, or touched a single control in Interface Builder.
Refining the GUI
Now it’s time for us to fix some of the obvious problems with the GUI, and also satisfy the urge to dig deeper that any programmer has to feel (don’t tell me you didn’t) when too many things start to work “magically” with just a few button clicks.
Go back to Interface Builder, and bring up the Bindings Inspector (⌘4). While you’re editing the GUI, it can be instructive to keep an eye on this Inspector, which will show you the bindings for each object you click on. Most of the bindings you’ll see here are similar to the bindings you configured in Chapter 6; the one that differs the most is the binding for the Mythical Person Controller in the main nib window, through which the controller accesses model objects. In DungeonThing, the array controllers we created always had their Content Array bound to a property of our controller. If you click on the Mythical Person Controller, you’ll see that here, instead of the Content Array, the array controller’s Managed Object Context is bound to a property of our app’s controller called managedObjectContext. The