<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/css" href="http://commons.oreilly.com/wiki/skins/common/feed.css?97"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://commons.oreilly.com/wiki/index.php?title=Learning_Cocoa_with_Objective-C/Single-Window_Applications/Graphical_User_Interfaces&amp;action=history&amp;feed=atom</id>
		<title>Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces - Revision history</title>
		<link rel="self" type="application/atom+xml" href="http://commons.oreilly.com/wiki/index.php?title=Learning_Cocoa_with_Objective-C/Single-Window_Applications/Graphical_User_Interfaces&amp;action=history&amp;feed=atom"/>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=Learning_Cocoa_with_Objective-C/Single-Window_Applications/Graphical_User_Interfaces&amp;action=history"/>
		<updated>2013-05-21T15:05:47Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.11.0</generator>

	<entry>
		<id>http://commons.oreilly.com/wiki/index.php?title=Learning_Cocoa_with_Objective-C/Single-Window_Applications/Graphical_User_Interfaces&amp;diff=6265&amp;oldid=prev</id>
		<title>Docbook2Wiki: Initial conversion from Docbook</title>
		<link rel="alternate" type="text/html" href="http://commons.oreilly.com/wiki/index.php?title=Learning_Cocoa_with_Objective-C/Single-Window_Applications/Graphical_User_Interfaces&amp;diff=6265&amp;oldid=prev"/>
				<updated>2008-03-07T12:56:06Z</updated>
		
		<summary type="html">&lt;p&gt;Initial conversion from Docbook&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Learning Cocoa with Objective-C/TOC}}&lt;br /&gt;
Now that we've covered the Foundation, we're going to take a step up and start working with the AppKit framework to create GUI-based applications. In this chapter, we'll build a single-window application from beginning to end, letting us introduce the various GUI subjects necessary to become proficient with Cocoa programming. For the first time, you'll see the complete workflow typical of Cocoa application development, composed of the following steps:&lt;br /&gt;
&lt;br /&gt;
# Design the application.&lt;br /&gt;
# Create the project using Project Builder.&lt;br /&gt;
# Create the interface using Interface Builder.&lt;br /&gt;
# Define the classes using Interface Builder.&lt;br /&gt;
# Connect the Model, View, and Controller objects using Interface Builder.&lt;br /&gt;
# Implement the classes using Project Builder.&lt;br /&gt;
# Build and run the project using Project Builder.&lt;br /&gt;
&lt;br /&gt;
The application we'll build in this chapter is a currency converter—a simple utility that converts a dollar amount to an amount in some other currency. This example has been one of the mainstay examples of NeXTSTEP/OpenStep/Cocoa programming; it's been around almost long enough to reach &amp;quot;Hello World&amp;quot; status. Although it is a simple application, it consolidates quite a few of the concepts and techniques needed to get started with writing Cocoa GUI applications.&lt;br /&gt;
&lt;br /&gt;
After working through this first complete GUI application, we'll spend the rest of this section of the book exploring in-depth the topics introduced in this chapter.&lt;br /&gt;
&lt;br /&gt;
== Graphical User Interfaces in Cocoa ==&lt;br /&gt;
&lt;br /&gt;
Graphical user interfaces in Cocoa are built on the following four concepts:&lt;br /&gt;
&lt;br /&gt;
* Windows&lt;br /&gt;
* Nib files&lt;br /&gt;
* Outlets&lt;br /&gt;
* Actions&lt;br /&gt;
&lt;br /&gt;
=== Windows ===&lt;br /&gt;
&lt;br /&gt;
A ''window'' in Cocoa looks similar to windows in other user environments, such as Microsoft Windows or earlier versions of the Mac OS. A window can be moved around the screen and stacked on top of other windows like pieces of paper. A typical Cocoa window, shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-1|Figure 5-1]], has a titlebar, content area, and several control objects.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-1&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-1. A typical Cocoa-based window'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt149.png|A typical Cocoa-based window]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Many user-interface objects other than the standard windows are window objects without the standard window widgets. These include menus, pop-up menus, dialog boxes, sheets, alerts, panels, info windows, tool tips, tool palettes, and scrolling lists. In fact, anything drawn on the screen must appear in a window. End users, however, may not recognize or refer to them as &amp;quot;windows.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Nib Files ===&lt;br /&gt;
&lt;br /&gt;
A ''nib file'' is an archive of object instances generated by Interface Builder. Unlike the product of many user interface-building systems, a nib file is not generated code. It is a set of true objects that have been encoded specially and stored on disk. The objects in the nib file are created and manipulated using Interface Builder's graphical tools.&lt;br /&gt;
&lt;br /&gt;
Nib files typically package a group of related user-interface objects and supporting resources, along with information about how the objects are related—both to one another and to other objects in your application. Nib files hold all of the objects they describe by specially archiving, or freeze-drying, so that they can be reconstituted in a running application and then used again.&lt;br /&gt;
&lt;br /&gt;
Every application with a graphical user interface has at least one nib file that is loaded automatically when the application is launched. The main nib file typically contains the application menu. Auxiliary nib files contain the application windows, as well as their associated user-interface objects. For example, an image-manipulation program such as Photoshop might have auxiliary nib files for the various palettes and controls that let you work with an image.&lt;br /&gt;
&lt;br /&gt;
It can be useful to think of the objects that compose a user interface, and are contained within a nib file, as forming a hierarchy. [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-2|Figure 5-2]] shows the ownership hierarchy of nib-based objects for [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-1|Figure 5-1]]. At the top of the nib file's hierarchy of archived objects is the ''file's owner'' object, a proxy object pointing to the actual object that owns, or controls, the nib file—typically the object that loaded the nib file from disk.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-2&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-2. Ownership hierarchy of nib-based objects for [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-1|Figure 5-1]]'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt150.png|Ownership hierarchy of nib-based objects for Figure 5-1&lt;br /&gt;
                  ]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Outlets ===&lt;br /&gt;
&lt;br /&gt;
An ''outlet'' is a special-instance variable, marked with the &amp;lt;tt&amp;gt;IBOutlet&amp;lt;/tt&amp;gt; keyword in a class's header, that contains a reference to another object, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-3|Figure 5-3]]. An object can communicate with other objects in an application by sending messages to them through outlets.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-3&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-3. Object connected to a text field via an outlet'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt151.png|Object connected to a text field via an outlet]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An outlet can reference any object in an application: user-interface objects, instances of custom classes, and even the application object itself. What distinguishes outlets from other instance variables is that Interface Builder recognizes the &amp;lt;tt&amp;gt;IBOutlet&amp;lt;/tt&amp;gt; keyword and lets you manipulate the connections it defines. These connections, once defined, will be linked up for you when your application runs. Specifying these relationships between objects in Interface Builder saves you from having to write initialization code by hand. There are ways other than outlets to reference objects in an application, but outlets and Interface Builder's facility for initializing them are a great convenience.&lt;br /&gt;
&lt;br /&gt;
=== Actions ===&lt;br /&gt;
&lt;br /&gt;
''Actions'' are special methods, indicated with the &amp;lt;tt&amp;gt;IBAction&amp;lt;/tt&amp;gt; keyword, which are defined by a class and triggered by user-interface objects. Interface Builder recognizes action declarations in a header file, as it does with outlets. Similarly, Interface Builder allows you to connect actions that a user might take with an interface, such as pushing a button, to methods on an object. These connections are shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-4|Figure 5-4]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-4&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-4. Targets and actions'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt152.png|Targets and actions]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An action refers both to a message sent to an object when the user clicks a button (or manipulates some other control), and to the invoked method.&lt;br /&gt;
&lt;br /&gt;
== Designing Applications Using MVC ==&lt;br /&gt;
&lt;br /&gt;
Cocoa applications make use of a long-standing object-oriented paradigm called ''Model-View-Controller'' (MVC). As illustrated in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-5|Figure 5-5]], MVC proposes three types of objects in an application—model, view, and controller:&lt;br /&gt;
&lt;br /&gt;
;Model&lt;br /&gt;
: Objects that hold data and define the logic that manipulates that data&lt;br /&gt;
;View&lt;br /&gt;
: Objects that represent something visible to the user, such as a window or a button&lt;br /&gt;
;Controller&lt;br /&gt;
: Objects that act as mediators between model and view objects&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-5&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-5. Model, view, and controller objects in an MVC design'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt153.png|Model, view, and controller objects in an MVC design]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The MVC paradigm works well for many applications, because the controller's central and mediating role frees the model objects from needing to know about the state and events of the user interface. Likewise, the view objects don't have to know about the programmatic interfaces of the model objects. Dividing the problem along these lines helps encapsulate the various objects in an application. This can also aid reuse, since the model could be used elsewhere, perhaps even on another platform.&lt;br /&gt;
&lt;br /&gt;
MVC, strictly observed, is not advisable in all circumstances. Sometimes it can be advantageous to combine roles. For example, in an graphics-intensive application, such as an arcade game, you might have several view objects that merge the roles of view and model for performance reasons. In other applications, especially simple ones, you can combine the roles of controller and model; these objects join the special data structures and logic of model objects with the controller's hooks to the interface.&lt;br /&gt;
&lt;br /&gt;
=== MVC in Currency Converter's Design ===&lt;br /&gt;
&lt;br /&gt;
The Currency Converter application will consist of two custom objects: a &amp;lt;tt&amp;gt;Converter&amp;lt;/tt&amp;gt; that will serve as our model and a &amp;lt;tt&amp;gt;Controller&amp;lt;/tt&amp;gt; that will mediate between the user interface and the &amp;lt;tt&amp;gt;Converter&amp;lt;/tt&amp;gt; object. We'll create the view of the application using a collection of AppKit objects, which we'll assemble using Interface Builder. The relationships between these objects are shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-6|Figure 5-6]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-6&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-6. MVC in Currency Converter's design'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt154.png|MVC in Currency Converter's design]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;Controller&amp;lt;/tt&amp;gt; object will assume the central role in the application. Like all controller objects, it communicates with the interface and model objects, and it handles tasks specific to the application. The &amp;lt;tt&amp;gt;Controller&amp;lt;/tt&amp;gt; object gets the values that users enter into fields, passes these values to the &amp;lt;tt&amp;gt;Converter&amp;lt;/tt&amp;gt; object, gets the result back from the &amp;lt;tt&amp;gt;Converter&amp;lt;/tt&amp;gt;, and puts this result into a field in the interface. By insulating the &amp;lt;tt&amp;gt;Converter&amp;lt;/tt&amp;gt; from the implementation-specific details of the user interface, the &amp;lt;tt&amp;gt;Converter&amp;lt;/tt&amp;gt; object becomes a reusable component for other applications.&lt;br /&gt;
&lt;br /&gt;
== Create the Currency Converter Project ==&lt;br /&gt;
&lt;br /&gt;
Now that we have designed the application, we can get to work on the implementation. In Project Builder, create a new Cocoa Application project (File → New Project → Application → Cocoa Application) as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-7|Figure 5-7]], then name the project &amp;quot;Currency Converter&amp;quot;, and save it in your ''~/LearningCocoa''folder.&lt;br /&gt;
&lt;br /&gt;
If you click on the disclosure triangle next to Other Sources in the left pane and click on ''main.m,''you'll notice that the file looks a bit different from the Foundation projects we've worked with in the past. The ''main.m''file contains the following code:&lt;br /&gt;
&lt;br /&gt;
 //&lt;br /&gt;
 //  main.m&lt;br /&gt;
 //  Currency Converter&lt;br /&gt;
 //&lt;br /&gt;
 //  Created by James Duncan Davidson on Fri Aug 30 2002.&lt;br /&gt;
 //  Copyright (c) 2002 __MyCompanyName__. All rights reserved.&lt;br /&gt;
 //&lt;br /&gt;
 &lt;br /&gt;
 #import &amp;lt;Cocoa/Cocoa.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 int main(int argc, const char *argv[])&lt;br /&gt;
 {&lt;br /&gt;
     return NSApplicationMain(argc, argv);&lt;br /&gt;
 }       &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-7&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-7. Creating a new Cocoa Application'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt156.png|Creating a new Cocoa Application]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that the &amp;lt;tt&amp;gt;import&amp;lt;/tt&amp;gt; statement has changed to importing ''Cocoa.h''instead of ''Foundation.h''. The ''Cocoa.h''header contains the definitions for both the Foundation and AppKit classes. Also notice that the main method makes a call to the &amp;lt;tt&amp;gt;NSApplicationMain&amp;lt;/tt&amp;gt; function. This function is defined by the AppKit and will start the application, load the main nib file, and set up the event loop and autorelease pool for that loop. Now that we have taken a look at this source file, we can let it be. You'll very rarely, if ever, modify the ''main.m'' file of a Cocoa GUI application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;tip&amp;quot;&amp;gt;&lt;br /&gt;
'''Tip'''&lt;br /&gt;
&lt;br /&gt;
Project Builder automatically generates the comments at the top of the source file from the Cocoa Application template. You'll probably want to change the &amp;lt;tt&amp;gt;__MyCompanyName__&amp;lt;/tt&amp;gt; text to the actual copyright holder and make sure that the copyright year is correct. See Chapter 17 for more details on how to finish and polish your applications.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Create the Interface ==&lt;br /&gt;
&lt;br /&gt;
The Currency Converter's interface is actually quite simple to create. It consists of a few text fields and a button. The process of creating it will give you an opportunity to explore how Interface Builder works. [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-8|Figure 5-8]] shows a hand-drawn sketch of how we'd like the interface to look. This gives us something to go by when designing the interface in Interface Builder.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-8&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-8. The Currency Converter interface'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt157.png|The Currency Converter interface]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Open the Main Nib File ===&lt;br /&gt;
&lt;br /&gt;
 Begin by creating an application's user interface in Interface Builder.&lt;br /&gt;
&lt;br /&gt;
# In Project Builder's left pane, click on the disclosure triangle next to Resources to reveal the ''MainMenu.nib'' file.&lt;br /&gt;
# Double-click on ''MainMenu.nib''to open it in Interface Builder.&lt;br /&gt;
&lt;br /&gt;
A default menu bar and window, titled Window, will appear when the nib file is opened.&lt;br /&gt;
&lt;br /&gt;
=== Resize the Window ===&lt;br /&gt;
&lt;br /&gt;
The window is a bit large for our purposes. You can change the size either by dragging the bottom-right corner of the window or by using the Info window, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-9|Figure 5-9]]. You can open this window by selecting Tools → Show Info from Interface Builder's menu (Shift-[[Image:Learning Cocoa with Objective-C_I_1_tt158.png|]] -I).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-9&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-9. Info window showing the Size panel'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt159.png|Info window showing the Size panel]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When you have opened the Info window, use the following process to resize the window:&lt;br /&gt;
&lt;br /&gt;
# Select Size from the Info window's pop-up menu.&lt;br /&gt;
# In the Content Rect area, select Width/Height from the right-hand pop-up menu.&lt;br /&gt;
# In the text fields under the Width/Height menu, type &amp;lt;tt&amp;gt;400&amp;lt;/tt&amp;gt; in the width (''w'') field and &amp;lt;tt&amp;gt;200&amp;lt;/tt&amp;gt; in the height (''h'') field, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-9|Figure 5-9]].&lt;br /&gt;
&lt;br /&gt;
=== Set the Window's Title and Attributes ===&lt;br /&gt;
&lt;br /&gt;
By default, our window has a title of Window. We want the application window to have a more meaningful title, as well as a few other attributes that we care about.&lt;br /&gt;
&lt;br /&gt;
# Select Attributes from the Info window's pop-up menu, and change the window's title to Currency Converter, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-10|Figure 5-10]].&lt;br /&gt;
# Verify that the Visible at launch time option is selected. This will ensure that this window is created on screen when the application is launched.&lt;br /&gt;
# Deselect the Resize checkbox in the Controls area. This will prevent users from resizing the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-10&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-10. Info window showing the Attributes panel'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt160.png|Info window showing the Attributes panel]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Place the Text Fields ===&lt;br /&gt;
&lt;br /&gt;
The Currency Converter will use text fields to accept user input and display converted values. To place a text field into the window:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Drag an &amp;lt;tt&amp;gt;NSTextField&amp;lt;/tt&amp;gt; object from the Views palette (shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-11|Figure 5-11]]), and place it in the upper-right corner of the application window.&lt;br /&gt;
&lt;br /&gt;
When you drag the text field onto the window, Interface Builder helps you place objects according to the Aqua Human Interface Guidelines (HIG) by displaying guidelines when an object is dragged close to the proper distance from neighboring objects or the edge of the window.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-11&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-11. Dragging an NSTextField from the Views palette'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt161.png|Dragging an NSTextField from the Views palette]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Resize the text field by grabbing a handle and dragging it in the direction in which you want it to grow. In this case, drag the left handle to the left to enlarge the text field, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-12|Figure 5-12]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-12&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-12. Resizing a text field'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt162.png|Resizing a text field]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Just as you can specify the size of the application window, you can also specify exact sizes for other elements of your application. For example, if you want the text field to be 150 pixels wide, select the &amp;lt;tt&amp;gt;NSTextField&amp;lt;/tt&amp;gt; object, and then select Size from the NSTextField Info window (Shift-[[Image:Learning Cocoa with Objective-C_I_1_tt163.png|]]-I). In the width field (w), enter &amp;lt;tt&amp;gt;150&amp;lt;/tt&amp;gt; as the value, and hit the Tab key to accept the value; the &amp;lt;tt&amp;gt;NSTextField&amp;lt;/tt&amp;gt; object will conform to its newly defined dimensions.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Duplicating Objects ===&lt;br /&gt;
&lt;br /&gt;
Currency Converter needs two more text fields, each the same size as the first. To place these fields, you have two options: you can drag another text field from the palette and make it the same size, or you can duplicate the first object. To create a new text field by duplication:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Select the text field, if it is not already selected.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Choose Edit → Duplicate (or use the keyboard shortcut, [[Image:Learning Cocoa with Objective-C_I_1_tt164.png|]]-D). The new text field appears slightly offset from the original field.&lt;br /&gt;
&lt;br /&gt;
Another way to duplicate a field is to click on the object, then hold down the Option and drag the object. A plus sign will appear next to the pointer to indicate that you're making a copy of the object, and the guidelines will help you move the newly duplicated object into place.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Reposition the new text field under the first text field. You'll notice that the guides will appear once again to help you move the second text field into place.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;To make the third text field, make another duplicate. Notice that Interface Builder remembers the offset from the previous Duplicate command and automatically uses that offset to create and place the third text field.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Change the Attributes of a Text Field ===&lt;br /&gt;
&lt;br /&gt;
Since the third text field will display the results of the computation, it should not be editable. To change its attributes:&lt;br /&gt;
&lt;br /&gt;
# Select the third text field.&lt;br /&gt;
# Choose Attributes from the Info window's pop-up menu, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-13|Figure 5-13]].&lt;br /&gt;
# In the Options section of the Info window, uncheck the Editable attribute so users cannot alter the contents of the field.&lt;br /&gt;
# Make sure that the Selectable attribute is on so that users can copy and paste the contents of this field to other applications.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-13&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-13. The NSTextField Info window'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt165.png|The NSTextField Info window]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add Text Labels ===&lt;br /&gt;
&lt;br /&gt;
Next we need to add some labels to the text fields, so the user will know why the fields are there.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Using [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-14|Figure 5-14]] as a guide, drag a System Font Text object from the Views palette onto the Currency Converter window.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-14&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-14. Aligning a text field with its label'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt166.png|Aligning a text field with its label]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Right-align the text using the Info window.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Duplicate the text label twice, and then edit the text for all three labels, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-15|Figure 5-15]]. To edit a text label, double-click on the current label (System Font Text) to highlight it, then type in the new label. After entering the new label, hit the Tab key to accept the new label.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;tip&amp;quot;&amp;gt;&lt;br /&gt;
'''Tip'''&lt;br /&gt;
&lt;br /&gt;
Hitting the Return key will not accept the new label; instead, it will insert a carriage return.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-15&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-15. Currency Converter's text fields and labels'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt167.png|Currency Converter's text fields and labels]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you type in the new labels, you'll notice that the text fields aren't wide enough to hold the text shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-15|Figure 5-15]]. To correct this problem, resize the text fields by grabbing the middle-left field holder and dragging the edge of the text field to the left until all of the text appears.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Add a Button to the Interface ===&lt;br /&gt;
&lt;br /&gt;
The last functional part of the user interface that we need to add is the Convert button. It needs to be set up so that it can be invoked either by clicking it or by pressing Return when the application has the user's focus.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Drag a button object from the Views palette, and place it in the lower-right portion of the window.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Double-click the title of the button to select its label, and change the title to Convert.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;With the button selected, choose Attributes in the Info window, and then choose Return from the pop-up menu labeled Equiv, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-16|Figure 5-16]]. This allows the button to respond to the Return key, as well as to mouse clicks.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-16&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-16. The NSButton Info window, used to change the Convert button's attributes, so it will respond to a mouse-click or to the Return key'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt168.png|The NSButton Info window, used to change the Convert button's attributes, so it will respond to a mouse-click or to the Return key]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Align the button under the text fields. To center the button under the text fields, you can pop up a set of measurement guides that tell you the distance from an object to any of its neighboring objects. With the button selected, hold down the Option key and point to an object whose distance from the button you want to see, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-17|Figure 5-17]]. With the Option key still down, use the arrow keys to nudge the button to the exact center of the text fields.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-17&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-17. Aligning the Convert button with the text fields'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt169.png|Aligning the Convert button with the text fields]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Decorative Line ===&lt;br /&gt;
&lt;br /&gt;
Lines can separate elements to help the user make sense of the objects in the user interface. We'll add a line between the editable fields and the result field.&lt;br /&gt;
&lt;br /&gt;
# Move the third text field and label down a bit in the user interface.&lt;br /&gt;
# Drag a horizontal line from the Views palette onto the interface, and use the alignment guides to place it right under the dollars text field.&lt;br /&gt;
# Use the selection handles on the line to extend it to each side of the interface.&lt;br /&gt;
# Move the result text field and label back up into position using the guides, then move the Convert button up into place.&lt;br /&gt;
# Resize the window using the guides to give you the proper distance from the text fields on the right and the Convert button on the bottom.&lt;br /&gt;
&lt;br /&gt;
At this point, Currency Converter's application window should look like [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-18|Figure 5-18]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-18&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-18. The final Currency Converter interface'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt170.png|The final Currency Converter interface]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;sidebar&amp;quot;&amp;gt;&lt;br /&gt;
'''Aqua Layout and Object Alignment'''&lt;br /&gt;
&lt;br /&gt;
Aligning Aqua interface widgets could be difficult, because the objects have shadows, and UI guideline metrics don't typically take the shadows into account. To help alleviate this problem, Interface Builder uses visual guides and layout rectangles to aid with object alignment. Interface Builder provides the guides to indicate when your objects line up or are in the correct location.&lt;br /&gt;
&lt;br /&gt;
In Cocoa, all drawing is done within the bounds of an object's frame. Because interface objects have shadows, they don't visually align correctly if you align the edges of the frames. For example, the Aqua UI guidelines say that a push button should be 20 pixels tall, but you actually need a frame height of 32 pixels for both the button and its shadow. The layout rectangle is what you must align, not the button itself.&lt;br /&gt;
&lt;br /&gt;
You can view the layout rectangles of objects in Interface Builder by using Layout → Show Layout Rectangles ([[Image:Learning Cocoa with Objective-C_I_1_tt171.png|]]-L). Also, Interface Builder's Size Inspector has a pop-up to toggle between the frame and layout rectangle, so you can set values by hand when appropriate.&lt;br /&gt;
&lt;br /&gt;
Look in the Alignment and Guides submenus of the Layout menu for various alignment commands and tools. You can also use the alignment tool (Tools → Alignment) to provide a floating window with buttons that perform various types of alignment functions.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Setting the Initial First Responder and Enabling Tabbing ===&lt;br /&gt;
&lt;br /&gt;
The final step in composing Currency Converter's interface has little to do with appearance and everything to do with behavior. When users launch the application, they should immediately be able to enter information in the interface and tab between the text fields.&lt;br /&gt;
&lt;br /&gt;
The first place a user's input should go when they launch the application is in the first text field. To ensure that this happens, specify the first text field as the application window's ''initial first responder --'' the object in the window that will be first in line to accept events from the keyboard. To do this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;In the Instances pane of nib file window, click on the Window instance and Control-drag a connection to the first text field in Currency Converter's window.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Select the &amp;lt;tt&amp;gt;initialFirstResponder&amp;lt;/tt&amp;gt; outlet in the Info window, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-19|Figure 5-19]], and click the Connect button.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-19&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-19. Connecting Currency Converter's initial first responder'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt172.png|Connecting Currency Converter's initial first responder]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, we want to ensure that when the user presses the Tab key, the focus moves to another text field. To do this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Select the first text field, and Control-drag a connection line from it to the second text field, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-20|Figure 5-20]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-20&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-20. Connecting text fields for inter-field tabbing'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt173.png|Connecting text fields for inter-field tabbing]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Select the &amp;lt;tt&amp;gt;nextKeyView&amp;lt;/tt&amp;gt; outlet in the Info window, and click the Connect button.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Repeat the previous two steps, but connect the second field to the first. This will make it so you can tab from the second field back up to the first text field.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Test the Interface ===&lt;br /&gt;
&lt;br /&gt;
The Currency Converter interface is now complete. Interface Builder lets you test the interface without having to write any code.&lt;br /&gt;
&lt;br /&gt;
# Choose File → Save All to save your work.&lt;br /&gt;
# Choose File → Test Interface ([[Image:Learning Cocoa with Objective-C_I_1_tt174.png|]]-R) to launch the interface in a mode where you can test it.&lt;br /&gt;
# Try various operations in the interface, such as tabbing, cutting, and pasting between text fields.&lt;br /&gt;
# When finished, choose Quit New Application ([[Image:Learning Cocoa with Objective-C_I_1_tt175.png|]]-Q) from the Interface Builder application menu to exit the text mode.&lt;br /&gt;
&lt;br /&gt;
Notice that the screen position of the Currency Converter window in Interface Builder is used as the initial position for the window when the application is launched. Place the window near the top-left corner of the screen so that it will be a convenient (and traditional) initial location.&lt;br /&gt;
&lt;br /&gt;
== Define the Classes ==&lt;br /&gt;
&lt;br /&gt;
We'll define the two classes needed for our application here in Interface Builder: a controller class and a model class. If you recall, the controller class controls the interaction between the model and view objects, while the model object holds data and defines the logic that manipulates that data.&lt;br /&gt;
&lt;br /&gt;
=== Create the Controller Class ===&lt;br /&gt;
&lt;br /&gt;
The controller class, &amp;lt;tt&amp;gt;Controller&amp;lt;/tt&amp;gt;, doesn't need to inherit any special functionality from other classes, so it will be a subclass of &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt;. To define it:&lt;br /&gt;
&lt;br /&gt;
# Click the Classes tab of the ''MainMenu.nib''window, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-21|Figure 5-21]].&lt;br /&gt;
# Select NSObject from the list of classes.&lt;br /&gt;
# Press Return to create a new subclass of &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt;, and rename it &amp;lt;tt&amp;gt;Controller&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-21&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-21. Creating the Controller class and defining outlets'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt176.png|Creating the Controller class and defining outlets]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Define outlets for the controller ====&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;Controller&amp;lt;/tt&amp;gt; object needs access to the text fields of the interface, so you must create ''outlets'' for them. &amp;lt;tt&amp;gt;Controller&amp;lt;/tt&amp;gt; will also need to communicate with the &amp;lt;tt&amp;gt;Converter&amp;lt;/tt&amp;gt; class (yet to be defined) and thus requires a fourth outlet for that purpose.&lt;br /&gt;
&lt;br /&gt;
# Select the &amp;lt;tt&amp;gt;Controller&amp;lt;/tt&amp;gt; class in the Classes window, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-21|Figure 5-21]].&lt;br /&gt;
# Select the Attributes menu item in the Info window.&lt;br /&gt;
# Add an outlet named &amp;lt;tt&amp;gt;rateField&amp;lt;/tt&amp;gt; by clicking the Add button, entering the name, and pressing Return.&lt;br /&gt;
# Create three more outlets, named &amp;lt;tt&amp;gt;dollarField&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;totalField&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;converter&amp;lt;/tt&amp;gt;, as detailed in step 3.&lt;br /&gt;
&lt;br /&gt;
==== Define actions for the controller ====&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;Controller&amp;lt;/tt&amp;gt; class needs only one action method to respond to user-interface events. When the user clicks the Convert button (or uses the Return key, which we defined as an equivalent), we want a &amp;lt;tt&amp;gt;convert:&amp;lt;/tt&amp;gt; message sent to an instance of the &amp;lt;tt&amp;gt;Controller&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
# Click on the Action tab in the Info window.&lt;br /&gt;
# Add an action named &amp;quot;&amp;lt;tt&amp;gt;convert:&amp;quot;&amp;lt;/tt&amp;gt;. Interface Builder will add the &amp;quot;&amp;lt;tt&amp;gt;:&amp;quot;&amp;lt;/tt&amp;gt; for you if you don't.&lt;br /&gt;
&lt;br /&gt;
=== Define the Model Class ===&lt;br /&gt;
&lt;br /&gt;
Like the &amp;lt;tt&amp;gt;Controller&amp;lt;/tt&amp;gt; class, the &amp;lt;tt&amp;gt;Converter&amp;lt;/tt&amp;gt; class—our model in MVC speak—doesn't need to inherit any special functionality, so you can make it a subclass of &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt;. Because instances of this class won't communicate directly with the interface, there's no need for outlets or actions.&lt;br /&gt;
&lt;br /&gt;
# In the Classes window, create the &amp;lt;tt&amp;gt;Converter&amp;lt;/tt&amp;gt; object, and make it a subclass of &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt; by clicking on NSObject, hitting Return, and entering &amp;lt;tt&amp;gt;Converter&amp;lt;/tt&amp;gt; as the subclass's name.&lt;br /&gt;
# Save &amp;lt;tt&amp;gt;MainMenu.nib&amp;lt;/tt&amp;gt;. As with any program, it's always a good idea to hit [[Image:Learning Cocoa with Objective-C_I_1_tt177.png|]]-S every now and then, so you won't lose your work.&lt;br /&gt;
&lt;br /&gt;
== Connect the Model, Controller, and View ==&lt;br /&gt;
&lt;br /&gt;
The last task that remains in Interface Builder is to hook up the various parts of our application so that each part can talk to the others.&lt;br /&gt;
&lt;br /&gt;
=== Generate an Instance of the Controller and Model ===&lt;br /&gt;
&lt;br /&gt;
When the application is first launched and the nib file is loaded, we want to create an instance of both our controller and model classes. To do this:&lt;br /&gt;
&lt;br /&gt;
# Select &amp;lt;tt&amp;gt;Controller&amp;lt;/tt&amp;gt; in the Classes tab of the nib file window.&lt;br /&gt;
# Choose Instantiate from the Classes menu. The instance will appear in the Instances view of the MainMenu.nib window, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-22|Figure 5-22]].&lt;br /&gt;
# Repeat the process for the &amp;lt;tt&amp;gt;Converter&amp;lt;/tt&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-22&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-22. The Converter and Controller instances'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt178.png|The Converter and Controller instances]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Connect the Controller to the Interface ===&lt;br /&gt;
&lt;br /&gt;
Now you can connect the &amp;lt;tt&amp;gt;Controller&amp;lt;/tt&amp;gt; instance object to the user interface. By connecting it to specific objects in the interface, you initialize its outlets. &amp;lt;tt&amp;gt;Controller&amp;lt;/tt&amp;gt; will use these outlets to get and set values in the interface.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;In the Instances display of the nib file window, Control-drag a connection line from the &amp;lt;tt&amp;gt;Controller&amp;lt;/tt&amp;gt; instance to the first text field, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-23|Figure 5-23]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-23&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-23. Connecting the Controller instance to the rate text field'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt179.png|Connecting the Controller instance to the rate text field]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Interface Builder will bring up the Connections display of the Info window. Select the action that corresponds to the first field, &amp;lt;tt&amp;gt;rateField&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Click the Connect button.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Following the same steps, connect the &amp;lt;tt&amp;gt;Controller&amp;lt;/tt&amp;gt;'s &amp;lt;tt&amp;gt;dollarField&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;totalField&amp;lt;/tt&amp;gt; outlets to the appropriate text fields.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To tell the controller that it is time to perform an action, we need to hook up the Convert button to the &amp;lt;tt&amp;gt;Controller&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Control-drag a connection from the Convert button to the &amp;lt;tt&amp;gt;Controller&amp;lt;/tt&amp;gt; instance in the nib file window. Instead of dragging from the controller object to an interface object, we are dragging a connection from a user-interface object to the controller.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;In the Connections Info window, make sure that the target is selected in the Outlets column, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-24|Figure 5-24]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-24&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-24. Connecting the Convert button to the Controller'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt180.png|Connecting the Convert button to the Controller]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Select convert: &amp;lt;tt/&amp;gt;in the Actions column.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Click the Connect button.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Connect the Model to the Controller ===&lt;br /&gt;
&lt;br /&gt;
The last connection is to hook up the instance of our &amp;lt;tt&amp;gt;Converter&amp;lt;/tt&amp;gt; model class to the &amp;lt;tt&amp;gt;Controller&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
# In the Instances view of the nib file window, Control-drag the &amp;lt;tt&amp;gt;Controller&amp;lt;/tt&amp;gt; instance to the &amp;lt;tt&amp;gt;Converter&amp;lt;/tt&amp;gt; instance.&lt;br /&gt;
# Connect the &amp;lt;tt&amp;gt;Converter&amp;lt;/tt&amp;gt; instance to the &amp;lt;tt&amp;gt;converter&amp;lt;/tt&amp;gt; outlet.&lt;br /&gt;
&lt;br /&gt;
== Implement the Classes ==&lt;br /&gt;
&lt;br /&gt;
Now we come to the part of this exercise where we take all of that work done in Interface Builder, generate the source files for our classes, and finish the class implementations in Project Builder.&lt;br /&gt;
&lt;br /&gt;
=== Generate the Source Files ===&lt;br /&gt;
&lt;br /&gt;
To generate the source files, follow these steps:&lt;br /&gt;
&lt;br /&gt;
# Go to the Classes display of the nib file window.&lt;br /&gt;
# Select the &amp;lt;tt&amp;gt;Controller&amp;lt;/tt&amp;gt; class.&lt;br /&gt;
# Choose Create Files from the Classes menu.&lt;br /&gt;
# Verify that the checkboxes in the Create column next to the ''.h'' and ''.m'' files are selected.&lt;br /&gt;
# Click the Choose button.&lt;br /&gt;
# Repeat Steps 1-5 for the &amp;lt;tt&amp;gt;Converter&amp;lt;/tt&amp;gt; class.&lt;br /&gt;
# Save the nib file.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;tip&amp;quot;&amp;gt;&lt;br /&gt;
'''Tip'''&lt;br /&gt;
&lt;br /&gt;
You can also create the files for a class by Control-clicking (or right-clicking if you have a two-button mouse) on the class name in the Classes menu and selecting the &amp;quot;Create files for...&amp;quot; menu item.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, we leave Interface Builder for this application. You'll complete the application using Project Builder.&lt;br /&gt;
&lt;br /&gt;
=== Examine an Interface (Header) File in Project Builder ===&lt;br /&gt;
&lt;br /&gt;
When Interface Builder adds the header and source files to the Currency Converter project, it tries to put them in the same group folder as other source files in the same disk folder. Since the newly created files are class implementations, move them to the Classes group if Interface Builder did not do so automatically.&lt;br /&gt;
&lt;br /&gt;
# Click Project Builder's main window to activate it.&lt;br /&gt;
# Select the Controller and Converter files in the Groups &amp;amp; Files list, and drag them into the Classes group, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-FIG-25|Figure 5-25]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-FIG-25&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 5-25. Adding the source files to the Classes group'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_1_tt181.png|Adding the source files to the Classes group]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Look at the ''Controller.h'' file that Interface Builder generated. Notice that in addition to being declared of type &amp;lt;tt&amp;gt;id&amp;lt;/tt&amp;gt;, our variables have an &amp;lt;tt&amp;gt;IBOutlet&amp;lt;/tt&amp;gt; declaration. This is a macro that, in the compiler, doesn't evaluate anything. It is used as a hint to Interface Builder's parser, telling it that the variable is an outlet. You will also notice that the &amp;lt;tt&amp;gt;convert:&amp;lt;/tt&amp;gt; method has a return type of &amp;lt;tt&amp;gt;IBAction&amp;lt;/tt&amp;gt;. This type is the &amp;lt;tt&amp;gt;same&amp;lt;/tt&amp;gt; as &amp;lt;tt&amp;gt;void&amp;lt;/tt&amp;gt; and also tells Interface Builder that the method serves as an action that can be hooked up to user-interface elements and other objects. These declarations allow you to add outlets and actions in the code and enable Interface Builder to parse them. We'll see this in action in later chapters.&lt;br /&gt;
&lt;br /&gt;
=== Add the Conversion Method ===&lt;br /&gt;
&lt;br /&gt;
We need to add a method to the &amp;lt;tt&amp;gt;Converter&amp;lt;/tt&amp;gt; class that the controller object can invoke to perform our currency conversion.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Start by declaring the &amp;lt;tt&amp;gt;convertAmount:atRate:&amp;lt;/tt&amp;gt; method in ''Converter.h'', as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-EX-1|Example 5-1]]. This method declaration states that &amp;lt;tt&amp;gt;convertAmount:atRate:&amp;lt;/tt&amp;gt; takes two arguments of type &amp;lt;tt&amp;gt;float&amp;lt;/tt&amp;gt; and returns a &amp;lt;tt&amp;gt;float&amp;lt;/tt&amp;gt; value.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-EX-1&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 5-1. Converter.h header file'''&lt;br /&gt;
&lt;br /&gt;
 #import &amp;lt;Cocoa/Cocoa.h&amp;gt;&lt;br /&gt;
 @interface Converter : NSObject&lt;br /&gt;
 {&lt;br /&gt;
 }&lt;br /&gt;
 '''- (float)convertAmount:(float)amt atRate:(float)rate;'''&lt;br /&gt;
 @end&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Add the method implementation to the ''Converter.m''file, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-EX-2|Example 5-2]]. This method simply multiplies the two arguments and returns the result.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-EX-2&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 5-2. Converter.m implementation file'''&lt;br /&gt;
&lt;br /&gt;
 #import &amp;quot;Converter.h&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 @implementation Converter&lt;br /&gt;
 '''- (float)convertAmount:(float)amt atRate:(float)rate'''&lt;br /&gt;
 {&lt;br /&gt;
     return (amt * rate);&lt;br /&gt;
 }&lt;br /&gt;
 @end&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Update the &amp;quot;empty&amp;quot; implementation of the &amp;lt;tt&amp;gt;convert:&amp;lt;/tt&amp;gt; method in ''Controller.m'' that Interface Builder generated for you, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces#learncocoa2-CHP-5-EX-3|Example 5-3]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-5-EX-3&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 5-3. Controller.m implementation file'''&lt;br /&gt;
&lt;br /&gt;
 #import &amp;quot;Controller.h&amp;quot;&lt;br /&gt;
 '''#import &amp;quot;Converter.h&amp;quot;                                                 // a'''&lt;br /&gt;
 &lt;br /&gt;
 @implementation Controller&lt;br /&gt;
 &lt;br /&gt;
 - (IBAction)convert:(id)sender&lt;br /&gt;
 {&lt;br /&gt;
 '''    float rate = [rateField floatValue];                              // b'''&lt;br /&gt;
                            '''    float amt = [dollarField floatValue];                             // c'''&lt;br /&gt;
                            '''    float total = [converter convertAmount:amt atRate:rate];          // d'''&lt;br /&gt;
                            '''    [totalField setFloatValue:total];                                 // e'''&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 @end&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The lines we added do the following things:&lt;br /&gt;
&lt;br /&gt;
# Imports the &amp;lt;tt&amp;gt;Converter&amp;lt;/tt&amp;gt; class interface.&lt;br /&gt;
# Gets the value of the &amp;lt;tt&amp;gt;rateField&amp;lt;/tt&amp;gt; outlet of the interface as a floating-point number. All text fields (and other classes that inherit from &amp;lt;tt&amp;gt;NSControl&amp;lt;/tt&amp;gt;) can present the data that they contain in various forms, including doubles, floats, Strings, and integers.&lt;br /&gt;
# Gets the value of the &amp;lt;tt&amp;gt;dollarField&amp;lt;/tt&amp;gt; outlet of the interface as a floating-point number.&lt;br /&gt;
# Calls the &amp;lt;tt&amp;gt;convertAmount:atRate:&amp;lt;/tt&amp;gt; method of the &amp;lt;tt&amp;gt;Converter&amp;lt;/tt&amp;gt; object instance.&lt;br /&gt;
# Sets the value of the &amp;lt;tt&amp;gt;totalField&amp;lt;/tt&amp;gt; outlet of the interface to the result obtained from the &amp;lt;tt&amp;gt;Converter&amp;lt;/tt&amp;gt; object instance.&lt;br /&gt;
&amp;lt;/div&amp;gt;&amp;lt;/li&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Build and Run ==&lt;br /&gt;
&lt;br /&gt;
When you click the Build and Run button, the build process begins. When Project Builder finishes—and hopefully encounters no errors along the way—it displays Build succeeded on its status line and starts the application.&lt;br /&gt;
&lt;br /&gt;
To exercise the application, enter some rates and dollar amounts, and click Convert. Of course, the more complex an application is, the more thoroughly you will need to test it. You might discover errors or shortcomings that necessitate a change in overall design, in the interface, in a custom class definition, or in the implementation of methods and functions.&lt;br /&gt;
&lt;br /&gt;
== Exercises ==&lt;br /&gt;
&lt;br /&gt;
# Change the font used by the text labels on the application to Helvetica.&lt;br /&gt;
# Change the color of text displayed in the &amp;lt;tt&amp;gt;totalField&amp;lt;/tt&amp;gt; to blue.&lt;/div&gt;</summary>
		<author><name>Docbook2Wiki</name></author>	</entry>

	</feed>