Mac OS X Reference Library Apple Developer
Search

Expanding on the Basics

Cocoa was designed to make application development simple and to take a lot of the uninteresting, repetitive work out of application development. Many common features of applications that users may take for granted are quite difficult to program. Cocoa implements a large selection of these features so you can spend your time on what makes your application special, instead of implementing baseline features. This chapter describes those integrated components of Cocoa. You may be surprised how many classes and features come packaged with Cocoa to maximize your productivity.

For Free with Cocoa

The simplest Cocoa application, without a line of code added to it, includes a wealth of features you get “for free.” You do not have to program these features yourself. You can see this when you test an interface in Interface Builder.

Application and Window Behavior

In the Interface Builder test mode, Currency Converter behaves almost like any other application on the screen. Click elsewhere on the screen, and Currency Converter is deactivated, becoming totally or partially obscured by the windows of other applications.

If you closed your application, run it again. Once the Currency Converter window is open, move it around by its title bar. Here are some other tests you can do:

  1. Open the Edit menu. Its items appear and then disappear when you release the mouse button, as with any application menu.

  2. Click the miniaturize button. Click the window’s icon in the Dock to get the application back.

  3. Click the close button; the Currency Converter window disappears.

If you hadn’t configured the Currency Converter window in Interface Builder to remove the resize box, you could resize it now. You could also have set the autoresizing attributes of the window and its views so that the window’s elements would resize proportionally to the resized window or would retain their initial size (see Interface Builder Help for details on autoresizing).

Controls and Text

The buttons and text fields of Currency Converter come with many built-in behaviors. Notice that the Convert button pulsates (as is the default for buttons associated with the Return key). Click the Convert button. Notice how the button is highlighted for a moment.

If you had buttons of a different style, they would also respond in characteristic ways to mouse clicks.

Now click in one of the text fields. See how the insertion point blinks in place. Type some text and select it. Use the commands in the Edit menu to copy it and paste it in the other text field.

Do you recall the nextKeyView connections you made between the Currency Converter text fields? Insert the cursor in a text field, press the Tab key and watch the cursor jump from field to field.

Menu Commands

Interface Builder gives every new application a default menu that includes the application, File, Edit, Window, and Help menus. Some of these menus, such as Edit, contain ready-made sets of commands. For example, with the Services submenu (whose items are added by other applications at runtime) you can communicate with other Mac OS X applications. You can manage your application’s windows with the Window menu.

Currency Converter needs only a few commands: the Quit and Hide commands and the Edit menu’s Copy, Cut, and Paste commands. You can delete the unwanted commands if you wish. However, you could also add new ones and get “free” behavior. An application designed in Interface Builder can acquire extra functionality with the simple addition of a menu or menu command, without the need for compilation. For example:

Document Management

Many applications create and manage repeatable, semi-autonomous objects called documents. Documents contain discrete sets of information and support the entry and maintenance of that information. A word-processing document is a typical example. The application coordinates with the user and communicates with its documents to create, open, save, close, and otherwise manage them. You could also save your Currency Converters as documents, with a little extra code.

See Document-Based Applications Overview in Cocoa Design Guidelines Documentation for more information.

File Management

An application can use the Open dialog, which is created and managed by the Application Kit framework, to help the user locate files in the file system and open them. It can also use the Save dialog to save information in files. Cocoa also provides classes for managing files in the file system (creating, comparing, copying, moving, and so forth) and for managing user defaults.

Communicating with Other Applications

Cocoa gives an application several ways to exchange information with other applications:

Custom Drawing and Animation

Cocoa lets you create your own custom views that draw their own content and respond to user actions. To assist you in this, Cocoa provides objects and functions for drawing, such as the NSBezierPath class.

Internationalization

Cocoa provides API and tool support for internationalizing the strings, images, sounds, and nib files that are part of an application. Internationalization allows you to easily localize your application to multiple languages and locales without significant overhead.

Editing Support

You can get several panels (and associated functionality) when you add certain menus to your application’s menu bar in Interface Builder. These “add-ons” include the Font window (and font management), the color picker (and color management), the text ruler, and the tabbing and indentation capabilities the Text menu brings with it.

Formatter classes enable your application to format numbers, dates, and other types of field values. Support for validating the contents of fields is also available.

Printing

With just a simple Interface Builder procedure, Cocoa automates simple printing of views that contain text or graphics. When a user executes the Print command, an appropriate dialog helps to configure the print process. The output is WYSIWYG (what you see is what you get).

Several Application Kit classes give you greater control over the printing of documents and forms, including features such as pagination and page orientation.

Help

You can very easily create context-sensitive help—known as “help tags”—for your application using the Interface Builder inspector. After you’ve entered the help tag text for the user interface elements in your application, a small window containing concise information on the element appears when the user places the pointer over these elements.

Plug-in Architecture

You can design your application so that users can incorporate new modules later on. For example, a drawing program could have a tools palette: pencil, brush, eraser, and so on. You could create a new tool and have users install it. When the application is next started, this tool appears in the palette.

Turbo Coding with Xcode

When you write code with Xcode you have a set of “workbench” tools at your disposal. A few of these tools are described next.

Project Find

Project Find (available from the Find window in Xcode) allows you to search both your project’s code and the system headers for identifiers. Project Find uses a project index that stores all of a project’s identifiers (classes, methods, globals, and so forth) on disk.

For C-based languages, Xcode automatically gathers indexing information while the source files are being compiled; therefore, it is not necessary to build the project to create the index before you can use Project Find.

Code Sense and Code Completion

Code Sense indexes your project files to provide quick access to the symbols in your code and the frameworks linked by your project. Code Completion uses this indexing to automatically suggest matching symbols as you type. These features can be turned on in the Code Sense preferences pane in the Xcode Preferences window.

Since Code Sense and Code Completion use Xcode’s speedy indexing system, the suggestions they provide appear instantaneously as you type. If you see an ellipsis (...) following your cursor, Xcode could not find an exact match.

Integrated Documentation Viewing

Xcode supports viewing HTML-based ADC Reference Library content directly in the application. You can access reference material about the Xcode application, other developer tools, Carbon, Cocoa, AppleScript Studio, and even access UNIX man pages.

Additionally, you can jump directly from fully or partially completed identifiers in your code to reference information and header files. To retrieve the reference information for an identifier, Option–double-click it; to retrieve its declaration in a header file, Command–double-click it.

The search bar in the Developer Documentation window also offers you a quick and easy way to find an identifier in any of Cocoa’s programming interfaces.

Indentation

In the Indentation preferences pane in the Xcode Preferences window you can set the characters at which indentation automatically occurs, the number of spaces per indentation, and other global indentation characteristics. The Format menu also offers commands to indent code blocks individually.

Delimiter Checking

Double-click a brace (left or right, it doesn’t matter) to locate the matching brace; the code between the braces is highlighted. In a similar fashion, double-click a square bracket in a message expression to locate the matching bracket, and double-click a parenthesis character to highlight the code enclosed by the parentheses. If there is no matching delimiter, Xcode emits a beep.

Emacs Bindings

You can use the most common Emacs commands in the Xcode code editor. (Emacs is a popular editor for writing code.) For example, there are the commands page-forward (Control-v), word-forward (Meta-f), delete-word (Meta-d), kill-forward (Control-k), and yank from kill ring (Control-y).

Some Emacs commands may conflict with some of the standard Macintosh key bindings. You can modify the key bindings the code editor uses in the Key Bindings preferences pane in Xcode Preferences to substitute other “command” keys—such as the Option key or Shift-Control—for Emacs Control or Meta keys. For information on key bindings, see About Key Bindings in Text Input Management.




Last updated: 2009-08-03

Did this document help you? Yes It's good, but... Not helpful...