Online Book Reader

Home Category

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

By Root 1054 0
box, first click to select the box, then edit its Autosizing behavior by make sure that the red lines for its upper and right margins are solid, and all other red lines are dashed, just like you see in Figure 5-7.

Figure 5-7. These Autosizing settings should be applied to all the remaining boxes, making them “stick” to the upper-right corner of the window.

Do that for each of the remaining boxes, then choose File➤Simulate Interface. Resize the window, and you should see that all the views resize as planned.

For the rest of this book, we’re going to skip any further discussion of the autosizing features, and leave it up to you to determine the autosizing options for your layouts as you see fit.

Making Some New Connections


Before we get into the code for this app, there’s one last step that we need to do in Interface Builder: connect AppController’s new actions and outlets. Click on the AppController icon in the main nib window, then open the Connection Inspector (⌘5), and you’ll see that our two new outlets, villainsTableView and window, now appear in the list. Ctrl-drag from AppController to the window (either to its icon in the main nib window, or to the title bar of the actual window), release the button, and select window.

Then Ctrl-drag from AppController to the table view, release the button, and select villainsTableView.

Now it’s time to connect a couple of outlets from the table view to our AppController. These two outlets let us specify which object will be able to respond to NSTableView’s delegate methods (called, for example, when the table view’s selection changes) and its dataSource methods (used to populate the table with data). In our case, we are using AppController to handle both of these duties. Ctrl-drag from the table view to the AppController and click on dataSource, then repeat the drag and click delegate. If dragging from the table view to AppController doesn’t bring up a menu of outlets to choose from, you’re probably dragging from the NSScrollView instead. Click it again to select the table view inside the scroll view, and try again.

The last step is to connect the new “add” and “remove” buttons to the relevant action methods in AppController. First ctrl-drag from the “add” button to the AppController icon and select the newVillain: action, then ctrl-drag from the “remove” button to the AppController icon and select the deleteVillain: action.

With those connections out of the way, our GUI layout is done for now, and we can get back into writing the code.

Making Way for the Table View: Code Edition


With the groundwork we already laid in Chapter 4, adding support for an array of villains is surprisingly simple. Basically, we have to create an array, tell the table view when we want it to display its content (whenever its content has changed), implement a few dataSource methods to give the table view its content, and implement a delegate method that will be called when the table view’s selection changes (so that we can update all the other views to match the selected villain). Then we’ll add a pair of methods to add and delete villains from the array, and we’ll be done!

Let’s start off by initializing an array to hold our villains, and telling the table view that it should load its content. We do this by adding just a few lines to applicationDidFinishLaunching:, as seen in bold in the following listing. First we create an array to hold all villains (which initially contains just the first villain we created in code earlier) and assign it to our villains instance variable. Then we tell the table view to load its content, and then to select its first row.

You’ll also need to add the [villainsTableView reloadData] call to the end of each of the takeName:, takeLastSeenDate:, and takeMugshot: methods, so that when the user edits the controls for those attributes, the table view will be updated accordingly. Just copy that line and paste it into the end of each of those methods. It may seem like overkill to call a method that looks like it’s going to reload the entire

Return Main Page Previous Page Next Page

®Online Book Reader