Home > Articles > Programming > General Programming/Other Languages

A Cocoa Application: Views

  • Print
  • + Share This
This chapter introduces Interface Builder, a tool no less important to Cocoa development than Xcode itself. IB is used as a straightforward tool for laying out windows and views. You'll see how to set the many options for automatic sizing of embedded views and how to use Interface Builder's own simulation mode to verify that your layout and sizing choices work.
This chapter is from the book

Now that the model is taken care of, we turn to the other end of the application: the views. Our design for the application relies on standard elements in the Mac OS X Aqua interface.

Interface Builder (IB) is the indispensible tool for laying out human interfaces for Mac OS X applications. It edits Nib files, which are archives of human-interface objects that are reconstituted by your application when the files are loaded. All Cocoa application have a main Nib file, which the Xcode application template names Main-Menu.nib. This file contains at least the main menu bar for the application and may contain other applicationwide windows and views.

An application can have more than one Nib, and a Nib can be loaded and its contents instantiated more than once. For instance, the Xcode template for a document-based Cocoa application includes a MyDocument.nib file. The file contains a trivial window for documents of the MyDocument class, and the template for the MyDocument class implementation specifies MyDocument.nib as the Nib to load when creating a new document.

Because our design calls for a window that displays the data points and regression statistics for each document, we want to edit the window in MyDocument.nib to match our design. In the Groups & Files list, click the triangle next to Resources to open that group. You should see MyDocument.nib, with the gold icon of a Nib file, in that group. Double–clicking this item launches Interface Builder and opens MyDocument.nib for editing.

6.1 Interface Builder

The newly opened Interface Builder will show you three windows ( Figure 6.1). (Use Hide Others in the Interface Builder application menu to reduce the clutter.) The largest, named Window, contains a text element saying "Your document contents here" in the middle. Close this window.

06fig01.jpg

Figure 6.1 Interface Builder on opening MyDocument.nib. The window that represents the Nib is at lower left; above it is the simple window, with some filler text, that comes in MyDocument.nib as provided in the template. To the right is the palette containing standard Aqua controls that can be dragged into windows.

The window at the lower left, MyDocument.nib, shows the Nib file as a file opened by Interface Builder (Figure 6.2). It shows three icons. The first two—File's Owner and First Responder—are placeholders for certain objects outside the Nib. Any other icons are for objects in the Nib—in this case, a window named Window.

06fig02.jpg

Figure 6.2 The window for the MyDocument.nib file contains icons for File's Owner and First Responder, which are placeholders for objects outside the Nib, along with an icon for every top-level object in the Nib. In this case, MyDocument.nib contains only a window, named Window.

Double–click the Window icon. The window we're building for the MyDocument class opens again.

The third window, to the right, is a utility window embodying a series of palettes containing objects you can add to a Nib file. We'll be using this window a lot.

  • + Share This
  • 🔖 Save To Your Account