Learn Objective-C on the Mac - Mark Dalrymple [62]
Next, we’re going to change a single line at the end of each action method. Instead of putting the summary text created by the method directly into a text field, we’ll add each summary to an array, using NSArrayController to do this so that all dependent views (any views that have bindings through the same NSArrayController) will be automatically updated as well. Rather than inserting the bare string into the array, we’re creating an NSMutableDictionary containing the created object and the current time, using the dictionary as the simplest sort of model object, containing two keyed values. Implement this change at the end of createCharacter: by changing this:
There you have it, all the code changes we need to make! Note that unlike our example in Chapter 5, the code here doesn’t have to do anything at all regarding the table views. No delegate or dataSource methods to implement, no outlets pointing to the table views, nothing. Thanks to bindings and NSArrayController, the table views will really take care of themselves.
Show History in Tables
Now it’s time to go back to Interface Builder, create some table views, and set up their bindings to the new arrays we added to DungeonThingAppDelegate. We’ll also configure the existing text fields to get their data through bindings. Switch over to Interface Builder, or if you’ve closed your nib file, open it again by double-clicking it in Xcode’s navigation pane.
Dealing With Inconsistencies in Nib Files
The first time you bring this nib file to the front after making the changes described in the last section, you may notice a small yellow triangle at the bottom of the main nib window. This warning triangle turns up whenever inconsistencies arise that Interface Builder can’t resolve without your help. Click the warning triangle to show what the problems are. In this case, Interface Builder has noticed that its internal idea about DungeonThingAppDelegate no longer matches the reality described in the header file: The nib file contains connections for outlets that no longer exist! To resolve these problems, select DungeonThingAppDelegate in the main nib window, then bring up the Connections Inspector (⌘5). At the top of the Inspector, you’ll see all the current outlet connections, including the ones for the missing outlets (each of them marked with an exclamation point). Eliminate the bad connections by clicking the small “X” control on each of them. Figure 6-11 shows all the parts you’ll see while fixing this.
Figure 6-11. The main nib window shows a warning triangle after you remove outlets from a class. Clicking the triangle reveals details about the problems, and the Connections Inspector lets you remove the obsolete connections.
Configuring the Table Views and Text Views
Now it’s time to get to work on the main application window. Start off by making the window larger. We’re going to create three “history” table views, one below each of the existing sets of views, so we need to make the window taller (but we can keep the current width). Use the window’s resize control in the lower right to make the window about twice its current height. The blue guidelines will help you maintain the current width.
In the Library window, search for “table” and drag the resulting table view out into the window. Position it so that its upper left corner is just below the lower left corner of the left-most box, then grab the table view’s lower right resize handle, and drag it down until the table fills most of the available space down to the bottom of the window, with its right edge lined up with the right edge of the box above it. Figure 6-12 shows you the idea.
Figure 6-12. We’ve begun adding tables to the window.
In our history tables, we’re only going to display the time that the object was created. The user can then click on a row to see the relevant object displayed in the text field above