oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Build Your First Cocoa App
Pages: 1, 2, 3, 4

Also in Programming With Cocoa:

Understanding the NSTableView Class

Inside StYNCies, Part 2

Inside StYNCies

Build an eDoc Reader for Your iPod, Part 3

Build an eDoc Reader for your iPod, Part 2

Creating the user interface

Going back to the Groups and Files list in Project Builder, open the group labeled Resources by clicking on the arrow to the left of it. Contained within this group are your application NIB interface files. All Cocoa-Document based applications have two of these initially, MainMenu.nib and MyDocument.nib. MainMenu.nib is the application-wide NIB file, and MyDocument.nib is the document-specific NIB file.

Double click MainMenu.nib to open it in Interface Builder. In Interface Builder, you will see three windows. One is a your application's menu bar (the one that is in a floating window); another has several tabbed views that display all the objects archived by your NIB file. The third window contains what are known as palettes.

Palettes in Interface Builder are repositories of prototype object instances of the most commonly used Application Kit classes, grouped according by common function. It's possible to create your own palettes with custom controllers. The palettes used in interface builder are an excellent example of Cocoa's plug-in architecture, which we have spoken little about. Plug-ins that allow a user to add some custom functionality to an application are just one of the freebies in Cocoa.

Let's start by modifying out application's menu bar a bit. There is a palette that contains several standard, pre-built menus named Cocoa-Menus Palette. To add a new menu to the MainMenu, drag one of the prototype menus from the palette and position it where you like on your application's main menu. Menus can also be rearranged by dragging, and you can remove a menu by highlighting it and then hitting the delete key.

Screenshot of adding a new menu.
Adding a new menu to the MainMenu.

Save the NIB file in Interface Builder, close it, and return to Project Builder.

Now open MyDocument.nib, and in Interface Builder, you will be presented with the same setup as before, except now there is a blank window rather than the floating MainMenu window. This window is the empty canvas for you to create your document's interface.

What you build here is what your document window's interface will look like at runtime. We're going to keep our document window simple with just a single interface component known as an NSTextView, or more simply, a text view. The NSTextView class is a front-end to and subclass of another class that deals with text, NSText. NSText defines the fundamental functionality that Cocoa has for handling and working with large bodies of text; NSTextView manages the way the user interacts with text data.

For example, NSText let's you do things like programmatically control cutting and pasting, text selection. alignment, and attributes of the text-like color and font. NSTextView, on the other hand, takes the primitive methods defined by NSText and create a more intuitive and rich interface to NSText's behaviors. I highly recommend that at some point to take the time and read the class references for the two classes. You can find them online at Application Kit Objective-C Reference, or through the Help Viewer by following the links to Cocoa Developer Documentation.

Typically, NSTextViews are created within an a scroll window, which is represented by the NSScrollView class. What this does is give your text window the capability to scroll through large blocks of text that can't all fit in the window. Interface Builder puts these two classes together by default, so don't worry too much about NSScrollView right now; I just wanted you to be aware of it.

The prototype NSTextView object is found in the Cocoa-Data Views palette. The NSTextView is the largest of the three objects in this palette, and is nothing more than a blank space.

Drag the NSTextView object from the palette onto the window; anywhere will do. Once you place the NSTextView object into your main window, you can position it and resize it to your needs. The newest version of Interface Builder bundled with Mac OS X 10.0 adds a nifty feature where guidelines are displayed when object edges and corners come near sweetspots in the interface. These sweetspots correspond to object spacings and alignments that are standard in Aqua. This makes the task of creating consistent and well-designed user interfaces more manageable.

Pages: 1, 2, 3, 4

Next Pagearrow