Learn Objective-C on the Mac - Mark Dalrymple [29]
■ NSImageView: Users can easily bring an image into your application by dragging any image from the Finder or another application and dropping it onto one of these.
■ NSTextView: This is nearly a complete text editor rolled into a single object, letting the user edit text with multiple fonts, formatting, rulers, and more.
NOTE: Throughout this book, we will sometimes use Cocoa class names (e.g. “NSButton”) and other times their casual, common names (e.g. “button”). Normally we’ll use the class names when discussing the code, and their casual names for most other situations, but in both cases we’re really talking about the same things.
Figure 4-1. A sampling of Cocoa UI elements
We’ll use these classes to create a simple application that pretends to be a sort of database application. The user will be able to edit and create some objects by using some simple GUI controls, but the results of the editing won’t be saved in any way (that’s where the pretending comes in). In later chapters, you’ll learn how to use Core Data to save your objects to disk, but for now, we’re going to focus on the GUI.
Creating the VillainTracker Application
The application we’ll be creating in this chapter is called VillainTracker. It’s a simple application for keeping track of super-villains, their last known whereabouts, special abilities, and so on. This is the sort of application that any superhero team worth its salt will have installed on their computer systems, and is therefore an obvious first app for a hero such as yourself. We’ll split the development of VillainTracker into two iterations: in the first iteration, we’ll create a simple app that enables us to edit information for a single villain, and in the second, in Chapter 5, we’ll add the ability to manage a whole list of villains. This will demonstrate the basics of how developers have been creating applications based on the Model-View-Controller (MVC) design pattern for years. Whether you have experience with this pattern or it’s new to you, it’s important that you get a sense of how MVC is typically implemented in Cocoa applications, because it’s a really a cornerstone of good application design in Cocoa.
We’ll start by creating a new application project in Xcode. We’ll lay the foundations for our user interface by creating some “stub” code in a controller class, consisting mainly of outlets and actions declared in a header file, so that we have a controller object ready to connect to some views. Then we’ll switch over to Interface Builder to create the GUI and connect it to the controller. Then we’ll go back to Xcode, where you’ll learn how to write the code to set and retrieve values the values shown in your GUI controls, one class at a time, so you’ll start to get a feel for how each of them works.
NOTE: Sometimes, when discussing GUI issues, you’ll see that we use the terms “views” and “controls” almost interchangeably. Most of the Cocoa user interface classes are, technically, both views and controls, at least in the Cocoa sense; NSView is a class that allows drawing on the screen, and NSControl is a subclass of NSView that extends it by responding to user events, triggering an action in a target, and so on. So, a large part of the view layer in your application will consist of objects that are “controls,” which of course you don’t want to confuse with the controller layer. If this seems puzzling, just try to remember that a “control” is a particular kind of view, whereas a “controller” is an object in the controller layer that coordinates between view and model objects. After a while, it will all make perfect sense, we promise.
So, let’s create a new application project. Launch Xcode, or switch to it if it’s already running, and select File➤New Project from the menu. You’ll now see the New Project assistant; under Mac OS X on the left, click Application, and then in the large pane on the right, click Cocoa Application, then click the Choose... button.
You’ll now see a standard