<?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/Windows%2C_Views%2C_and_Controls&amp;action=history&amp;feed=atom</id>
		<title>Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls - 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/Windows%2C_Views%2C_and_Controls&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/Windows%2C_Views%2C_and_Controls&amp;action=history"/>
		<updated>2013-05-25T19:33:12Z</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/Windows%2C_Views%2C_and_Controls&amp;diff=6266&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/Windows%2C_Views%2C_and_Controls&amp;diff=6266&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;
All of the objects that you interact with on your computer screen are displayed within ''windows''. This includes what we consider &amp;quot;normal&amp;quot; windows (those with titlebars and controls), as well as menu items, pop-up contextual menus, floating palettes, sheets, drawers, and the Dock.&lt;br /&gt;
&lt;br /&gt;
== Windows and the Window System ==&lt;br /&gt;
&lt;br /&gt;
Two interacting systems create and manage Cocoa windows. On one hand, Mac OS X's window server creates a window and displays it on screen. The window server is a process that uses Quartz—the low-level drawing system—to draw, resize, hide, and move windows. As depicted in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-1|Figure 6-1]], the window server also detects users events (such as mouse clicks or keyboard key presses) and forwards them to applications.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-1&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-1. Cocoa and the window server'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt182.png|Cocoa and the window server]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
On the other hand, the window created by the window server is paired with an object supplied by the AppKit—an instance of the &amp;lt;tt&amp;gt;NSWindow&amp;lt;/tt&amp;gt; class. Each physical window in a Cocoa program is managed by an instance of &amp;lt;tt&amp;gt;NSWindow&amp;lt;/tt&amp;gt; or a subclass. As shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-2|Figure 6-2]], when an &amp;lt;tt&amp;gt;NSWindow&amp;lt;/tt&amp;gt; object is created, the window server creates the physical window being managed. The window server references the window by its window number and the &amp;lt;tt&amp;gt;NSWindow&amp;lt;/tt&amp;gt; object instance by its own identifier.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-2&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-2. NSWindow objects and window server windows'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt183.png|NSWindow objects and window server windows]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Window, View, and Application ===&lt;br /&gt;
&lt;br /&gt;
Three classes explicitly define the functionality at the core of a running application: &amp;lt;tt&amp;gt;NSWindow&amp;lt;/tt&amp;gt; , &amp;lt;tt&amp;gt;NSView&amp;lt;/tt&amp;gt; , and &amp;lt;tt&amp;gt;NSApplication&amp;lt;/tt&amp;gt; . Each class plays a critical role in drawing the user interface of the application and directing user events to the various parts of a program. Each class inherits functionality from the &amp;lt;tt&amp;gt;NSResponder&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;NSObject&amp;lt;/tt&amp;gt; classes, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-3|Figure 6-3]]. The structure of their interaction is sometimes called the &amp;quot; core program framework.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-3&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-3. The core program framework'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt184.png|The core program framework]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
;&amp;lt;tt&amp;gt;NSResponder&amp;lt;/tt&amp;gt;&lt;br /&gt;
: &amp;lt;tt&amp;gt;NSResponder&amp;lt;/tt&amp;gt; is an abstract class that enables event handling in all classes that inherit from it. It defines the set of messages invoked when different mouse and keyboard events occur. It also defines the mechanics of event processing among objects in an application. We'll cover events in more depth in [[Learning Cocoa with Objective-C/Single-Window Applications/Event Handling|Chapter 8]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;dl&amp;gt;&lt;br /&gt;
&amp;lt;dt&amp;gt;&amp;lt;tt&amp;gt;NSWindow&amp;lt;/tt&amp;gt;&lt;br /&gt;
&amp;lt;dd&amp;gt;An &amp;lt;tt&amp;gt;NSWindow&amp;lt;/tt&amp;gt; object manages each physical window on the screen. It draws the window's frame area and responds to user actions that close, move, resize, and otherwise manipulate the window. The main purpose of an &amp;lt;tt&amp;gt;NSWindow&amp;lt;/tt&amp;gt; is to display an application's user interface, or at least a part of it, in its content area. The content area is that space below the titlebar and within the window frame.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;tt&amp;gt;NSWindow&amp;lt;/tt&amp;gt; allows you to assign a custom object as its delegate to participate in its activities. This allows you to add application-specific window functionality to your application without requiring knowledge of the &amp;lt;tt&amp;gt;NSWindow&amp;lt;/tt&amp;gt; class internals.&lt;br /&gt;
&amp;lt;/dl&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;dl&amp;gt;&lt;br /&gt;
&amp;lt;dt&amp;gt;&amp;lt;tt&amp;gt;NSView&amp;lt;/tt&amp;gt;&lt;br /&gt;
&amp;lt;dd&amp;gt;Any object you see in a widow's content area is an instance of a subclass of the &amp;lt;tt&amp;gt;NSView&amp;lt;/tt&amp;gt; class. Each view owns a rectangular region associated with a particular window. A view produces the image content for that region and responds to events occurring within it.&lt;br /&gt;
&lt;br /&gt;
Graphically, a view can be regarded as a framed canvas. The ''frame'' locates the view in its superview, defines its size, and clips the drawing to its edges. The frame can be moved around resized, and rotated in the superview. Within the frame is the ''bounds'' of the view—the rectangle within which the view draws itself.&lt;br /&gt;
&lt;br /&gt;
Views draw themselves as an indirect result of receiving the &amp;lt;tt&amp;gt;display&amp;lt;/tt&amp;gt; message or one if its variants. This message leads to the invocation of a view's &amp;lt;tt&amp;gt;drawRect:&amp;lt;/tt&amp;gt; method and the &amp;lt;tt&amp;gt;drawRect:&amp;lt;/tt&amp;gt; methods of all subviews of that view. The &amp;lt;tt&amp;gt;drawRect:&amp;lt;/tt&amp;gt; method should contain all the code needed to redraw the view completely.&lt;br /&gt;
&amp;lt;/dl&amp;gt;&lt;br /&gt;
;&amp;lt;tt&amp;gt;NSApplication&amp;lt;/tt&amp;gt;&lt;br /&gt;
: Every application has exactly one &amp;lt;tt&amp;gt;NSApplication&amp;lt;/tt&amp;gt; object instance to supervise and coordinate the overall behavior of the application. This object dispatches events to the appropriate windows, which, in turn, distribute them onto their views. The application object manages its windows; it also detects and handles changes in their status, as well as its own active and inactive status. The application object is represented in each application by the global instance variable &amp;lt;tt&amp;gt;NSApp&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
=== Key and Main Windows ===&lt;br /&gt;
&lt;br /&gt;
Windows have numerous characteristics, the first of which being that they can be onscreen or offscreen. Onscreen windows are layered on the screen in tiers managed by the window server. Onscreen windows also can carry a status: ''key'' or ''main''. Offscreen windows are hidden or minimized to the Dock and are not visible on the screen.&lt;br /&gt;
&lt;br /&gt;
;Key window&lt;br /&gt;
: The ''key window'' responds to key presses for an application and is the primary recipient of messages from menus and dialog boxes. Usually a window is made key when the user clicks it. Each application can have only one key window at a time.&lt;br /&gt;
;Main window&lt;br /&gt;
: The ''main window'' is the principle focus of user actions for an application. Often user actions in a modal key window (typically a dialog box, such as the Font dialog or an Info window) have a direct effect on the main window. Main windows often have key status.&lt;br /&gt;
&lt;br /&gt;
=== The Window Menu ===&lt;br /&gt;
&lt;br /&gt;
Cocoa applications usually include a Window menu in the menu bar at the top of the screen. The Window menu automatically lists the windows that have a titlebar, are resizable, and can become the main window. When a window's title is changed, that new title is reflected in this menu. [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-4|Figure 6-4]] shows a Window menu in Project Builder, with two open windows.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-4&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-4. Window menu in Project Builder, with two open windows'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt185.png|Window menu in Project Builder, with two open windows]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&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;
Not all applications have a Window menu, but it is automatically provided by Cocoa, and you should always use it. Also note that a list of an application's windows can be obtained by Control-clicking on the application's icon in the Dock.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Panels ===&lt;br /&gt;
&lt;br /&gt;
A ''panel'' is a special kind of window that usually serves some auxiliary function in an application. For example, much of the functionality of Interface Builder, such as the view's palette and inspector, is implemented using panels. To support the roles they typically play, panels differ from windows in the following ways:&lt;br /&gt;
&lt;br /&gt;
* To reduce screen clutter, an application's panels—except for attention panels—are displayed only when the application is active. For example, when you have more than one application running, only the panels for the active application are in the foreground.&lt;br /&gt;
* Panels can become the key window, but never the main window. For example, when working in Photoshop, you have a main window where you create and edit images. The other panels, such as the Layers panel, are open but not active (or key) until they are clicked; the focus then changes to that panel.&lt;br /&gt;
&lt;br /&gt;
The user can close a panel that is the key window by pressing the Escape key (if the panel has a close button).&lt;br /&gt;
&lt;br /&gt;
== The View Hierarchy ==&lt;br /&gt;
&lt;br /&gt;
Inside of each window—inside the area enclosed by the titlebar and the other three sides of the frame—lies the ''content view'' . The content view is the root, or top, view in a hierarchy of views that belongs to the window. Like a tree, one or more views may branch from the content view. For example, each button, text field, and label in the Currency Converter application from [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces|Chapter 5]] is a view located within the content view of the window, as illustrated in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-5|Figure 6-5]]. Enclosure determines the relationship between each view and its subviews.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-5&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-5. Currency Converter views contained by the content view'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt186.png|Currency Converter views contained by the content view]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The core program framework provides several ways for your application to access the participating objects, so you need not define outlets or instance variables for every object in the hierarchy.&lt;br /&gt;
&lt;br /&gt;
* By sending the appropriate message to the &amp;lt;tt&amp;gt;NSApp&amp;lt;/tt&amp;gt; global variable, you can obtain the application's &amp;lt;tt&amp;gt;NSWindow&amp;lt;/tt&amp;gt; objects.&lt;br /&gt;
* You can get the content view of a window by sending it the &amp;lt;tt&amp;gt;contentView&amp;lt;/tt&amp;gt; message. From the returned &amp;lt;tt&amp;gt;NSView&amp;lt;/tt&amp;gt; object, you can get all subviews of the view.&lt;br /&gt;
* You can obtain from an &amp;lt;tt&amp;gt;NSView&amp;lt;/tt&amp;gt; instance most of the objects that it references. For example, you can discover its window, its superview, and its subviews.&lt;br /&gt;
&lt;br /&gt;
The relationship between these parts of an application's view hierarchy is shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-6|Figure 6-6]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-6&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-6. Hierarchical relationship between major view hierarchy components'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt187.png|Hierarchical relationship between major view hierarchy components]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Coordinate Systems ==&lt;br /&gt;
&lt;br /&gt;
Positioning of windows and views, as well as the correct propagation of events within them, requires the use of a set of coordinate systems. These systems then help locate objects in relation to the various parts of the onscreen display. The following three types of coordinate systems are used by Cocoa:&lt;br /&gt;
&lt;br /&gt;
* Screen coordinates&lt;br /&gt;
* Window coordinates&lt;br /&gt;
* View coordinates&lt;br /&gt;
&lt;br /&gt;
=== Screen Coordinate System ===&lt;br /&gt;
&lt;br /&gt;
The ''screen coordinate system'' is the basis for all other coordinate systems. Think of the entire screen as occupying the upper-right quadrant of a two-dimensional coordinate grid, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-7|Figure 6-7]]. The screen quadrant has its origin in the lower-left corner. The positive ''x''-axis extends horizontally to the right, and the positive ''y''-axis extends vertically upward. Each unit in the coordinate system represents an on-screen pixel.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-7&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-7. Screen coordinate system'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt188.png|Screen coordinate system]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&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;
Although [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-7|Figure 6-7]] represents the coordinate system using a single display device, the screen coordinate system is really a logical rectangular union of all the screen rectangles of all physical frame buffers attached to the computer. The origin lies at the lower-left corner of that unioned rectangle.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The screen coordinate system has just one function: to position windows on the screen. When your application creates a new window, it must specify the window's initial size and location in screen coordinates.&lt;br /&gt;
&lt;br /&gt;
=== Window Coordinate System ===&lt;br /&gt;
&lt;br /&gt;
The ''window coordinate system'' defines the coordinates used within a single window on screen. It differs from the screen coordinate system in only two ways:&lt;br /&gt;
&lt;br /&gt;
* It applies only to a particular window. Each window has its own coordinate system.&lt;br /&gt;
* Its origin is at the lower-left corner of the window. If the window moves, the origin and the entire coordinate system move with it.&lt;br /&gt;
&lt;br /&gt;
=== View Coordinate System ===&lt;br /&gt;
&lt;br /&gt;
For drawing, each view uses a coordinate system transformed from the window coordinate system, or from its superview in the case of a contained view. This coordinate system has its origin point in the lower-left corner of the view, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-8|Figure 6-8]], making it convenient for drawing operations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-8&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-8. Window and view coordinate systems'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt189.png|Window and view coordinate systems]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This set of coordinate systems has several implications that are important in the layout and drawing of user-interface elements:&lt;br /&gt;
&lt;br /&gt;
* Subviews are positioned in the coordinates of their superview.&lt;br /&gt;
* Each view's coordinate system is a transformation of its superview's system.&lt;br /&gt;
* When a view is moved or transformed, all subviews are moved or transformed in concert.&lt;br /&gt;
* Because a view has its own coordinate system for drawing, drawing instructions remain constant, regardless of any change in position of itself or its superview.&lt;br /&gt;
&lt;br /&gt;
== Controls, Cells, and Formatters ==&lt;br /&gt;
&lt;br /&gt;
Controls are the user-interface objects that enable users to signal their intentions to an application and control what happens. Cells are rectangular areas embedded within a control. Each control can have one or more cells, allowing a single control to have multiple active areas. [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-9|Figure 6-9]] shows the relationship between controls and cells.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-9&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-9. Controls and cells'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt190.png|Controls and cells]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Controls and cells lie behind the appearance and behavior of most user-interface objects in Cocoa, including buttons, text fields, sliders, and browsers. Although they are quite different types of objects, they interact closely. Controls are responsible for the following:&lt;br /&gt;
&lt;br /&gt;
* Displaying the control to the user&lt;br /&gt;
* Accepting user events, such as clicking or typing&lt;br /&gt;
* Sending actions to other objects in response to a user event&lt;br /&gt;
&lt;br /&gt;
A control usually delegates the first two responsibilities to cells. Cells, which are subclass instances of the &amp;lt;tt&amp;gt;NSCell&amp;lt;/tt&amp;gt; class, let you display text or images in a view without the full overhead of an &amp;lt;tt&amp;gt;NSView&amp;lt;/tt&amp;gt; subclass. This allows for greater flexibility when creating a control, such as a spreadsheet table, with many identical elements.&lt;br /&gt;
&lt;br /&gt;
The controls that Cocoa provides fall into the categories listed in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-TABLE-1|Table 6-1]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-TABLE-1&amp;quot;&amp;gt;&lt;br /&gt;
'''Table 6-1. Cocoa's NSView controls'''&lt;br /&gt;
&lt;br /&gt;
{| border=&amp;quot;1&amp;quot; &lt;br /&gt;
|-&lt;br /&gt;
! Control !! Description&lt;br /&gt;
|-&lt;br /&gt;
| Boxes || Group together other views, including controls, in an area that can have a border and title&lt;br /&gt;
|-&lt;br /&gt;
| Browsers || Display a list of data and allow the user to select items&lt;br /&gt;
|-&lt;br /&gt;
| Buttons || Send an action message to a target when clicked&lt;br /&gt;
|-&lt;br /&gt;
| Combo Boxes || Allow a user to enter a value either by entering it directly into a text field or choosing it from a pop-up list of preselected values&lt;br /&gt;
|-&lt;br /&gt;
| Forms || Group a related set of text fields&lt;br /&gt;
|-&lt;br /&gt;
| Image Views || Display a single image in a frame and, optionally, allows a user to drag an image to it&lt;br /&gt;
|-&lt;br /&gt;
| Matrices || Group cells that work together in various ways, such as radio buttons&lt;br /&gt;
|-&lt;br /&gt;
| Outline Views || Display hierarchical data to let the user expand or collapse rows&lt;br /&gt;
|-&lt;br /&gt;
| Progress Indicators || Show that a lengthy task is underway and, optionally, can display how much of that task is complete&lt;br /&gt;
|-&lt;br /&gt;
| Sliders || Display a range of values and have an indicator, or knob, indicating the current setting&lt;br /&gt;
|-&lt;br /&gt;
| Steppers || Increment or decrement a value, such as a date or time, that is displayed next to them&lt;br /&gt;
|-&lt;br /&gt;
| Tab Views || Group views on multiple pages together into one user-interface element&lt;br /&gt;
|-&lt;br /&gt;
| Table Views || Display a set of related records, with rows representing individual records and columns representing the attributes of those records&lt;br /&gt;
|-&lt;br /&gt;
| Text Fields || Display text that a user can select or edit&lt;br /&gt;
|-&lt;br /&gt;
| Text Views || Allow the editing of text&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Controls act as managers of their cells, telling them when and where to draw and notifying them when a user event occurs in their areas. This division of labor, given the relative &amp;quot;weight&amp;quot; of cells and controls, conserves memory and provides a great boost to application performance. For example, a matrix of buttons can be implemented as a single control with many cells instead of as a set of individual controls.&lt;br /&gt;
&lt;br /&gt;
A control does not need a cell associated with it, but most user-interface objects available in Cocoa are cell-control combinations. Even a single button—from Interface Builder or programmatically created—is a control (an &amp;lt;tt&amp;gt;NSButton&amp;lt;/tt&amp;gt; instance) with an associated cell (a &amp;lt;tt&amp;gt;NSButtonCell&amp;lt;/tt&amp;gt; instance).&lt;br /&gt;
&lt;br /&gt;
The cells in a control such as a matrix must be the same size, but they can be of different classes. More complex controls, such as table views and browsers, can incorporate various sizes and types of cells. Most controls that use a single cell, such as &amp;lt;tt&amp;gt;NSButton&amp;lt;/tt&amp;gt;, provide convenience methods so you don't have to deal with the contained cell directly.&lt;br /&gt;
&lt;br /&gt;
=== Cells and Formatters ===&lt;br /&gt;
&lt;br /&gt;
When looking at the contents of cells, it is natural to consider only text (&amp;lt;tt&amp;gt;NSString&amp;lt;/tt&amp;gt;) and images (&amp;lt;tt&amp;gt;NSImage&amp;lt;/tt&amp;gt;). The content seems to be whatever is displayed. However, cells can hold other kinds of objects, such as dates (&amp;lt;tt&amp;gt;NSDate&amp;lt;/tt&amp;gt;), numbers (&amp;lt;tt&amp;gt;NSNumber&amp;lt;/tt&amp;gt;), and even application-supplied custom objects, which are shown in the user interface as strings.&lt;br /&gt;
&lt;br /&gt;
One way to make your application's user interface more attractive is to format the contents of fields that display currencies and other numeric data. Fields can have fixed decimal digits, limit numbers to specific ranges, have currency symbols, and show negative values in a special color.&lt;br /&gt;
&lt;br /&gt;
''Formatters'' are objects that translate the values of certain objects to specific on-screen representations. Formatters can also convert a formatted string on a user interface into the represented object. For example, [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-10|Figure 6-10]] shows how a date formatter translates the contents of an &amp;lt;tt&amp;gt;NSDate&amp;lt;/tt&amp;gt; object into a specific string for display.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-10&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-10. A date formatter'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt191.png|A date formatter]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can create, set, and modify formatter objects programmatically or with Interface Builder. Formatter objects handle the textual representation of the objects associated with the cells and translate what is typed into a cell to the underlying object. You can attach a formatter object to a cell in Interface Builder or use the &amp;lt;tt&amp;gt;setFormatter:&amp;lt;/tt&amp;gt; method of &amp;lt;tt&amp;gt;NSCell&amp;lt;/tt&amp;gt; to associate a formatter with a cell programmatically.&lt;br /&gt;
&lt;br /&gt;
=== A Formatted Cell Example ===&lt;br /&gt;
&lt;br /&gt;
To show formatters in action, we're going to create a simple application that shows the current date and time in a text field. In Project Builder, create a new Cocoa Application (File → New Project → Application → Cocoa Application) named &amp;quot;Simple Date&amp;quot;, and save it in your ''~/LearningCocoa''folder.&lt;br /&gt;
&lt;br /&gt;
==== Open the main nib file ====&lt;br /&gt;
&lt;br /&gt;
Begin by opening the application's main nib file in Interface Builder:&lt;br /&gt;
&lt;br /&gt;
# In Project Builder's Groups &amp;amp; Files pane, click on the disclosure triangle next to Resources to reveal the ''MainMenu.nib'' file.&lt;br /&gt;
# Double-click on the nib file to open it in Interface Builder.&lt;br /&gt;
&lt;br /&gt;
A default menu bar and window will appear when the nib file is opened.&lt;br /&gt;
&lt;br /&gt;
==== Create the user interface ====&lt;br /&gt;
&lt;br /&gt;
Set the size and initial location of the application's main window by resizing and moving the window in Interface Builder&lt;br /&gt;
&lt;br /&gt;
# Move the window near the upper-left corner of the screen by dragging its titlebar.&lt;br /&gt;
# Make the window smaller using the resize control at the bottom-right corner of the window, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-11|Figure 6-11]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-11&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-11. Cocoa window with resize control'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt192.png|Cocoa window with resize control]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add a text field ====&lt;br /&gt;
&lt;br /&gt;
Now, add a text field object to the application's 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;Select the Views palette by clicking the second button from the left in the toolbar of the Cocoa Views window, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-12|Figure 6-12]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-12&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-12. Interface Builder's Views palette'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt193.png|Interface Builder's 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;Drag a text field object onto 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;Resize the text field to make it wider, using the handles on the text field, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-13|Figure 6-13]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-13&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-13. Resizing a text field'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt194.png|Resizing a 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;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Create a controller ====&lt;br /&gt;
&lt;br /&gt;
We'll create a very simple controller, &amp;lt;tt&amp;gt;MyController&amp;lt;/tt&amp;gt;, which 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.&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;MyController&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==== Define an outlet ====&lt;br /&gt;
&lt;br /&gt;
Now the controller needs a way to send messages to the text field in the main window. Use Interface Builder to create an outlet for that purpose.&lt;br /&gt;
&lt;br /&gt;
# Select the &amp;lt;tt&amp;gt;MyController&amp;lt;/tt&amp;gt; class in the Classes window.&lt;br /&gt;
# Open the Show Info window (Tools → Show Info, or Shift-[[Image:Learning Cocoa with Objective-C_I_2_tt195.png|]]-I), and select Attributes in the pull-down menu.&lt;br /&gt;
# In the Outlets tab, click the Add button, and add an outlet named &amp;lt;tt&amp;gt;textField&amp;lt;/tt&amp;gt; (as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-14|Figure 6-14]]); enter the name, and press Return.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-14&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-14. Adding an outlet'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt196.png|Adding an outlet]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Generate a controller instance ====&lt;br /&gt;
&lt;br /&gt;
As the final step of defining the controller in Interface Builder, create an instance of the &amp;lt;tt&amp;gt;MyController&amp;lt;/tt&amp;gt; class.&lt;br /&gt;
&lt;br /&gt;
# Select &amp;lt;tt&amp;gt;MyController&amp;lt;/tt&amp;gt; in the Classes pane of the ''MainMenu.nib''window.&lt;br /&gt;
# Choose instantiate from the Classes menu (Classes → Instantiate MyController, or Option-[[Image:Learning Cocoa with Objective-C_I_2_tt197.png|]] -I).&lt;br /&gt;
&lt;br /&gt;
When you instantiate a class (that is, create an instance of it), Interface Builder switches to the Instances pane and highlights the new instance, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-15|Figure 6-15]]. The instance is named after the class.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-15&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-15. Instances pane showing a Controller object instance'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt198.png|Instances pane showing a Controller object instance]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&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;
In fact, the instantiate command does not generate a true instance of &amp;lt;tt&amp;gt;MyController&amp;lt;/tt&amp;gt;. It creates a proxy object used within Interface Builder for defining connections to other objects in the nib file. When the application is launched and the nib file's contents are loaded, the runtime system creates a true instance of &amp;lt;tt&amp;gt;MyController&amp;lt;/tt&amp;gt; and uses the proxy object to establish connections to other objects.&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 that you have created an instance of &amp;lt;tt&amp;gt;MyController&amp;lt;/tt&amp;gt;, you can use it to declare a connection between it and the text field you created earlier.&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 panel of the ''MainMenu.nib'' window, Control-drag a connection line from the &amp;lt;tt&amp;gt;MyController&amp;lt;/tt&amp;gt; instance to the text field. When the text field is outlined, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-16|Figure 6-16]], release the mouse button.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-16&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-16. Connecting the instance to the text field'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt199.png|Connecting the instance to the 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 brings up the Connections pane of the Show Info window, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-17|Figure 6-17]].&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 &amp;lt;tt&amp;gt;textField&amp;lt;/tt&amp;gt;, and click the Connect button.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-17&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-17. Connections pane of the Show Info window'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt200.png|Connections pane of the Show Info window]]&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;
==== Generate the source files ====&lt;br /&gt;
&lt;br /&gt;
Generate the source files so that we can add our controller code and run the application.&lt;br /&gt;
&lt;br /&gt;
# Go to the Classes tab of the ''MainMenu.nib'' file window.&lt;br /&gt;
# Select the &amp;lt;tt&amp;gt;MyController&amp;lt;/tt&amp;gt; class.&lt;br /&gt;
# Choose Create Files from the Classes menu (Classes → Create Files for MyController, or Option-[[Image:Learning Cocoa with Objective-C_I_2_tt201.png|]]-F).&lt;br /&gt;
&lt;br /&gt;
Interface Builder displays the dialog box shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-18|Figure 6-18]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-18&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-18. The Create Files dialog box'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt202.png|The Create Files dialog box]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Verify that the checkboxes in the Create column next to the ''.h'' and ''.m'' files are selected.&lt;br /&gt;
# Verify that the checkbox next to Simple Date is selected in the Insert into targets column.&lt;br /&gt;
# Click on the Choose button.&lt;br /&gt;
# Save the nib file (File → Save, or [[Image:Learning Cocoa with Objective-C_I_2_tt203.png|]]-S).&lt;br /&gt;
&lt;br /&gt;
Now that we've built the basic interface, we can leave Interface Builder and switch to Project Builder to complete the application. Click on Project Builder's icon in the Dock to leave Interface Builder.&lt;br /&gt;
&lt;br /&gt;
==== Statically type the outlet ====&lt;br /&gt;
&lt;br /&gt;
By default, outlet declarations are dynamically typed using the &amp;lt;tt&amp;gt;id&amp;lt;/tt&amp;gt; keyword. You can use &amp;lt;tt&amp;gt;id&amp;lt;/tt&amp;gt; as the type for any object, meaning that the class of the object is determined at runtime. When you don't need a dynamically typed object, you can—and should—statically type it as a pointer to an object. It takes a little extra time, but it is good programming practice. Static typing also allows the compiler to perform type checking, potentially saving you debugging time later.&lt;br /&gt;
&lt;br /&gt;
When you look at the source code for ''MyController.h'', note that generic outlets are declared as follows:&lt;br /&gt;
&lt;br /&gt;
 IBOutlet id variableName;&lt;br /&gt;
&lt;br /&gt;
There are two ways to type outlets. The first is to indicate the type in Interface Builder. Take another look at [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-14|Figure 6-14]], and notice the type pull-down as part of the &amp;lt;tt&amp;gt;textField&amp;lt;/tt&amp;gt; outlet definition. You can use the pull-down to select which type of object the outlet should be typed as. The other way is to change the type in the header file. To do this, use the following steps:&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 Project Builder, select ''MyController.h'' in the Other Sources folder in the left pane.&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;Change the declaration in ''MyController.h'' to match the code shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-EX-1|Example 6-1]]. Don't forget to add the pointer star!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-EX-1&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 6-1. MyController header file with a statically typed outlet'''&lt;br /&gt;
&lt;br /&gt;
 /* MyController */&lt;br /&gt;
 &lt;br /&gt;
 #import &amp;lt;Cocoa/Cocoa.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 @interface MyController : NSObject&lt;br /&gt;
 {&lt;br /&gt;
 '''    IBOutlet NSTextField * textField;'''&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;
&lt;br /&gt;
&amp;lt;/ol&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Implement the awakeFromNib method ====&lt;br /&gt;
&lt;br /&gt;
When an application is launched, the &amp;lt;tt&amp;gt;NSApplicationMain&amp;lt;/tt&amp;gt; function loads the main nib file. After a nib file has been completely unpacked and its objects connected, the runtime system sends the &amp;lt;tt&amp;gt;awakeFromNib&amp;lt;/tt&amp;gt; message to all objects derived from information in the nib file, signaling that the loading process is complete. All object's outlets are guaranteed to be initialized when &amp;lt;tt&amp;gt;awakeFromNib&amp;lt;/tt&amp;gt; is called. This lets objects in the nib file do any extra setup required before the user or the rest of the application attempts to interact with them.&lt;br /&gt;
&lt;br /&gt;
In this application, we'll use the &amp;lt;tt&amp;gt;awakeFromNib&amp;lt;/tt&amp;gt; method to print the current time to the text field in the main 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;In the left pane, click on ''MyController.m''in the Other Sources folder.&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;Edit the ''MyController.m'' file to match the code shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-EX-2|Example 6-2]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-EX-2&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 6-2. Adding the awakeFromNib method'''&lt;br /&gt;
&lt;br /&gt;
 #import &amp;quot;MyController.h&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
 @implementation MyController&lt;br /&gt;
 &lt;br /&gt;
 '''- (void)awakeFromNib'''&lt;br /&gt;
                               '''{'''&lt;br /&gt;
                               '''&amp;lt;nowiki&amp;gt;    [textField setObjectValue:[NSCalendarDate date]];&amp;lt;/nowiki&amp;gt;'''&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;Save the project (File → Save, or [[Image:Learning Cocoa with Objective-C_I_2_tt205.png|]]-S).&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;Build and run the application ([[Image:Learning Cocoa with Objective-C_I_2_tt206.png|]]-R). You should see a window that resembles [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-19|Figure 6-19]].&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;Quit the application.&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;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-19&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-19. Simple Date application'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt207.png|Simple Date application]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Add the formatter ====&lt;br /&gt;
&lt;br /&gt;
Wait a minute . . . our date looks really nerdy. Instead of this representation for the date, we want to make a nicely formatted date. To do this, switch back to Interface Builder to perform the following steps:&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 date formatter from the Views palette to the text field, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-20|Figure 6-20]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-20&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-20. Adding a date formatter to a text field'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt208.png|Adding a date formatter to a 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;While the text field is selected, bring up the Show Info window (Shift-[[Image:Learning Cocoa with Objective-C_I_2_tt209.png|]]-I) if it isn't open already.&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 Formatter pane of the Show Info window, specify the &amp;lt;tt&amp;gt;%c&amp;lt;/tt&amp;gt; date format, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-21|Figure 6-21]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-21&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-21. Formatter pane'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt210.png|Formatter pane]]&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;Save the nib file (File → Save, or [[Image:Learning Cocoa with Objective-C_I_2_tt211.png|]]-S).&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;Return to Project Builder, and build and run ([[Image:Learning Cocoa with Objective-C_I_2_tt212.png|]]-R) the project. You should see something like [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-22|Figure 6-22]].&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;Quit the application (Simple Date → Quit NewApplication, or [[Image:Learning Cocoa with Objective-C_I_2_tt213.png|]]-Q).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-22&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-22. Simple Date application using a formatter'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt214.png|Simple Date application using a formatter&lt;br /&gt;
&lt;br /&gt;
]]&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;
== Targets and Actions ==&lt;br /&gt;
&lt;br /&gt;
The target/action pattern is part of the mechanism by which user-interface controls respond to user actions, enabling users to communicate their intentions to an application. The target/action pattern specifies a one-to-one relationship between two objects: the control (more specifically, the control's cell) and its target. When a user clicks a user-interface control, it sends an action message to the target, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-23|Figure 6-23]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-23&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-23. Target/action pattern'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt215.png|Target/action pattern]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The target/action relationship is typically defined using Interface Builder, in which you select a target object for a control, along with a specific action message that will be sent to the target. Target/action relationships can also be set (or modified) while an application is running.&lt;br /&gt;
&lt;br /&gt;
=== Target/Action Example ===&lt;br /&gt;
&lt;br /&gt;
To show target/action pattern in practice, we are going to modify the Simple Date application we've already built.&lt;br /&gt;
&lt;br /&gt;
==== Add a Refresh button ====&lt;br /&gt;
&lt;br /&gt;
In Interface Builder, open the ''MainMenu.nib'' file, and add a button named &amp;quot;Refresh&amp;quot; to the main window, using the following steps:&lt;br /&gt;
&lt;br /&gt;
# In the Cocoa-Views window, grab an &amp;lt;tt&amp;gt;NSButton&amp;lt;/tt&amp;gt; object, and drag it to the main window.&lt;br /&gt;
# Change the name of the button by double-clicking on the &amp;quot;Button&amp;quot; name to highlight it, typing &amp;quot;Refresh&amp;quot;, then hitting Return to accept the new name. The interface should now look similar to [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-24|Figure 6-24]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-24&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-24. Target/action example interface'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt216.png|Target/action example interface]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Define an action ====&lt;br /&gt;
&lt;br /&gt;
When the user presses the Refresh button, we want the date to update itself. To do this, we need to define an action on our &amp;lt;tt&amp;gt;MyController&amp;lt;/tt&amp;gt; object that the button will call. Define an action called &amp;lt;tt&amp;gt;refresh:&amp;lt;/tt&amp;gt; using the following steps:&lt;br /&gt;
&lt;br /&gt;
# Select the &amp;lt;tt&amp;gt;MyController&amp;lt;/tt&amp;gt; class in the Classes pane of the ''MainMenu.nib''window.&lt;br /&gt;
# In the Attributes pane of the Show Info window, click on the Actions tab and then on the Add button.&lt;br /&gt;
# Change &amp;lt;tt&amp;gt;myAction:&amp;lt;/tt&amp;gt; to &amp;lt;tt&amp;gt;refresh:&amp;lt;/tt&amp;gt; and hit Return to add the action, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-25|Figure 6-25]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-25&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-25. Defining an action'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt217.png|Defining an action]]&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Connect the button to the action ====&lt;br /&gt;
&lt;br /&gt;
For &amp;lt;tt&amp;gt;MyController&amp;lt;/tt&amp;gt; to receive an action message from the button in the user interface, you must connect the button to the controller. The button object keeps a reference to its target using an outlet; not surprisingly, the outlet is named &amp;lt;tt&amp;gt;target&amp;lt;/tt&amp;gt;. To make this connection:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Click on the Instances tab in the ''MainMenu.nib''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;Control-drag a connection from the Refresh button to the &amp;lt;tt&amp;gt;MyController&amp;lt;/tt&amp;gt; instance in the ''MainMenu.nib'' window, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-26|Figure 6-26]]. When the instance is outlined, release the mouse button.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-26&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-26. Creating a connection between the Refresh button and the controller'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt218.png|Creating a connection between the Refresh button and 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;In the Connections pane of the Show Info window, make sure target is selected in the Outlets 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;Select &amp;lt;tt&amp;gt;refresh:&amp;lt;/tt&amp;gt; in the right column, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-27|Figure 6-27]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-27&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-27. Connecting to the refresh: method'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt219.png|Connecting to the refresh: method]]&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;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;Save the ''MainMenu.nib''file (File → Save, or [[Image:Learning Cocoa with Objective-C_I_2_tt220.png|]]-S).&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;
==== Update the source files ====&lt;br /&gt;
&lt;br /&gt;
Since we made changes to the controller, the source files need to be updated so that we can add our controller code and run the application.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;ol&amp;gt;&lt;br /&gt;
&amp;lt;li&amp;gt;&amp;lt;div&amp;gt;Go to the Classes tab of the ''MainMenu.nib'' file 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;MyController&amp;lt;/tt&amp;gt; class.&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 Classes → Create Files for MyController (Option-[[Image:Learning Cocoa with Objective-C_I_2_tt221.png|]]-F). Follow the dialog boxes to save the files into the project.&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 warn you that the file ''MyController.h''already exists. Click on the Merge button to bring up the FileMerge tool, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-28|Figure 6-28]]. If you don't see the window shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-28|Figure 6-28]], look for the FileMerge icon on your Dock, and click it to bring the FileMerge window to the top.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-28&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-28. Merge tool in action'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt222.png|Merge tool in action]]&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;The FileMerge tool consists of three panes. The left pane is the newly generated file from Interface Builder, the right pane is the file in your project, and the bottom pane is the result of the merge. We want to keep our edits that were statically typed for the &amp;lt;tt&amp;gt;textField&amp;lt;/tt&amp;gt; outlet. To do this, we select the #1 arrow, then &amp;quot;Choose right&amp;quot; from the Actions pop-up at the bottom-right corner 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;Save the ''MyController.h'' file from FileMerge (File → Save Merge ,or [[Image:Learning Cocoa with Objective-C_I_2_tt223.png|]]-S) and then close 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;Return to Interface Builder. You will be prompted to merge the ''MyController.m''file; do so.&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;Merge the Files (as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-FIG-29|Figure 6-29]]) by selecting &amp;quot;Choose both (left first)&amp;quot; for the first block of code and &amp;quot;Choose right&amp;quot; for the second block of code. Unfortunately, at the time of writing this book, FileMerge isn't smart enough to handle this merge on its own. If you encounter this problem, you'll need to add the curly braces after the &amp;lt;tt&amp;gt;refresh:&amp;lt;/tt&amp;gt; method yourself.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-FIG-29&amp;quot;&amp;gt;&lt;br /&gt;
'''Figure 6-29. Merging MyController.m'''&lt;br /&gt;
&lt;br /&gt;
[[Image:Learning Cocoa with Objective-C_I_2_tt224.png|Merging MyController.m]]&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;Save the resulting merged file (File → Save Merge, or [[Image:Learning Cocoa with Objective-C_I_2_tt225.png|]]-S), and quit the FileMerge tool ([[Image:Learning Cocoa with Objective-C_I_2_tt226.png|]]-Q).&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;Save the nib file (File → Save, or [[Image:Learning Cocoa with Objective-C_I_2_tt227.png|]]-S).&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;
There are other ways of adding outlets and actions to your source code and the nib files that don't involve using the FileMerge tool. We'll see some of these other ways in later chapters.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;sidebar&amp;quot;&amp;gt;&lt;br /&gt;
'''Making Outlet and Action Connections'''&lt;br /&gt;
&lt;br /&gt;
Developers new to Cocoa sometimes get confused when making action and outlet connections in Interface Builder. A general rule for determining which way to draw a connection line is to draw the connection in the direction that messages will flow:&lt;br /&gt;
&lt;br /&gt;
* To make an action connection, draw a line from a control object in the user interface, such as a button or a text field, to the object instance that should receive the action message.&lt;br /&gt;
* To make an outlet connection, draw a line from the object instance to another object in the application.&lt;br /&gt;
&lt;br /&gt;
Another way to clarify connections is to consider who needs to find whom. With outlets, the object needs to find some other object, so the connection is from the first object to the second object. With actions, the control object needs to find an object to send messages to, so the connection is from the control object to the target object.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Implement the action method ====&lt;br /&gt;
&lt;br /&gt;
Now switch back to Project Builder. Our next step is to edit the ''MyController.m'' file and insert the code for the &amp;lt;tt&amp;gt;refresh:&amp;lt;/tt&amp;gt; method, as shown in [[Learning Cocoa with Objective-C/Single-Window Applications/Windows, Views, and Controls#learncocoa2-CHP-6-EX-3|Example 6-3]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;learncocoa2-CHP-6-EX-3&amp;quot;&amp;gt;&lt;br /&gt;
'''Example 6-3. Implementing the refresh: method'''&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;nowiki&amp;gt;#import &amp;quot;MyController.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
@implementation MyController&lt;br /&gt;
&lt;br /&gt;
- (void) awakeFromNib&lt;br /&gt;
{&lt;br /&gt;
    [textField setObjectValue:[NSCalendarDate date]];&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
- (IBAction)refresh:(id)sender&lt;br /&gt;
{&lt;br /&gt;
&amp;lt;/nowiki&amp;gt;'''&amp;lt;nowiki&amp;gt;    [textField setObjectValue:[NSCalendarDate date]];&amp;lt;/nowiki&amp;gt;'''&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 @end&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Save the changes to the nib file (File → Save, or [[Image:Learning Cocoa with Objective-C_I_2_tt228.png|]]-S).&lt;br /&gt;
# Build and run the application ([[Image:Learning Cocoa with Objective-C_I_2_tt229.png|]] -R).&lt;br /&gt;
&lt;br /&gt;
When the application launches, you can refresh the date display by pressing the Refresh button. Of course, the date won't change if you've selected to show only the date in the text field. If you've opted to also display the current time, hitting the Refresh button should update the time.&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;
As we progress through the chapters in this book, our examples will contain more and more methods. It doesn't matter to the compiler which order methods appear in your source files; they can be in any order you want.&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Exercises ==&lt;br /&gt;
&lt;br /&gt;
# Read the online documentation for the &amp;lt;tt&amp;gt;NSWindow&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;NSView&amp;lt;/tt&amp;gt; classes.&lt;br /&gt;
# Give the window of our Simple Date application a title other than &amp;quot;Window&amp;quot;.&lt;br /&gt;
# Go back to the Currency Converter application in [[Learning Cocoa with Objective-C/Single-Window Applications/Graphical User Interfaces|Chapter 5]], and statically type the &amp;lt;tt&amp;gt;rateField&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;dollarField&amp;lt;/tt&amp;gt;, and &amp;lt;tt&amp;gt;totalField&amp;lt;/tt&amp;gt; outlets.&lt;/div&gt;</summary>
		<author><name>Docbook2Wiki</name></author>	</entry>

	</feed>