<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog @ Tim Isted &#187; Bindings</title>
	<atom:link href="http://www.timisted.net/blog/archive/category/programming/cocoa/bindings-cocoa-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.timisted.net/blog</link>
	<description>Cocoa Programming and Other Things</description>
	<lastBuildDate>Sat, 12 Dec 2009 17:43:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A Bindable Custom NSView Subclass</title>
		<link>http://www.timisted.net/blog/archive/a-bindable-custom-nsview-subclass/</link>
		<comments>http://www.timisted.net/blog/archive/a-bindable-custom-nsview-subclass/#comments</comments>
		<pubDate>Thu, 27 Nov 2008 15:54:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Bindings]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Core Data]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[NSView]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://www.timisted.net/blog/?p=259</guid>
		<description><![CDATA[In this post I&#8217;ll walk through writing a custom subclass of NSView that can bind to an NSArrayController to display information either from some generic array connected to the array controller, or from a Core Data object model.
The view we&#8217;ll be creating is a custom view to display a pie-chart; in the sample app the [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I&#8217;ll walk through writing a custom subclass of NSView that can bind to an NSArrayController to display information either from some generic array connected to the array controller, or from a Core Data object model.</p>
<p>The view we&#8217;ll be creating is a custom view to display a pie-chart; in the sample app the pie-chart will be drawn from data entered in an NSTableView. The finished application looks like the following:<br />
<img class="size-full wp-image-235" title="customnsviewapp" src="http://www.timisted.net/blog/wp-content/uploads/2008/09/customnsviewapp.gif" alt="The finished sample application showing the custom NSView subclass bound to a Core Data entity" /><br />
<span id="more-259"></span><br />
If you&#8217;ve read previous posts on this site, you just might possibly have realized that I&#8217;m quite a big fan of Core Data. Unsurprisingly, therefore, the sample application is a Core Data document-based app. Its project and code can be downloaded here: <a href="http://www.timisted.net/blog/wp-content/uploads/2008/11/customnsview.zip">customnsview.zip</a>.</p>
<p>We&#8217;ll be looking at binding to an array controller&#8217;s &#8216;arrangedObjects&#8217; key, and also its &#8217;selectionIndexes&#8217; key to enable the user to change which object is selected by clicking in our view. The segments on our pie-chart have both a &#8216;name&#8217; and a numeric &#8216;value&#8217; so, in the same way that you can bind a model&#8217;s keys to different column tables in an NSTableView, we&#8217;ll make it possible to bind a model&#8217;s keys to the names and values on the pie-chart. This would allow us to bind, for example, a department&#8217;s employees&#8217; &#8216;fullNames&#8217; and their &#8217;salaries&#8217; to view relative salaries, or in a file-viewing application, &#8216;fileNames&#8217; and &#8216;fileSizes&#8217; etc.</p>
<h3>Basic Principles</h3>
<p>To be able to display our information in the view, the view needs to maintain its own store of the data. We&#8217;ll maintain one array of segment names and one array of segment values. When the view is told to display itself, it will sum the values and use the total to determine how big each segment should be relative to the others; the view will then draw each segment using various geometric functions and bezier paths.</p>
<h3>Creating our View</h3>
<p>We&#8217;ll begin by creating a very basic subclass of NSView to which we can add various functionality throughout this article.</p>
<h4>The View Subclass</h4>
<ul>
<li>Begin by creating a new Xcode &#8216;Cocoa Core Data Document-Based Application&#8217;, called &#8216;CustomNSView&#8217;.</li>
<li>In the new project&#8217;s browser, right-click (or control-click) on the Classes group and choose Add -&gt; New File&#8230;; in the window that appears, chose the &#8216;Objective-C NSView Subclass&#8217; and click Next.</li>
<li>Name the file &#8216;MyPieChartView.m&#8217; and make sure the &#8216;Also create &#8220;MyPieChartView.h&#8221; checkbox is ticked.</li>
<li>After clicking &#8216;Finish&#8217; to create the files, open up &#8216;MyPieChartView.m&#8217; and change the template <code>drawRect:</code> method with the one below; we&#8217;ll also override the &#8216;isFlipped&#8217; method to return YES so that our coordinate system has {0,0} set to the top left of the view:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>rect
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
	NSRectFill<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>isFlipped
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>Next we need to add an instance of our view to the Window of our document object.</p>
<ul>
<li>Open up MyDocument.xib in Interface Builder and delete the placeholder label from the Window.</li>
<li>Drag a custom view onto the Window, set it to resize with the window and use the identity inspector to change the class of the view to &#8216;MyPieChartView&#8217;. My interface looks like this:<br />
<img class="size-full wp-image-262" title="basic-interface-builder-win" src="http://www.timisted.net/blog/wp-content/uploads/2008/11/basic-interface-builder-win.gif" alt="The basic interface for the document" /></li>
</ul>
<p>If you build and run the application, you should find that each new document opens a window with our black box view showing.</p>
<h4>Adding the Arrays and Bindings</h4>
<p>At this point, we need to add a couple of arrays to the view that will hold the names and values of each pie segment.</p>
<ul>
<li>Open the MyPieChartView.h header file and add in two NSArrays for the names and values, together with KVC accessors:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyPieChartView <span style="color: #002200;">:</span> <span style="color: #400080;">NSView</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentNamesArray;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentValuesArray;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentNamesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentNamesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentValuesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentValuesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>We&#8217;ll be allowing other objects to bind to these arrays so we&#8217;ll need to add an <code>initialize:</code> method to our view that exposes them for binding. Whenever the arrays change, we need to get the view to redisplay itself so in the setter methods we need to call <code>[self setNeedsDisplayInRect:]</code>. We also need to release the stored arrays when the view is deallocated. In MyPieChartView.m, implement these methods:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>initialize
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentNamesArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>_segmentNamesArray retain<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentNamesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_segmentNamesArray release<span style="color: #002200;">&#93;</span>;
	_segmentNamesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>newArray copy<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self setNeedsDisplayInRect<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self visibleRect<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentValuesArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>_segmentValuesArray retain<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentValuesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_segmentValuesArray release<span style="color: #002200;">&#93;</span>;
	_segmentValuesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>newArray copy<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self setNeedsDisplayInRect<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self visibleRect<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>_segmentNamesArray release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_segmentValuesArray release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>With our arrays setup, we need to create a data model and some objects to which we can bind.</p>
<h3>The Data Model</h3>
<p>For the purposes of this demonstration, we need only a very simple data model together with an NSTableView in the interface to add and remove objects.</p>
<ul>
<li>Open MyDocument.xcdatamodel and add a new entity called &#8216;PieSegment&#8217;. Add two attributes, a String and a Decimal, called &#8216;name&#8217; and &#8216;amount&#8217; respectively. Give the &#8216;name&#8217; attribute a default string like &#8216;new segment&#8217; and &#8216;amount&#8217; a default value of 0.</li>
<li>Return to MyDocument.xib in Interface Builder and add a new NSArrayController called &#8217;segmentsArrayController&#8217; for the &#8216;PieSegment&#8217; entity, that prepares content. Bind its Managed Object Context to the File&#8217;s Owner managedObjectContext.</li>
<li>Add an NSTableView and two buttons to the document window; bind the columns of the table view to the &#8216;name&#8217; and &#8216;amount&#8217; path of the segmentsArrayController arrangedObjects key.</li>
<li>Add a decimal NSNumberFormatter to the &#8216;amount&#8217; column.</li>
<li>Connect the buttons to the array controller&#8217;s add: and remove: actions and make any other adjustments as you wish. My interface looks like this:<br />
<img class="aligncenter size-full wp-image-266" title="The revised interface for the CustomNSView application" src="http://www.timisted.net/blog/wp-content/uploads/2008/11/interface-redesigned.gif" alt="" /></li>
</ul>
<p>Build and Run the application to make sure that the table view and buttons work as expected.</p>
<h3>Setting up the Bindings and Displaying the Data</h3>
<p>Obviously, nothing is happening yet with our custom NSView since we haven&#8217;t bound anything to our names and values arrays, nor have we modified the view&#8217;s <code>drawRect:</code> method. We&#8217;ll rectify that now:</p>
<ul>
<li>Open the MyDocument.h header file and add in IBOutlets for the array controller and view:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyDocument <span style="color: #002200;">:</span> <span style="color: #400080;">NSPersistentDocument</span>
<span style="color: #002200;">&#123;</span>
	IBOutlet <span style="color: #400080;">NSArrayController</span> <span style="color: #002200;">*</span>segmentsArrayController;
	IBOutlet <span style="color: #400080;">NSView</span> <span style="color: #002200;">*</span>pieChartView;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Connect these outlets to the relevant view and controller in Interface Builder.</li>
<li>In MyDocument.m, modify the <code>windowControllerDidLoadNib:</code> method so that we bind the view to the array controller:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>windowControllerDidLoadNib<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSWindowController</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>windowController
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>super windowControllerDidLoadNib<span style="color: #002200;">:</span>windowController<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.name&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.amount&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>In MyPieChartView.m, modify the <code>drawRect:</code> method with some very simple text display code that will just output the two arrays side-by-side:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>rect
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
	NSRectFill<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">NSPoint</span> midPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span>NSMidX<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>, NSMidY<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>namesText <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self segmentNamesArray<span style="color: #002200;">&#93;</span> description<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>valuesText <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self segmentValuesArray<span style="color: #002200;">&#93;</span> description<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> whiteColor<span style="color: #002200;">&#93;</span>, NSForegroundColorAttributeName, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFont</span> systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">10</span><span style="color: #002200;">&#93;</span>, NSFontAttributeName, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	NSSize namesSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>namesText sizeWithAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
	NSSize valuesSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>valuesText sizeWithAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">NSPoint</span> namesTextPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span>midPoint.x <span style="color: #002200;">-</span> namesSize.width <span style="color: #002200;">-</span> <span style="color: #2400d9;">2</span>, midPoint.y <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>namesSize.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">NSPoint</span> valuesTextPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span>midPoint.x <span style="color: #002200;">+</span> valuesSize.width <span style="color: #002200;">+</span> <span style="color: #2400d9;">2</span>, midPoint.y <span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>valuesSize.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>namesText drawAtPoint<span style="color: #002200;">:</span>namesTextPoint withAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>valuesText drawAtPoint<span style="color: #002200;">:</span>valuesTextPoint withAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p style="text-align: center;">If you build and run the application, you&#8217;ll find that values added to the table view will show up in columns in the pie-chart view:<br />
<img class="aligncenter size-full wp-image-269" title="The view showing data with a basic textual display" src="http://www.timisted.net/blog/wp-content/uploads/2008/11/application-with-basic-disp.gif" alt="" /></p>
<h3>Displaying the Data as a Pie-Chart</h3>
<p>Whilst at this point we have a nicely-bound custom NSView subclass, we still need to use the provided data to display a pie-chart. There are many different ways to go about displaying such a chart; what follows is the path I decided to take. You may have different suggestions, in which case please feel free to make comments. The methods I use to calculate the sizes and shapes of the segments may not be the fastest, or least memory-intensive, but should be fairly easily understood by anyone with basic mathematical and geometric knowledge.</p>
<p>We&#8217;ll draw the segments of the &#8216;pie&#8217; using NSBezierPaths. Since it isn&#8217;t only the changing of data that can cause a view to be told to draw itself, we&#8217;ll &#8216;cache&#8217; these paths in an array held by the view, updating them only if the data changes. To make it easy to color each segment we&#8217;ll maintain a static class array of colors, generated randomly when needed, such that additional instances of our class can also use the same colors.</p>
<h4>Adding Drawing Information to our View</h4>
<p>We need to add an array pointer to MyPieChartView that will hold the segment paths. We also need a method to access this paths array, and a method that will regenerate it when necessary. We need a method that will produce a random color, and another method that will supply a color from a static colors array if given a zero-based index, generating it if necessary.</p>
<ul>
<li>Change the MyPieChartView.h header file by adding the new array pointer and methods:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyPieChartView <span style="color: #002200;">:</span> <span style="color: #400080;">NSView</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentNamesArray;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentValuesArray;
&nbsp;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_segmentPathsArray;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentNamesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentNamesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentValuesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentValuesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentPathsArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>generateDrawingInformation;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>randomColor;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>colorForIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>index;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>In MyPieChartView.m implement the <code>segmentPathsArray:</code> method and modify the <code>dealloc:</code> method to release the array:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentPathsArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> _segmentPathsArray;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>_segmentNamesArray release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_segmentValuesArray release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentPathsArray <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#91;</span>_segmentPathsArray release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Implement the <code>randomColor:</code> and <code>colorForIndex:</code> methods like this:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>randomColor
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">float</span> red <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>random<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">%</span>1000<span style="color: #002200;">&#41;</span><span style="color: #002200;">/</span><span style="color: #2400d9;">1000.0</span>;
	<span style="color: #a61390;">float</span> green <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>random<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">%</span>1000<span style="color: #002200;">&#41;</span><span style="color: #002200;">/</span><span style="color: #2400d9;">1000.0</span>;
	<span style="color: #a61390;">float</span> blue <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>random<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">%</span>1000<span style="color: #002200;">&#41;</span><span style="color: #002200;">/</span><span style="color: #2400d9;">1000.0</span>;
	<span style="color: #a61390;">float</span> alpha <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>random<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">%</span>1000<span style="color: #002200;">&#41;</span><span style="color: #002200;">/</span><span style="color: #2400d9;">1000.0</span>;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> colorWithCalibratedRed<span style="color: #002200;">:</span>red green<span style="color: #002200;">:</span>green blue<span style="color: #002200;">:</span>blue alpha<span style="color: #002200;">:</span>alpha<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>colorForIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>index
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">static</span> <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>colorsArray <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> colorsArray <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		colorsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> index <span style="color: #002200;">&amp;</span>gt;<span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>colorsArray count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">unsigned</span> currentNum <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
		<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> currentNum <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>colorsArray count<span style="color: #002200;">&#93;</span>; currentNum <span style="color: #002200;">&amp;</span>lt;<span style="color: #002200;">=</span> index; currentNum<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #002200;">&#91;</span>colorsArray addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self randomColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>colorsArray objectAtIndex<span style="color: #002200;">:</span>index<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>The <code>generateDrawingInformation:</code> method is quite complicated. It keeps hold of its own pointer to the segmentValuesArray, and uses this to generate the paths array. Here I&#8217;m also using <code>#define</code> to set a value for the padding around the pie-chart.

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>generateDrawingInformation
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Keep a pointer to the segmentValuesArray</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>cachedSegmentValuesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentValuesArray<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get rid of any existing Paths Array</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentPathsArray <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>_segmentPathsArray release<span style="color: #002200;">&#93;</span>;
		_segmentPathsArray <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// If there aren't any values to display, we can exit now</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">1</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">return</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get the sum of the amounts and exit if it is zero</span>
	<span style="color: #a61390;">float</span> sumOfAmounts <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> <span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>eachAmountToSum <span style="color: #a61390;">in</span> cachedSegmentValuesArray <span style="color: #002200;">&#41;</span>
		sumOfAmounts <span style="color: #002200;">+=</span> <span style="color: #002200;">&#91;</span>eachAmountToSum floatValue<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> sumOfAmounts <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">return</span>;
&nbsp;
	_segmentPathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #6e371a;">#define PADDINGAROUNDGRAPH 20.0</span>
&nbsp;
	<span style="color: #a61390;">NSRect</span> viewBounds <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">NSRect</span> graphRect <span style="color: #002200;">=</span> NSInsetRect<span style="color: #002200;">&#40;</span>viewBounds, PADDINGAROUNDGRAPH, PADDINGAROUNDGRAPH<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Make the graphRect square and centred</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> graphRect.size.width <span style="color: #002200;">&amp;</span>gt; graphRect.size.height <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">double</span> sizeDifference <span style="color: #002200;">=</span> graphRect.size.width <span style="color: #002200;">-</span> graphRect.size.height;
		graphRect.size.width <span style="color: #002200;">=</span> graphRect.size.height;
		graphRect.origin.x <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span>sizeDifference <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> graphRect.size.height <span style="color: #002200;">&amp;</span>gt; graphRect.size.width <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">double</span> sizeDifference <span style="color: #002200;">=</span> graphRect.size.height <span style="color: #002200;">-</span> graphRect.size.width;
		graphRect.size.height <span style="color: #002200;">=</span> graphRect.size.width;
		graphRect.origin.y <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span>sizeDifference <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Calculate how big a 'unit' is</span>
	<span style="color: #a61390;">float</span> unitSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">360.0</span> <span style="color: #002200;">/</span> sumOfAmounts<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> unitSize <span style="color: #002200;">&amp;</span>gt; <span style="color: #2400d9;">360</span> <span style="color: #002200;">&#41;</span>
		unitSize <span style="color: #002200;">=</span> <span style="color: #2400d9;">360</span>;
&nbsp;
	<span style="color: #a61390;">float</span> radius <span style="color: #002200;">=</span> graphRect.size.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span>;
&nbsp;
	<span style="color: #a61390;">NSPoint</span> midPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span> NSMidX<span style="color: #002200;">&#40;</span>graphRect<span style="color: #002200;">&#41;</span>, NSMidY<span style="color: #002200;">&#40;</span>graphRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// cycle through the segmentValues and create the bezier paths</span>
	<span style="color: #a61390;">float</span> currentDegree <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">unsigned</span> currentIndex;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> currentIndex <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; currentIndex <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span>; currentIndex<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>eachValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray objectAtIndex<span style="color: #002200;">:</span>currentIndex<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">float</span> startDegree <span style="color: #002200;">=</span> currentDegree;
		currentDegree <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>eachValue floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">*</span> unitSize<span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">float</span> endDegree <span style="color: #002200;">=</span> currentDegree;
&nbsp;
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachSegmentPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBezierPath</span> bezierPath<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachSegmentPath moveToPoint<span style="color: #002200;">:</span>midPoint<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachSegmentPath appendBezierPathWithArcWithCenter<span style="color: #002200;">:</span>midPoint radius<span style="color: #002200;">:</span>radius startAngle<span style="color: #002200;">:</span>startDegree endAngle<span style="color: #002200;">:</span>endDegree<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachSegmentPath closePath<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// close path also handles the lines from the midPoint to the start and end of the arc</span>
		<span style="color: #002200;">&#91;</span>eachSegmentPath setLineWidth<span style="color: #002200;">:</span><span style="color: #2400d9;">2.0</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>_segmentPathsArray addObject<span style="color: #002200;">:</span>eachSegmentPath<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>The paths array should be recalculated whenever the bound arrays change, so modify the two setter methods:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentNamesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_segmentNamesArray release<span style="color: #002200;">&#93;</span>;
	_segmentNamesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>newArray copy<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self generateDrawingInformation<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self setNeedsDisplayInRect<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self visibleRect<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentValuesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_segmentValuesArray release<span style="color: #002200;">&#93;</span>;
	_segmentValuesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>newArray copy<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self generateDrawingInformation<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self setNeedsDisplayInRect<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self visibleRect<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Finally, we can now replace the <code>drawRect:</code> method so that it displays the paths:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>rect
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> whiteColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// white background</span>
	NSRectFill<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>pathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentPathsArray<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">unsigned</span> count;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>pathsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>pathsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// fill the path with the drawing color for this index</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self colorForIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachPath fill<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// draw a black border around it</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachPath stroke<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>When you build and run the project, you&#8217;ll find that segments added to the table view with non-zero amounts will show up as a basic, colored pie-chart in the view:<br />
<img class="aligncenter size-full wp-image-276" title="Basic pie chart display in the custom NSView" src="http://www.timisted.net/blog/wp-content/uploads/2008/11/basic-pie-chart-display.gif" alt="" width="500" height="281" /></p>
<h3>Displaying the Text</h3>
<p>So far we haven&#8217;t done anything at all with the segment names; we still need to display these on our pie-chart. We&#8217;ll maintain an array of dictionaries containing information to display the text. We&#8217;ll modify the <code>generateDrawingInformation:</code> method so that it creates this array of dictionaries, and change the <code>drawRect:</code> method to display the text on the pie-chart.</p>
<ul>
<li>Open the MyPieChartView.h header file and add in an array for the text information, along with an accessor:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyPieChartView <span style="color: #002200;">:</span> <span style="color: #400080;">NSView</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentNamesArray;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentValuesArray;
&nbsp;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_segmentPathsArray;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_segmentTextsArray;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentNamesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentNamesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentValuesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentValuesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentPathsArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentTextsArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>generateDrawingInformation;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>randomColor;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>colorForIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>index;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Add the accessor method to MyPieChartView.m and change <code>dealloc:</code> to release it:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentTextsArray
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> _segmentTextsArray;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>_segmentNamesArray release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_segmentValuesArray release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentPathsArray <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#91;</span>_segmentPathsArray release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentTextsArray <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#91;</span>_segmentTextsArray release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Now modify the <code>generateDrawingInformation:</code> method so that it stores necessary information in this texts array. We draw each segment arc using two &#8216;half&#8217; arcs rather than one in order to find out the midpoint from which we decide where to put the text. We also check to see in which &#8216;quarter&#8217; of the pie-chart view that midpoint falls and offset it by a <code>#defined</code> TEXTPADDING value accordingly, also making sure that the resultant point doesn&#8217;t fall outside of the view&#8217;s bounds:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>generateDrawingInformation
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Keep pointers to the segmentValuesArray and segmentNamesArray</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>cachedSegmentValuesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentValuesArray<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>cachedSegmentNamesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentNamesArray<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get rid of any existing Paths Array</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentPathsArray <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>_segmentPathsArray release<span style="color: #002200;">&#93;</span>;
		_segmentPathsArray <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get rid of any existing Texts Array</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentTextsArray <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>_segmentTextsArray release<span style="color: #002200;">&#93;</span>;
		_segmentTextsArray <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// If there aren't any values to display, we can exit now</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">1</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">return</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get the sum of the amounts and exit if it is zero</span>
	<span style="color: #a61390;">float</span> sumOfAmounts <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> <span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>eachAmountToSum <span style="color: #a61390;">in</span> cachedSegmentValuesArray <span style="color: #002200;">&#41;</span>
		sumOfAmounts <span style="color: #002200;">+=</span> <span style="color: #002200;">&#91;</span>eachAmountToSum floatValue<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> sumOfAmounts <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">return</span>;
&nbsp;
	_segmentPathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	_segmentTextsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #6e371a;">#define PADDINGAROUNDGRAPH 20.0</span>
<span style="color: #6e371a;">#define TEXTPADDING 5.0</span>
&nbsp;
	<span style="color: #a61390;">NSRect</span> viewBounds <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">NSRect</span> graphRect <span style="color: #002200;">=</span> NSInsetRect<span style="color: #002200;">&#40;</span>viewBounds, PADDINGAROUNDGRAPH, PADDINGAROUNDGRAPH<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Make the graphRect square and centred</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> graphRect.size.width <span style="color: #002200;">&amp;</span>gt; graphRect.size.height <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">double</span> sizeDifference <span style="color: #002200;">=</span> graphRect.size.width <span style="color: #002200;">-</span> graphRect.size.height;
		graphRect.size.width <span style="color: #002200;">=</span> graphRect.size.height;
		graphRect.origin.x <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span>sizeDifference <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> graphRect.size.height <span style="color: #002200;">&amp;</span>gt; graphRect.size.width <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">double</span> sizeDifference <span style="color: #002200;">=</span> graphRect.size.height <span style="color: #002200;">-</span> graphRect.size.width;
		graphRect.size.height <span style="color: #002200;">=</span> graphRect.size.width;
		graphRect.origin.y <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span>sizeDifference <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// get NSRects for the different quarters of the pie-chart</span>
	<span style="color: #a61390;">NSRect</span> topLeftRect, topRightRect;
	NSDivideRect<span style="color: #002200;">&#40;</span>viewBounds, <span style="color: #002200;">&amp;</span>amp;topLeftRect, <span style="color: #002200;">&amp;</span>amp;topRightRect, <span style="color: #002200;">&#40;</span>viewBounds.size.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, NSMinXEdge <span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">NSRect</span> bottomLeftRect, bottomRightRect;
	NSDivideRect<span style="color: #002200;">&#40;</span>topLeftRect, <span style="color: #002200;">&amp;</span>amp;topLeftRect, <span style="color: #002200;">&amp;</span>amp;bottomLeftRect, <span style="color: #002200;">&#40;</span>viewBounds.size.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, NSMinYEdge <span style="color: #002200;">&#41;</span>;
	NSDivideRect<span style="color: #002200;">&#40;</span>topRightRect, <span style="color: #002200;">&amp;</span>amp;topRightRect, <span style="color: #002200;">&amp;</span>amp;bottomRightRect, <span style="color: #002200;">&#40;</span>viewBounds.size.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, NSMinYEdge <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Calculate how big a 'unit' is</span>
	<span style="color: #a61390;">float</span> unitSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">360.0</span> <span style="color: #002200;">/</span> sumOfAmounts<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> unitSize <span style="color: #002200;">&amp;</span>gt; <span style="color: #2400d9;">360</span> <span style="color: #002200;">&#41;</span>
		unitSize <span style="color: #002200;">=</span> <span style="color: #2400d9;">360</span>;
&nbsp;
	<span style="color: #a61390;">float</span> radius <span style="color: #002200;">=</span> graphRect.size.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span>;
&nbsp;
	<span style="color: #a61390;">NSPoint</span> midPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span> NSMidX<span style="color: #002200;">&#40;</span>graphRect<span style="color: #002200;">&#41;</span>, NSMidY<span style="color: #002200;">&#40;</span>graphRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Set the text attributes to be used for each textual display</span>
	<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> whiteColor<span style="color: #002200;">&#93;</span>, NSBackgroundColorAttributeName, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span>, NSForegroundColorAttributeName, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFont</span> systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">12</span><span style="color: #002200;">&#93;</span>, NSFontAttributeName, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// cycle through the segmentValues and create the bezier paths</span>
	<span style="color: #11740a; font-style: italic;">// Also add the text details (note we expect the texts' indexes to tie up with the values' indexes)</span>
	<span style="color: #a61390;">float</span> currentDegree <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">unsigned</span> currentIndex;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> currentIndex <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; currentIndex <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span>; currentIndex<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>eachValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray objectAtIndex<span style="color: #002200;">:</span>currentIndex<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">float</span> startDegree <span style="color: #002200;">=</span> currentDegree;
		currentDegree <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>eachValue floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">*</span> unitSize<span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">float</span> endDegree <span style="color: #002200;">=</span> currentDegree;
		<span style="color: #a61390;">float</span> midDegree <span style="color: #002200;">=</span> startDegree <span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>endDegree <span style="color: #002200;">-</span> startDegree<span style="color: #002200;">&#41;</span> <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachSegmentPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBezierPath</span> bezierPath<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachSegmentPath moveToPoint<span style="color: #002200;">:</span>midPoint<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachSegmentPath appendBezierPathWithArcWithCenter<span style="color: #002200;">:</span>midPoint radius<span style="color: #002200;">:</span>radius startAngle<span style="color: #002200;">:</span>startDegree endAngle<span style="color: #002200;">:</span>midDegree clockwise<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">NSPoint</span> textPoint <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachSegmentPath currentPoint<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachSegmentPath appendBezierPathWithArcWithCenter<span style="color: #002200;">:</span>midPoint radius<span style="color: #002200;">:</span>radius startAngle<span style="color: #002200;">:</span>midDegree endAngle<span style="color: #002200;">:</span>endDegree clockwise<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachSegmentPath closePath<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// close path also handles the lines from the midPoint to the start and end of the arc</span>
		<span style="color: #002200;">&#91;</span>eachSegmentPath setLineWidth<span style="color: #002200;">:</span><span style="color: #2400d9;">2.0</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>_segmentPathsArray addObject<span style="color: #002200;">:</span>eachSegmentPath<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Get the text to be displayed, if it exists, and see how big it is</span>
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>eachText <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span>;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>cachedSegmentNamesArray count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>gt; currentIndex <span style="color: #002200;">&#41;</span>
			eachText <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>cachedSegmentNamesArray objectAtIndex<span style="color: #002200;">:</span>currentIndex<span style="color: #002200;">&#93;</span>;
&nbsp;
		NSSize textSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachText sizeWithAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Offset it by TEXTPADDING in direction suitable for whichever quarter of the view it is in</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, topLeftRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.height <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
			textPoint.x <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.width <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, topRightRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.height <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
			textPoint.x <span style="color: #002200;">+=</span> TEXTPADDING;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, bottomLeftRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">+=</span> TEXTPADDING;
			textPoint.x <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.width <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, bottomRightRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">+=</span> TEXTPADDING;
			textPoint.x <span style="color: #002200;">+=</span> TEXTPADDING;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Make sure the point isn't outside the view's bounds</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> textPoint.x <span style="color: #002200;">&amp;</span>lt; viewBounds.origin.x <span style="color: #002200;">&#41;</span>
			textPoint.x <span style="color: #002200;">=</span> viewBounds.origin.x;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span>textPoint.x <span style="color: #002200;">+</span> textSize.width<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>gt; <span style="color: #002200;">&#40;</span>viewBounds.origin.x <span style="color: #002200;">+</span> viewBounds.size.width<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
			textPoint.x <span style="color: #002200;">=</span> viewBounds.origin.x <span style="color: #002200;">+</span> viewBounds.size.width <span style="color: #002200;">-</span> textSize.width;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> textPoint.y <span style="color: #002200;">&amp;</span>lt; viewBounds.origin.y <span style="color: #002200;">&#41;</span>
			textPoint.y <span style="color: #002200;">=</span> viewBounds.origin.y;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span>textPoint.y <span style="color: #002200;">+</span> textSize.height<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>gt; <span style="color: #002200;">&#40;</span>viewBounds.origin.y <span style="color: #002200;">+</span> viewBounds.size.height<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
			textPoint.y <span style="color: #002200;">=</span> viewBounds.origin.y <span style="color: #002200;">+</span> viewBounds.size.height <span style="color: #002200;">-</span> textSize.height;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Finally add the details as a dictionary to our segmentTextsArray.</span>
		<span style="color: #11740a; font-style: italic;">// We include here the textAttributes lest we decide later to e.g. color the texts the same color as the segment fill</span>
		<span style="color: #002200;">&#91;</span>_segmentTextsArray addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>textPoint.x<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointX&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>textPoint.y<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointY&quot;</span>, eachText, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text&quot;</span>, textAttributes, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textAttributes&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Modify the <code>drawRect:</code> method to display the text:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>rect
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> whiteColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// white background</span>
	NSRectFill<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>pathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentPathsArray<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">unsigned</span> count;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>pathsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>pathsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// fill the path with the drawing color for this index</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self colorForIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachPath fill<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// draw a black border around it</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachPath stroke<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>textsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentTextsArray<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>textsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>eachTextDictionary <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>textsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">NSPoint</span> textPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointX&quot;</span><span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointY&quot;</span><span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textAttributes&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text&quot;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>text drawAtPoint<span style="color: #002200;">:</span>textPoint withAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>The view should now display text on the chart:<br />
<img class="aligncenter size-full wp-image-278" title="The Basic Pie Chart now also displaying text" src="http://www.timisted.net/blog/wp-content/uploads/2008/11/basic-pie-chart-with-text.gif" alt="" width="500" height="291" /></p>
<h3>Indicating the Selected Segment(s)</h3>
<p>In order to display which segment is selected, we need to bind to the array controller&#8217;s &#8217;selectionIndexes&#8217;; the view needs to keep its own local copy of these indexes, and expose binding for them:</p>
<ul>
<li>Add an NSIndexSet for the selectionIndexes to the MyPieChartView.h header file, along with a getter and setter:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyPieChartView <span style="color: #002200;">:</span> <span style="color: #400080;">NSView</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentNamesArray;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentValuesArray;
&nbsp;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_segmentPathsArray;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_segmentTextsArray;
&nbsp;
	<span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span>_selectionIndexes;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentNamesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentNamesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentValuesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentValuesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentPathsArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentTextsArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>generateDrawingInformation;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>randomColor;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>colorForIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>index;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>selectionIndexes;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSelectionIndexes<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newIndexes;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Implement these accessor methods and expose the binding:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>selectionIndexes
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>_selectionIndexes retain<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSelectionIndexes<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newIndexes
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>_selectionIndexes <span style="color: #002200;">!=</span> newIndexes<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>amp;<span style="color: #002200;">&amp;</span>amp; <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>_selectionIndexes isEqualToIndexSet<span style="color: #002200;">:</span>newIndexes<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>_selectionIndexes release<span style="color: #002200;">&#93;</span>;
		_selectionIndexes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>newIndexes copy<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>self generateDrawingInformation<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>self setNeedsDisplayInRect<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self visibleRect<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>initialize
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>In MyDocument.m, bind the selectionIndexes to the array controller:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>windowControllerDidLoadNib<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSWindowController</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>windowController
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>super windowControllerDidLoadNib<span style="color: #002200;">:</span>windowController<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.name&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.amount&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>We also need to modify the <code>generateDrawingInformation:</code> method to offset the selected segment (and its text) by a <code>#defined</code> amount:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>generateDrawingInformation
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Keep pointers to the segmentValuesArray and segmentNamesArray</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>cachedSegmentValuesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentValuesArray<span style="color: #002200;">&#93;</span>;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>cachedSegmentNamesArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentNamesArray<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get rid of any existing Paths Array</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentPathsArray <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>_segmentPathsArray release<span style="color: #002200;">&#93;</span>;
		_segmentPathsArray <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get rid of any existing Texts Array</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> _segmentTextsArray <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>_segmentTextsArray release<span style="color: #002200;">&#93;</span>;
		_segmentTextsArray <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// If there aren't any values to display, we can exit now</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">1</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">return</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Get the sum of the amounts and exit if it is zero</span>
	<span style="color: #a61390;">float</span> sumOfAmounts <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> <span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>eachAmountToSum <span style="color: #a61390;">in</span> cachedSegmentValuesArray <span style="color: #002200;">&#41;</span>
		sumOfAmounts <span style="color: #002200;">+=</span> <span style="color: #002200;">&#91;</span>eachAmountToSum floatValue<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> sumOfAmounts <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">return</span>;
&nbsp;
	_segmentPathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	_segmentTextsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> initWithCapacity<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span>selectionIndexes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self selectionIndexes<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">BOOL</span> shouldOffsetSelectedSegment <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>selectionIndexes count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> ? <span style="color: #a61390;">YES</span> <span style="color: #002200;">:</span> <span style="color: #a61390;">NO</span>;
&nbsp;
<span style="color: #6e371a;">#define PADDINGAROUNDGRAPH 20.0</span>
<span style="color: #6e371a;">#define TEXTPADDING 5.0</span>
<span style="color: #6e371a;">#define SELECTEDSEGMENTOFFSET 5.0</span>
&nbsp;
	<span style="color: #a61390;">NSRect</span> viewBounds <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">NSRect</span> graphRect <span style="color: #002200;">=</span> NSInsetRect<span style="color: #002200;">&#40;</span>viewBounds, PADDINGAROUNDGRAPH, PADDINGAROUNDGRAPH<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Make the graphRect square and centred</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> graphRect.size.width <span style="color: #002200;">&amp;</span>gt; graphRect.size.height <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">double</span> sizeDifference <span style="color: #002200;">=</span> graphRect.size.width <span style="color: #002200;">-</span> graphRect.size.height;
		graphRect.size.width <span style="color: #002200;">=</span> graphRect.size.height;
		graphRect.origin.x <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span>sizeDifference <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> graphRect.size.height <span style="color: #002200;">&amp;</span>gt; graphRect.size.width <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">double</span> sizeDifference <span style="color: #002200;">=</span> graphRect.size.height <span style="color: #002200;">-</span> graphRect.size.width;
		graphRect.size.height <span style="color: #002200;">=</span> graphRect.size.width;
		graphRect.origin.y <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span>sizeDifference <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// get NSRects for the different quarters of the pie-chart</span>
	<span style="color: #a61390;">NSRect</span> topLeftRect, topRightRect;
	NSDivideRect<span style="color: #002200;">&#40;</span>viewBounds, <span style="color: #002200;">&amp;</span>amp;topLeftRect, <span style="color: #002200;">&amp;</span>amp;topRightRect, <span style="color: #002200;">&#40;</span>viewBounds.size.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, NSMinXEdge <span style="color: #002200;">&#41;</span>;
	<span style="color: #a61390;">NSRect</span> bottomLeftRect, bottomRightRect;
	NSDivideRect<span style="color: #002200;">&#40;</span>topLeftRect, <span style="color: #002200;">&amp;</span>amp;topLeftRect, <span style="color: #002200;">&amp;</span>amp;bottomLeftRect, <span style="color: #002200;">&#40;</span>viewBounds.size.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, NSMinYEdge <span style="color: #002200;">&#41;</span>;
	NSDivideRect<span style="color: #002200;">&#40;</span>topRightRect, <span style="color: #002200;">&amp;</span>amp;topRightRect, <span style="color: #002200;">&amp;</span>amp;bottomRightRect, <span style="color: #002200;">&#40;</span>viewBounds.size.height <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>, NSMinYEdge <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Calculate how big a 'unit' is</span>
	<span style="color: #a61390;">float</span> unitSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #2400d9;">360.0</span> <span style="color: #002200;">/</span> sumOfAmounts<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> unitSize <span style="color: #002200;">&amp;</span>gt; <span style="color: #2400d9;">360</span> <span style="color: #002200;">&#41;</span>
		unitSize <span style="color: #002200;">=</span> <span style="color: #2400d9;">360</span>;
&nbsp;
	<span style="color: #a61390;">float</span> radius <span style="color: #002200;">=</span> graphRect.size.width <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span>;
&nbsp;
	<span style="color: #a61390;">NSPoint</span> midPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span> NSMidX<span style="color: #002200;">&#40;</span>graphRect<span style="color: #002200;">&#41;</span>, NSMidY<span style="color: #002200;">&#40;</span>graphRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Set the text attributes to be used for each textual display</span>
	<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> whiteColor<span style="color: #002200;">&#93;</span>, NSBackgroundColorAttributeName, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span>, NSForegroundColorAttributeName, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFont</span> systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">12</span><span style="color: #002200;">&#93;</span>, NSFontAttributeName, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// cycle through the segmentValues and create the bezier paths</span>
	<span style="color: #11740a; font-style: italic;">// Also add the text details (note we expect the texts' indexes to tie up with the values' indexes)</span>
	<span style="color: #a61390;">float</span> currentDegree <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">unsigned</span> currentIndex;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> currentIndex <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; currentIndex <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray count<span style="color: #002200;">&#93;</span>; currentIndex<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>eachValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>cachedSegmentValuesArray objectAtIndex<span style="color: #002200;">:</span>currentIndex<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">float</span> startDegree <span style="color: #002200;">=</span> currentDegree;
		currentDegree <span style="color: #002200;">+=</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>eachValue floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">*</span> unitSize<span style="color: #002200;">&#41;</span>;
		<span style="color: #a61390;">float</span> endDegree <span style="color: #002200;">=</span> currentDegree;
		<span style="color: #a61390;">float</span> midDegree <span style="color: #002200;">=</span> startDegree <span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>endDegree <span style="color: #002200;">-</span> startDegree<span style="color: #002200;">&#41;</span> <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachSegmentPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBezierPath</span> bezierPath<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachSegmentPath moveToPoint<span style="color: #002200;">:</span>midPoint<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachSegmentPath appendBezierPathWithArcWithCenter<span style="color: #002200;">:</span>midPoint radius<span style="color: #002200;">:</span>radius startAngle<span style="color: #002200;">:</span>startDegree endAngle<span style="color: #002200;">:</span>midDegree clockwise<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">NSPoint</span> textPoint <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachSegmentPath currentPoint<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachSegmentPath appendBezierPathWithArcWithCenter<span style="color: #002200;">:</span>midPoint radius<span style="color: #002200;">:</span>radius startAngle<span style="color: #002200;">:</span>midDegree endAngle<span style="color: #002200;">:</span>endDegree clockwise<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachSegmentPath closePath<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// close path also handles the lines from the midPoint to the start and end of the arc</span>
		<span style="color: #002200;">&#91;</span>eachSegmentPath setLineWidth<span style="color: #002200;">:</span><span style="color: #2400d9;">2.0</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Check to see whether we should offset this segment if it's currently selected in the array controller</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> shouldOffsetSelectedSegment <span style="color: #002200;">&amp;</span>amp;<span style="color: #002200;">&amp;</span>amp; <span style="color: #002200;">&#91;</span>selectionIndexes containsIndex<span style="color: #002200;">:</span>currentIndex<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #a61390;">float</span> differenceRatio <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>SELECTEDSEGMENTOFFSET <span style="color: #002200;">/</span> radius<span style="color: #002200;">&#41;</span> <span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span>SELECTEDSEGMENTOFFSET <span style="color: #002200;">/</span> <span style="color: #002200;">&#40;</span>endDegree <span style="color: #002200;">-</span> startDegree<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
			<span style="color: #a61390;">float</span> diffY <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>textPoint.y <span style="color: #002200;">-</span> midPoint.y<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> differenceRatio;
			<span style="color: #a61390;">float</span> diffX <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>textPoint.x <span style="color: #002200;">-</span> midPoint.x<span style="color: #002200;">&#41;</span> <span style="color: #002200;">*</span> differenceRatio;
&nbsp;
			<span style="color: #400080;">NSAffineTransform</span> <span style="color: #002200;">*</span>transform <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSAffineTransform</span> transform<span style="color: #002200;">&#93;</span>;
&nbsp;
			<span style="color: #002200;">&#91;</span>transform translateXBy<span style="color: #002200;">:</span>diffX yBy<span style="color: #002200;">:</span>diffY<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>eachSegmentPath transformUsingAffineTransform<span style="color: #002200;">:</span> transform<span style="color: #002200;">&#93;</span>;
&nbsp;
			textPoint <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>transform transformPoint<span style="color: #002200;">:</span>textPoint<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #002200;">&#91;</span>_segmentPathsArray addObject<span style="color: #002200;">:</span>eachSegmentPath<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Get the text to be displayed, if it exists, and see how big it is</span>
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>eachText <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span>;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>cachedSegmentNamesArray count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>gt; currentIndex <span style="color: #002200;">&#41;</span>
			eachText <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>cachedSegmentNamesArray objectAtIndex<span style="color: #002200;">:</span>currentIndex<span style="color: #002200;">&#93;</span>;
&nbsp;
		NSSize textSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachText sizeWithAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Offset it by TEXTPADDING in direction suitable for whichever quarter of the view it is in</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, topLeftRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.height <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
			textPoint.x <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.width <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, topRightRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.height <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
			textPoint.x <span style="color: #002200;">+=</span> TEXTPADDING;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, bottomLeftRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">+=</span> TEXTPADDING;
			textPoint.x <span style="color: #002200;">-=</span> <span style="color: #002200;">&#40;</span>textSize.width <span style="color: #002200;">+</span> TEXTPADDING<span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> NSPointInRect<span style="color: #002200;">&#40;</span>textPoint, bottomRightRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			textPoint.y <span style="color: #002200;">+=</span> TEXTPADDING;
			textPoint.x <span style="color: #002200;">+=</span> TEXTPADDING;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Make sure the point isn't outside the view's bounds</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> textPoint.x <span style="color: #002200;">&amp;</span>lt; viewBounds.origin.x <span style="color: #002200;">&#41;</span>
			textPoint.x <span style="color: #002200;">=</span> viewBounds.origin.x;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span>textPoint.x <span style="color: #002200;">+</span> textSize.width<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>gt; <span style="color: #002200;">&#40;</span>viewBounds.origin.x <span style="color: #002200;">+</span> viewBounds.size.width<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
			textPoint.x <span style="color: #002200;">=</span> viewBounds.origin.x <span style="color: #002200;">+</span> viewBounds.size.width <span style="color: #002200;">-</span> textSize.width;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> textPoint.y <span style="color: #002200;">&amp;</span>lt; viewBounds.origin.y <span style="color: #002200;">&#41;</span>
			textPoint.y <span style="color: #002200;">=</span> viewBounds.origin.y;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span>textPoint.y <span style="color: #002200;">+</span> textSize.height<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>gt; <span style="color: #002200;">&#40;</span>viewBounds.origin.y <span style="color: #002200;">+</span> viewBounds.size.height<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
			textPoint.y <span style="color: #002200;">=</span> viewBounds.origin.y <span style="color: #002200;">+</span> viewBounds.size.height <span style="color: #002200;">-</span> textSize.height;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Finally add the details as a dictionary to our segmentTextsArray.</span>
		<span style="color: #11740a; font-style: italic;">// We include here the textAttributes lest we decide later to e.g. color the texts the same color as the segment fill</span>
		<span style="color: #002200;">&#91;</span>_segmentTextsArray addObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObjectsAndKeys<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>textPoint.x<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointX&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>textPoint.y<span style="color: #002200;">&#93;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointY&quot;</span>, eachText, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text&quot;</span>, textAttributes, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textAttributes&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>We&#8217;ll also modify the <code>drawRect:</code> method so that it fills selected segments with blue rather than the usual colour:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>rect
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> whiteColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// white background</span>
	NSRectFill<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>pathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentPathsArray<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">unsigned</span> count;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>pathsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>pathsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// fill the path with the drawing color for this index unless it's selected</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self selectionIndexes<span style="color: #002200;">&#93;</span> containsIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
			<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blueColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">else</span>
			<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self colorForIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachPath fill<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// draw a black border around it</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachPath stroke<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>textsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentTextsArray<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>textsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>eachTextDictionary <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>textsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">NSPoint</span> textPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointX&quot;</span><span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointY&quot;</span><span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textAttributes&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text&quot;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>text drawAtPoint<span style="color: #002200;">:</span>textPoint withAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>Now the view should display the selected segment as desired, updating when the user clicks on a different line in the table view:<br />
<img class="aligncenter size-full wp-image-280" title="Pie Chart highlighting selected segment" src="http://www.timisted.net/blog/wp-content/uploads/2008/11/pie-chart-with-selection-in.gif" alt="" width="500" height="291" /></p>
<p>If you wish, go into Interface Builder and change the selection type of the table view to &#8216;Multiple&#8217;. You should find that you can shift-click multiple items in the table view and they will show as desired in the chart view.</p>
<h3>Resizing Issues</h3>
<p>Since we cache the array of segment paths, only updating it when the arrays of data are changed, you might have noticed that the view doesn&#8217;t behave very well when we resize it. Assuming you set the view to resize with its window, whilst the white background will enlarge, the actual paths won&#8217;t change. This is easy to rectify:</p>
<ul>
<li>Simply check in the <code>drawRect:</code> method whether we are in the middle of a &#8216;liveResize&#8217; and, if so, update the drawing information arrays:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSRect</span><span style="color: #002200;">&#41;</span>rect
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> whiteColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// white background</span>
	NSRectFill<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>self inLiveResize<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>self generateDrawingInformation<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>pathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentPathsArray<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">unsigned</span> count;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>pathsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>pathsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// fill the path with the drawing color for this index unless it's selected</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self selectionIndexes<span style="color: #002200;">&#93;</span> containsIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
			<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blueColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">else</span>
			<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self colorForIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>eachPath fill<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// draw a black border around it</span>
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSColor</span> blackColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>eachPath stroke<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>textsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentTextsArray<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>textsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>eachTextDictionary <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>textsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">NSPoint</span> textPoint <span style="color: #002200;">=</span> NSMakePoint<span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointX&quot;</span><span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textPointY&quot;</span><span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>textAttributes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;textAttributes&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eachTextDictionary valueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;text&quot;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>text drawAtPoint<span style="color: #002200;">:</span>textPoint withAttributes<span style="color: #002200;">:</span>textAttributes<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>The view will now &#8216;live resize&#8217; itself as would be expected.</p>
<h3>Changing the Selection Indexes when the user clicks a Segment</h3>
<p>We have already set up the view to display a segment &#8217;selected&#8217; in the table view, but it would be nice if the user could also click on a segment to select it. Given that we store an array of segment paths inside the view, it should be pretty easy to &#8216;hit-test&#8217; each path to determine which segment was clicked.</p>
<ul>
<li>To receive clicks in our view, we need to override the <code>acceptsFirstResponder:</code> method for our view to return YES so add the following to MyPieChartView.m:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>acceptsFirstResponder
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Next we&#8217;ll implement a method to determine which segment was clicked so add the method declaration to MyPieChartView.h:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>objectIndexForPoint<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSPoint</span><span style="color: #002200;">&#41;</span>thePoint;</pre></div></div>

</li>
<li>And implement it in MyPieChartView.m:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>objectIndexForPoint<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSPoint</span><span style="color: #002200;">&#41;</span>thePoint
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>cachedPathsArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self segmentPathsArray<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">int</span> count;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span> count <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; count <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>cachedPathsArray count<span style="color: #002200;">&#93;</span>; count<span style="color: #002200;">++</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSBezierPath</span> <span style="color: #002200;">*</span>eachPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>cachedPathsArray objectAtIndex<span style="color: #002200;">:</span>count<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>eachPath containsPoint<span style="color: #002200;">:</span>thePoint <span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #a61390;">return</span> count;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// if control reaches here, no segment contained the point so return -1</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">1</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>We also need to implement a <code>mouseUp:</code> method that will be called when the user has clicked on a segment. Expected behavior varies depending on whether the user is holding down a modifier key at the time the segment was clicked; holding down the command key should either add or remove the clicked segment to the current selection; holding down the shift key and clicking should select a range of segments between the existing selection and the clicked segment:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>mouseUp<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSEvent</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theEvent
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">int</span> index <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self objectIndexForPoint<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self convertPoint<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>theEvent locationInWindow<span style="color: #002200;">&#93;</span> fromView<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #400080;">NSMutableIndexSet</span> <span style="color: #002200;">*</span>newSelectionIndexes <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self selectionIndexes<span style="color: #002200;">&#93;</span> mutableCopy<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>theEvent modifierFlags<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>amp; NSCommandKeyMask <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// Add or remove the clicked segment</span>
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>newSelectionIndexes containsIndex<span style="color: #002200;">:</span>index<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #002200;">&#91;</span>newSelectionIndexes removeIndex<span style="color: #002200;">:</span>index<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #002200;">&#91;</span>newSelectionIndexes addIndex<span style="color: #002200;">:</span>index<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>theEvent modifierFlags<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&amp;</span>amp; NSShiftKeyMask <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// Add range to selection</span>
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>newSelectionIndexes count<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #002200;">&#91;</span>newSelectionIndexes addIndex<span style="color: #002200;">:</span>index<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span>
		<span style="color: #002200;">&#123;</span>
			<span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">int</span> origin <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>index <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>newSelectionIndexes lastIndex<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> ? index <span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>newSelectionIndexes lastIndex<span style="color: #002200;">&#93;</span>;
			<span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">int</span> length <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>index <span style="color: #002200;">&amp;</span>lt; <span style="color: #002200;">&#91;</span>newSelectionIndexes lastIndex<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> ? <span style="color: #002200;">&#91;</span>newSelectionIndexes lastIndex<span style="color: #002200;">&#93;</span> <span style="color: #002200;">-</span> index <span style="color: #002200;">:</span> index <span style="color: #002200;">-</span> <span style="color: #002200;">&#91;</span>newSelectionIndexes lastIndex<span style="color: #002200;">&#93;</span>;
&nbsp;
			length<span style="color: #002200;">++</span>;
			<span style="color: #002200;">&#91;</span>newSelectionIndexes addIndexesInRange<span style="color: #002200;">:</span>NSMakeRange<span style="color: #002200;">&#40;</span>origin, length<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> <span style="color: #11740a; font-style: italic;">// the user just clicked without modifier keys so simply select the segment</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>newSelectionIndexes removeAllIndexes<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> index <span style="color: #002200;">&amp;</span>gt;<span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&#41;</span>
			<span style="color: #002200;">&#91;</span>newSelectionIndexes addIndex<span style="color: #002200;">:</span>index<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #002200;">&#91;</span>self setSelectionIndexes<span style="color: #002200;">:</span>newSelectionIndexes<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>newSelectionIndexes release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Finally we need to bind the selectionIndexes of our view to the selectionIndexes of the array controller so that it updates the table view as well. Add this last binding in MyDocument.m:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>windowControllerDidLoadNib<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSWindowController</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>windowController
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>super windowControllerDidLoadNib<span style="color: #002200;">:</span>windowController<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.name&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.amount&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>segmentsArrayController bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> toObject<span style="color: #002200;">:</span>pieChartView withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>When the user clicks on a segment in our chart view, that segment should become selected and also change the selection in the table view; modifier keys should also affect the way that segments become selected.</p>
<h3>One Last Binding</h3>
<p>Just in case you haven&#8217;t had enough bindings yet, we&#8217;ll add one final feature to the view &#8211; the ability to rotate the chart, perhaps so the user can better fit the related text or place one segment in the middle etc.</p>
<ul>
<li>We&#8217;ll start by adding a &#8216;rotationAmount&#8217; attribute to our view, along with the necessary accessors in MyPieChartView.h:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyPieChartView <span style="color: #002200;">:</span> <span style="color: #400080;">NSView</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentNamesArray;
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_segmentValuesArray;
&nbsp;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_segmentPathsArray;
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>_segmentTextsArray;
&nbsp;
	<span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span>_selectionIndexes;
&nbsp;
	<span style="color: #a61390;">float</span> _rotationAmount;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentNamesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentNamesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentValuesArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSegmentValuesArray<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newArray;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentPathsArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>segmentTextsArray;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>generateDrawingInformation;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>randomColor;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSColor</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>colorForIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">unsigned</span><span style="color: #002200;">&#41;</span>index;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>selectionIndexes;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setSelectionIndexes<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newIndexes;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span>objectIndexForPoint<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">NSPoint</span><span style="color: #002200;">&#41;</span>thePoint;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setRotationAmount<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>rotationAmount;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Expose the binding for the rotationAmount attribute and add the accessor methods to MyPieChartView.m:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>initialize
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;rotationAmount&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setRotationAmount<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>value floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">!=</span> _rotationAmount <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;rotationAmount&quot;</span><span style="color: #002200;">&#93;</span>;
		_rotationAmount <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>value floatValue<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;rotationAmount&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>self generateDrawingInformation<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>self setNeedsDisplayInRect<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self visibleRect<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>rotationAmount
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>_rotationAmount<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>To make use of the rotation amount, we simply need to modify our drawing code to set the &#8216;currentDegree&#8217; to the rotation amount so find and change this in <code>generateDrawingInformation:</code>:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	<span style="color: #11740a; font-style: italic;">// cycle through the segmentValues and create the bezier paths</span>
	<span style="color: #11740a; font-style: italic;">// Also add the text details (note we expect the texts' indexes to tie up with the values' indexes)</span>
	<span style="color: #a61390;">float</span> currentDegree <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self rotationAmount<span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">unsigned</span> currentIndex;</pre></div></div>

</li>
<li>Next we need to have a control to rotate the graph so open up MyDocument.xib in InterfaceBuilder and add a horizontal slider control; set its minimum and maximum values to 0.0 and 360.0, and its current value to 0.0; tick the &#8216;Continuous&#8217; box in the inspector. My interface looks like this:<br />
<img class="aligncenter size-full wp-image-284" title="The new slider control in Interface Builder" src="http://www.timisted.net/blog/wp-content/uploads/2008/11/the-new-slider-control.gif" alt="" width="500" height="316" /></li>
<li>Back in Xcode, add an IBOutlet for the new slider to MyDocument.h:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyDocument <span style="color: #002200;">:</span> <span style="color: #400080;">NSPersistentDocument</span>
<span style="color: #002200;">&#123;</span>
	IBOutlet <span style="color: #400080;">NSArrayController</span> <span style="color: #002200;">*</span>segmentsArrayController;
	IBOutlet <span style="color: #400080;">NSView</span> <span style="color: #002200;">*</span>pieChartView;
	IBOutlet <span style="color: #400080;">NSSlider</span> <span style="color: #002200;">*</span>sliderRotationControl;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Connect the outlet to the control in Interface Builder.</li>
<li>In MyDocument.m, bind the rotation control&#8217;s value to the rotationAmount on the chart view:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>windowControllerDidLoadNib<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSWindowController</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>windowController
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>super windowControllerDidLoadNib<span style="color: #002200;">:</span>windowController<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentNamesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.name&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;segmentValuesArray&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;arrangedObjects.amount&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>pieChartView bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> toObject<span style="color: #002200;">:</span>segmentsArrayController withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>segmentsArrayController bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> toObject<span style="color: #002200;">:</span>pieChartView withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;selectionIndexes&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>sliderRotationControl bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;value&quot;</span> toObject<span style="color: #002200;">:</span>pieChartView withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;rotationAmount&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<h3>That&#8217;s It!</h3>
<p>At this point you should have a pretty functional, bindable pie-chart view. For such a view it would make sense to create an Interface Builder palette so that the bindings can be setup visually in Interface Builder rather having to do it programmatically; this may well be a topic for a future post!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.timisted.net/blog/archive/a-bindable-custom-nsview-subclass/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>On Cocoa Bindings – &#8220;Let Us Break Their Bonds Asunder&#8221;</title>
		<link>http://www.timisted.net/blog/archive/on-cocoa-bindings/</link>
		<comments>http://www.timisted.net/blog/archive/on-cocoa-bindings/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 17:38:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Bindings]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://www.timisted.net/blog/?p=200</guid>
		<description><![CDATA[Chances are, if you&#8217;ve developed any kind of Cocoa application, you&#8217;ve probably made use of Cocoa Bindings in one way or another, particularly when it comes to Core Data. The technology is &#8216;key&#8217; to so many of the underlying frameworks but it is a technology that tends to be often-used but frequently not fully understood.
If [...]]]></description>
			<content:encoded><![CDATA[<p>Chances are, if you&#8217;ve developed any kind of Cocoa application, you&#8217;ve probably made use of Cocoa Bindings in one way or another, particularly when it comes to Core Data. The technology is &#8216;key&#8217; to so many of the underlying frameworks but it is a technology that tends to be often-used but frequently not fully understood.</p>
<p>If you are someone who happily binds Array Controllers and View objects together using Interface Builder but have always wondered discretely exactly how the magic behind the scenes happens, or if you just don&#8217;t quite comprehend the difference between &#8216;KVC&#8217; and &#8216;KVO&#8217; (or perhaps can never quite remember what they stand for&#8230;), this article might help you out. In it, I&#8217;ll attempt to demonstrate a &#8216;faux-bindings&#8217; methodology that connects a value from one object with a value from another object, but without using the actual Apple Cocoa Bindings technology. The bare-bones of how the &#8216;faux binding&#8217; works will be there for all to see; while the internal workings of Cocoa Bindings are a little different, this will hopefully at least show you what, how and why certain things are required for the real Cocoa Bindings to work successfully.</p>
<p><span id="more-200"></span></p>
<h3>Basic Principles</h3>
<p>The simple premise of our &#8216;faux-bindings&#8217; technology is that we link two objects together such that when a certain value changes in the first, the other changes its linked value to match. One way to accomplish this would be to have the objects (A and B) maintain links to each other such that when object A&#8217;s value changes, A can call a method on object B to update B&#8217;s value. This is all very well for two simple objects working in one direction but if you have multiple objects all bound together, each with a link to all the others, things can quickly become complicated, leak memory all over the place, and be difficult to maintain.</p>
<h4>Notifications</h4>
<p>Notifications are another wonderful, often underused, technology that allow communication between objects without those objects knowing anything about each other. Using a Notification Center, objects (e.g. objects B and C) can request notifications when specified types of messages are posted by other objects (e.g. object A). So, when object A decides to post a message, the notification center looks at the list of objects who want to receive such messages, and goes and tells those objects B and C that object A posted a notification. If object A is later released from memory but B and C are still listening for notifications, it doesn&#8217;t matter &#8212; they maintain no direct link to object A so they simply go on waiting without receiving anything. Similarly, if object C is released before object A posts a notification, again it doesn&#8217;t matter &#8212; A has no knowledge or worry about who receives the notifications as they go through a third party, a notification center.</p>
<p>To post a notification, an object can simply do this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> postNotificationName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;helloIChanged&quot;</span> object<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>This tells the default notification center (helpfully already created ready for you to use so you don&#8217;t need to worry about it) to distribute the &#8216;helloIChanged&#8217; notification to anyone who listens for it.</p>
<p>To specify that you want to listen for such a notification, you can do this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self
	                                         selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>receivedNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span>
	                                             name<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;helloIChanged&quot;</span>
	                                           object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>This code tells the notification center that you want to receive all &#8216;helloIChanged&#8217; notifications, and when such a notification is received, your <code>receivedNotification:</code> method should be called. The <code>object</code> parameter of this method allows you to specify that you only want to receive notifications from a single object instance rather than from any object. If you wished, you could say &#8220;name:nil&#8221; but instead limit your interest to a specific object so that <em>any</em> notification posted by that object (e.g. @&#8221;helloIChanged&#8221; or @&#8221;helloSomethingElseHappened&#8221;) would cause the notification centre to call your <code>receivedNotification:</code> method.</p>
<h3>The Sample Project</h3>
<p>We&#8217;re going to create a very simple sample project for this article. If you wish to download some sample code for our Faux Binding, you can do so from here: <a href='http://www.timisted.net/blog/wp-content/uploads/2008/08/fauxbindings.zip'>fauxbindings.zip</a>.</p>
<ul>
<li>In Xcode, create a new &#8216;Cocoa Application&#8217; project called &#8216;FauxBindings&#8217;.</li>
<li>Right-click on the &#8216;Classes&#8217; group and Add -> New File&#8230; to add a basic Objective-C class file called &#8220;BindController.m&#8221;; make sure the &#8216;Also create &#8220;BindController.h&#8221;&#8216; box is ticked, and click Finish. If you don&#8217;t have a two-button mouse, read &#8216;right-click&#8217; as &#8216;CTRL-click&#8217; throughout this article.</li>
<li>We are going to need an <code>IBOutlet</code> for an <code>NSTextField</code> and a <code>NSString</code> object. We&#8217;re also going to need an <code>IBAction</code> method to display the string using <code>NSLog()</code>. So, make your BindController.h file look like this:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;cocoa/Cocoa.h&gt;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@interface</span> BindController <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	IBOutlet <span style="color: #400080;">NSTextField</span> <span style="color: #002200;">*</span>theTextField;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>theString;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>showTheStringInLog<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Next open MainMenu.nib in Xcode and open up the &#8216;Window&#8217; object. Add an <code>NSTextField</code> and an <code>NSButton</code> called &#8216;Log theString&#8217; so that it looks something like this:<br />
<img src="http://www.timisted.net/blog/wp-content/uploads/2008/08/fauxbindingssimplewindow.gif" alt="The FauxBindings main Window layout in Interface Builder" title="fauxbindingssimplewindow" width="348" height="179" class="size-full wp-image-205" /><br />
Not the most exciting window ever created but functional enough for this demonstration.</li>
<li>From the Interface Builder Library tool drag out an NSObject (that&#8217;s the blue cube thing) into your MainMenu.nib window. Click this object and in the Inspector, use the Identity tab to specify that the object&#8217;s class is BindController. By adding a BindController object in this way to our nib file, an instance of the BindController class will be created when the nib is loaded; there is no other code anywhere in the project that does a [[BindController alloc] init] etc. It&#8217;s all handled for you.</li>
<li>Next right-click on the cube and connect its <code>theTextField</code> outlet to our text field in the window, and connect the <code>showTheStringInLog:</code> method to the &#8220;Log theString&#8221; button:<br />
<img src="http://www.timisted.net/blog/wp-content/uploads/2008/08/fauxbindingsconnections.gif" alt="Connections between the BindController object and the FauxBindings main window" title="fauxbindingsconnections" width="347" height="401" class="size-full wp-image-206" /></li>
<li>Return now to Xcode and open up the BindController.m class file. We need to add the code for the <code>showTheStringInLog:</code> method so write that now:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;BindController.h&quot;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@implementation</span> BindController
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>showTheStringInLog<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Value of theString is: %@&quot;</span>, theString<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Make sure the Console window is open (listed under the &#8216;Run&#8217; menu, or CMD-SHIFT-R) and then build and run your application. Press the button. No surprises here, you&#8217;ll see that the &#8220;Value of theString is: (null)&#8221; because we haven&#8217;t set theString to anything yet.</li>
</ul>
<h4>Establishing a Link</h4>
<p>Now we need to setup a <code>FauxBind:</code> method that can be used to link a specified value of one object to a specified value on another. We&#8217;ll use it to link the <code>stringValue</code> on the text field to our BindController&#8217;s <code>theString</code> object. We need to call this method when the application first loads so that the faux bindings are ready for the user input.</p>
<ul>
<li>Go back to Interface Builder and click on the Application object in the nib file. Use the Connections Inspector panel to connect the &#8216;delegate&#8217; outlet to the Bind Controller object. This will allow our object to respond to the &#8216;awakeFromNib&#8217; method that is called when the application has loaded the nib file into memory.</li>
<li>Open BindController.h in Xcode and add two objects to the controller &#8211; we&#8217;ll need an <code>originatingObject</code> and a <code>destinationObject</code>, together with two strings for that hold the names of the values to which we&#8217;ll be binding. Also add a FauxBind: method that will set up our faux binding between those objects. Finally, we&#8217;ll be using notifications so declare a method to be called when a notification is received:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;cocoa/Cocoa.h&gt;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@interface</span> BindController <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	IBOutlet <span style="color: #400080;">NSTextField</span> <span style="color: #002200;">*</span>theTextField;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>theString;
&nbsp;
	<span style="color: #a61390;">id</span> originatingObject;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>originatingValueName;
	<span style="color: #a61390;">id</span> destinationObject;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>destinationValueName;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>showTheStringInLog<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>FauxBindObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>destObject valueName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>destValueName toObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>origObject valueName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>origValueName;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>receivedNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Next open BindController.m and add an awakeFromNib: method after the showTheStringInLog: method. This method says we&#8217;re going to bind a value called &#8220;theString&#8221; in our own object, to a value called &#8220;stringValue&#8221; on theTextField:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>awakeFromNib
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self FauxBindObject<span style="color: #002200;">:</span>self valueName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span> toObject<span style="color: #002200;">:</span>theTextField valueName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;stringValue&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>We can also write the first steps of this faux bind method. Initially, let&#8217;s just see what notifications are being posted by theTextField and log them to the console:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>FauxBindObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>destObject valueName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>destValueName toObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>origObject valueName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>origValueName
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self
	                                         selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>receivedNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span>
	                                             name<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span>
	                                           object<span style="color: #002200;">:</span>origObject<span style="color: #002200;">&#93;</span>;
&nbsp;
	originatingObject <span style="color: #002200;">=</span> origObject;
	originatingValueName <span style="color: #002200;">=</span> origValueName;
	destinationObject <span style="color: #002200;">=</span> destObject;
	destinationValueName <span style="color: #002200;">=</span> destValueName;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>And, of course, we need to code the <code>receivedNotification:</code> method that is called when a notification is received; here we&#8217;ll simply log the whole notification to the console so that we can examine its contents:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>receivedNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Received a notification: %@&quot;</span>, notification<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Again, make sure the console is visible, then build and run the application.</li>
</ul>
<p>When you click, type and tab out of the text field, you&#8217;ll see a whole string of messages sent to the console, indicating a whole chain of notifications. You&#8217;ll see an <code>NSControlTextDidBeginEditingNotification</code> at the start, then an <code>NSControlTextDidChangeNotification</code> sent every time the text changed, then (assuming you pressed tab or enter to commit the text field), you&#8217;ll see an <code>NSControlTextDidEndEditingNotification</code>.</p>
<p>We want our <code>theString</code> value to change when the text field value is committed by the user pressing tab or enter so:</p>
<ul>
<li>Change the <code>FauxBindObject:</code> method to listen only for NSControlTextDidEndEditingNotifications:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>FauxBindObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>destObject valueName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>destValueName toObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>origObject valueName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>origValueName
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self
	                                         selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>receivedNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span>
	                                             name<span style="color: #002200;">:</span>NSControlTextDidEndEditingNotification
	                                           object<span style="color: #002200;">:</span>origObject<span style="color: #002200;">&#93;</span>;
&nbsp;
	originatingObject <span style="color: #002200;">=</span> origObject;
	originatingValueName <span style="color: #002200;">=</span> origValueName;
	destinationObject <span style="color: #002200;">=</span> destObject;
	destinationValueName <span style="color: #002200;">=</span> destValueName;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>This time when you build and run, you&#8217;ll see that we only receive a notification when the text field commits editing on return or tab.</li>
</ul>
<p>Having found out when the originating object changes, we need to be able to obtain the value of that object before updating the value of the destination object.</p>
<h4>A Note about Selectors</h4>
<p>Feel free to skip this part but it might be helpful to know a little about method calls in Objective-C. An in-depth discussion about the way that Objective-C handles such method calls between objects is outside the scope of this article. But, if you are comfortable with bog-standard C or C++, you will know all about functions. Functions look something like this <code>FunctionName( variable1, variable2)</code>. When you call a <strong>method</strong> in Objective-C with code like this: <code>[self doSomethingWith:anObject]</code>, the compiler is actually translating this method call into a call to the <code>objc_msgSend()</code> <strong>function</strong>. This function takes arguments that specify which &#8216;object&#8217; is receiving the method call, and a &#8217;selector&#8217; which specifies the name of a method that is being called.</p>
<p>A discussion of how selectors work is again outside the scope of this article but hold in your mind that the messaging function finds out which methods you&#8217;re calling (and the calls to those methods) at <strong>runtime</strong>. This means your code can also refer to methods &#8216;indirectly&#8217; by using selectors, perhaps in order to find out information about them. We used a selector earlier in our call to register for notifications, saying that our &#8216;receivedNotification:&#8217; method should be called. There is a method on any NSObject subclass called &#8216;respondsToSelector:&#8217; which makes it possible to find out whether an object responds to a particular selector (aka method). So, let&#8217;s assume for now that the Notification Center will ask our object to make sure it can actually receive the method call we specified. It is then also possible to actually call a method using a selector with the &#8216;performSelector:&#8217; NSObject method. This functionality is all provided to any object that subclasses NSObject.</p>
<p>Thinking about the Notification Center example, it might do the following when it needs to tell our object that a notification was received:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>object respondsToSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>receivedNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// then actually call the selector</span>
		<span style="color: #002200;">&#91;</span>object performSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>receivedNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span>theNotification<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Lastly, you can use a function <code>NSSelectorFromString()</code> to get hold of a selector from a string specifying its name, so the code above could also be written:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>object respondsToSelector<span style="color: #002200;">:</span>NSSelectorFromString<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;receivedNotification&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// then actually call the selector</span>
		<span style="color: #002200;">&#91;</span>object performSelector<span style="color: #002200;">:</span>NSSelectorFromString<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;receivedNotification&quot;</span><span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span>theNotification<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span></pre></div></div>

<p>For more information on messaging and selectors, see: <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_9_section_1.html" target="_blank">Apple Developer Connection: How Messaging Works</a>.</p>
<p>With our short discussion of selectors out of the way, we can now start writing the code to handle accessing and setting our bound values.</p>
<h3>Getters and Setters</h3>
<p>Our <code>receivedNotification:</code> method is being called now whenever our originating object changes. We can modify that method so that it tries to find out the specified value of the object. To get the value, we need to call a suitable method on the originating object that will return to us that current value. The most sensible name for such a method would be a method with the same name as the name of the value &#8212; in other words, if an object has a &#8217;someValue&#8217; internal value, you could call:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	<span style="color: #a61390;">id</span> theValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>object someValue<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>to find out that value.</p>
<p>On this assumption then, our method needs to find out whether our originating object responds to a method with the name of the originating value name and, if so, call that method to acquire the current value.</p>
<ul>
<li>Open BindController.m in Xcode and modify your <code>receivedNotification:</code> method to look like the following:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>receivedNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Received a notification that an object changed&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>notification object<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> originatingObject <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Originating Value to fetch is: %@&quot;</span>, originatingValueName<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// see if there is a method to fetch this value name</span>
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>getterMethodName <span style="color: #002200;">=</span> originatingValueName;
		<span style="color: #a61390;">id</span> fetchedValue <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
		NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Originating Method to test is: %@&quot;</span>, getterMethodName<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>originatingObject respondsToSelector<span style="color: #002200;">:</span>NSSelectorFromString<span style="color: #002200;">&#40;</span>getterMethodName<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Originating Object responds to: %@&quot;</span>, getterMethodName<span style="color: #002200;">&#41;</span>;
			fetchedValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>originatingObject performSelector<span style="color: #002200;">:</span>NSSelectorFromString<span style="color: #002200;">&#40;</span>getterMethodName<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
			NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Fetched value is: %@&quot;</span>, fetchedValue<span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span>
		<span style="color: #002200;">&#123;</span>
			NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Originating Object is not KVC compliant for the value: %@&quot;</span>, originatingValueName<span style="color: #002200;">&#41;</span>;
			<span style="color: #a61390;">return</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>If you examine this code, you&#8217;ll see that we are first of all checking to make sure that the notification we are receiving has come from the object in which we are interested. We then look at the string containing the name of the value we want, and ask our originating object whether it responds to a method with the name of that value. Assuming it does, we fetch the value by calling that method.</p>
<p>Build and run the project and type something in the text field before pressing tab or enter. You&#8217;ll see in the console log all the various steps above and if everything goes to plan, you should see something like:</p>
<pre>
2008-08-27 14:33:26.756 FauxBindings[2411:10b] Received a notification that an object changed
2008-08-27 14:33:26.761 FauxBindings[2411:10b] Originating Value to fetch is: stringValue
2008-08-27 14:33:26.785 FauxBindings[2411:10b] Originating Method to test is: stringValue
2008-08-27 14:33:26.805 FauxBindings[2411:10b] Originating Object responds to: stringValue
2008-08-27 14:33:26.823 FauxBindings[2411:10b] Fetched value is: something
</pre>
<h4>KVC Compliance</h4>
<p>You&#8217;ll notice in the above code that if an object doesn&#8217;t respond to our suspected getter method name, we log a message to the console that the object <strong>isn&#8217;t KVC compliant</strong> for that value. To test this, change the call in the <code>awakeFromNib:</code> method to something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>awakeFromNib
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self FauxBindObject<span style="color: #002200;">:</span>self valueName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span> toObject<span style="color: #002200;">:</span>theTextField valueName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;anUnknownValue&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>When you run the application and modify the text field, you&#8217;ll see that the object doesn&#8217;t respond to a method called &#8216;<code>anUnknownValue</code>&#8216; and so we can&#8217;t acquire that value.</p>
<p><strong>KVC</strong> stands for <strong>Key-Value-Coding</strong>; what this means is that an object provides suitably named methods to access its values. In the above example, we ask our <code>theTextField</code> for the value named &#8220;anUnknownValue&#8221;. This &#8216;value name&#8217; is also referred to as a &#8216;key&#8217; so it would be best to say:</p>
<p>&#8220;We ask our <code>theTextField</code> object for the value of its &#8220;anUnknownValue&#8221; key&#8221;</p>
<p>For an object to be KVC-compliant for a particular key, it must provide methods to access the value of the key that can be worked out given the name of that key. This generally means that a &#8216;getter&#8217; method is given the same name as the name of the key (as in &#8220;stringValue&#8221;). There are other possibilities too; it might make sense for a Boolean (true/false) value called &#8220;onWatchList&#8221; to be accessed using an accessor &#8220;isOnWatchList&#8221; and in full-blown Cocoa bindings, there are various options you can use when naming accessor methods. For the purposes of this article, however, we won&#8217;t include checking for those other options in our FauxBind: method.</p>
<p>The other half of KVC-compliance is that an object provide a suitably-named &#8217;setter&#8217; method, i.e. a method to set the value of its key. Although there are again various options, this generally means that you put the word &#8217;set&#8217; at the front of the name of the key and correct the capitalization such that for a value &#8220;stringValue&#8221;, the setter would be &#8220;setStringValue&#8221;.</p>
<p>With this knowledge, we can go about changing the <code>receivedNotifiction:</code> method to try and set the value of the key on the destination object using a KVC setter method.</p>
<ul>
<li>If you changed the &#8220;stringValue&#8221; key above to &#8220;anUnknownValue&#8221; etc then change it back now:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	<span style="color: #002200;">&#91;</span>self FauxBindObject<span style="color: #002200;">:</span>self valueName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span> toObject<span style="color: #002200;">:</span>theTextField valueName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;stringValue&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

</li>
<li>Modify the <code>receivedNotification:</code> method to the following:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>receivedNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Received a notification that an object changed&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>notification object<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> originatingObject <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Key to fetch is: %@&quot;</span>, originatingValueName<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// see if there is a method to fetch this value name</span>
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>getterMethodName <span style="color: #002200;">=</span> originatingValueName;
		<span style="color: #a61390;">id</span> fetchedValue <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
		NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Originating Method to test is: %@&quot;</span>, getterMethodName<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>originatingObject respondsToSelector<span style="color: #002200;">:</span>NSSelectorFromString<span style="color: #002200;">&#40;</span>getterMethodName<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Originating Object responds to: %@&quot;</span>, getterMethodName<span style="color: #002200;">&#41;</span>;
			fetchedValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>originatingObject performSelector<span style="color: #002200;">:</span>NSSelectorFromString<span style="color: #002200;">&#40;</span>getterMethodName<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
			NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Fetched value for key is: %@&quot;</span>, fetchedValue<span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span>
		<span style="color: #002200;">&#123;</span>
			NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Originating Object is not KVC compliant for the key: %@&quot;</span>, originatingValueName<span style="color: #002200;">&#41;</span>;
			<span style="color: #a61390;">return</span>;
		<span style="color: #002200;">&#125;</span>
&nbsp;
&nbsp;
		NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;<span style="color: #2400d9;">\n</span>&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
		NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Destination's Key to change is: %@&quot;</span>, destinationValueName<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// see if there is a method to change this value name</span>
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>setterMethodNameFirstCharacter <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>destinationValueName substringToIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>setterMethodName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;set%@%@:&quot;</span>, <span style="color: #002200;">&#91;</span>setterMethodNameFirstCharacter capitalizedString<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>destinationValueName substringFromIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
		NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Destination Method to test is: %@&quot;</span>, setterMethodName<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>destinationObject respondsToSelector<span style="color: #002200;">:</span>NSSelectorFromString<span style="color: #002200;">&#40;</span>setterMethodName<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span>
		<span style="color: #002200;">&#123;</span>
			NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Destination Object responds to: %@&quot;</span>, setterMethodName<span style="color: #002200;">&#41;</span>;
&nbsp;
			<span style="color: #002200;">&#91;</span>destinationObject performSelector<span style="color: #002200;">:</span>NSSelectorFromString<span style="color: #002200;">&#40;</span>setterMethodName<span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span>fetchedValue<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span>
		<span style="color: #002200;">&#123;</span>
			NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Destination Object is not KVC complicant for the value: %@&quot;</span>, destinationValueName<span style="color: #002200;">&#41;</span>;
		<span style="color: #002200;">&#125;</span>
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>The additional code above now uses various <code>NSString</code> methods to get a suitable setter method name string by putting the word &#8220;set&#8221; on the front of the key name, which in turn has had its first letter capitalized.</p>
<p>If you run the application now, you&#8217;ll see the following:</p>
<pre>
2008-08-27 15:09:30.464 FauxBindings[2587:10b] Received a notification that an object changed
2008-08-27 15:09:30.467 FauxBindings[2587:10b] Key to fetch is: stringValue
2008-08-27 15:09:30.482 FauxBindings[2587:10b] Originating Method to test is: stringValue
2008-08-27 15:09:30.494 FauxBindings[2587:10b] Originating Object responds to: stringValue
2008-08-27 15:09:30.498 FauxBindings[2587:10b] Fetched value for key is: something
2008-08-27 15:09:30.499 FauxBindings[2587:10b]
2008-08-27 15:09:30.502 FauxBindings[2587:10b] Destination's Key to change is: theString
2008-08-27 15:09:30.503 FauxBindings[2587:10b] Destination Method to test is: setTheString:
2008-08-27 15:09:30.504 FauxBindings[2587:10b] Destination Object is not KVC complicant for the value: theString
</pre>
<p>The method is trying to set the value for the &#8216;theString&#8217; key but fails because our BindController object isn&#8217;t KVC compliant for the &#8216;theString&#8217; key &#8212; which is because we haven&#8217;t implemented a &#8217;setTheString&#8217; method in our BindController object. Let&#8217;s do that now:</p>
<ul>
<li>In BindController.h, add a method declaration for the &#8217;setTheString&#8217; method:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;cocoa/Cocoa.h&gt;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@interface</span> BindController <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	IBOutlet <span style="color: #400080;">NSTextField</span> <span style="color: #002200;">*</span>theTextField;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>theString;
&nbsp;
	<span style="color: #a61390;">id</span> originatingObject;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>originatingValueName;
	<span style="color: #a61390;">id</span> destinationObject;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>destinationValueName;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>showTheStringInLog<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>FauxBindObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>destObject valueName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>destValueName toObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>origObject valueName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>origValueName;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>receivedNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Implement this method in BindController.m with the following code:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Setting theString to value: %@&quot;</span>, value<span style="color: #002200;">&#41;</span>;
	theString <span style="color: #002200;">=</span> value;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>Now when you run the application, press the &#8216;Log theString&#8217; button before doing anything else, then type something in the text box and press enter, then press the &#8216;Log theString&#8217; button again. If all has gone to plan, you&#8217;ll have seen the following in the Console:</p>
<pre>
2008-08-27 15:20:41.207 FauxBindings[2652:10b] Value of theString is: (null)
2008-08-27 15:20:44.022 FauxBindings[2652:10b] Received a notification that an object changed
2008-08-27 15:20:44.023 FauxBindings[2652:10b] Key to fetch is: stringValue
2008-08-27 15:20:44.034 FauxBindings[2652:10b] Originating Method to test is: stringValue
2008-08-27 15:20:44.040 FauxBindings[2652:10b] Originating Object responds to: stringValue
2008-08-27 15:20:44.045 FauxBindings[2652:10b] Fetched value for key is: something
2008-08-27 15:20:44.064 FauxBindings[2652:10b]
2008-08-27 15:20:44.070 FauxBindings[2652:10b] Destination's Key to change is: theString
2008-08-27 15:20:44.074 FauxBindings[2652:10b] Destination Method to test is: setTheString:
2008-08-27 15:20:44.085 FauxBindings[2652:10b] Destination Object responds to: setTheString:
2008-08-27 15:20:44.086 FauxBindings[2652:10b] Setting theString to value: something
2008-08-27 15:20:45.222 FauxBindings[2652:10b] Value of theString is: something
</pre>
<p>Finally, we have a working binding between our text field&#8217;s <code>stringValue</code> key and our BindController object&#8217;s <code>theString</code> key. When the text field posts the notification that its value has changed, the value of our <code>theString</code> key is updated to match the new <code>stringValue</code> value.</p>
<h3>Reversing the Process</h3>
<p>Let&#8217;s see if we can get this working the other way around. In other words, can we get our <code>theTextField</code> object to update its <code>stringValue</code> when our <code>BindController</code> object&#8217;s <code>theString</code> is changed?</p>
<ul>
<li>First of all, change the <code>awakeFromNib:</code> method so that the binding happens the other way around:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>awakeFromNib
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self FauxBindObject<span style="color: #002200;">:</span>theTextField valueName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;stringValue&quot;</span> toObject<span style="color: #002200;">:</span>self valueName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>Next, we&#8217;ll change the user interface so that we can specify a value for theString and see what happens. Open up BindController.h and add another <code>IBOutlet</code> for an <code>NSTextField</code>. Also add a method to be called to set <code>theString</code> to the value of the text field:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;cocoa/Cocoa.h&gt;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@interface</span> BindController <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	IBOutlet <span style="color: #400080;">NSTextField</span> <span style="color: #002200;">*</span>theTextField;
	IBOutlet <span style="color: #400080;">NSTextField</span> <span style="color: #002200;">*</span>secondTextField;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>theString;
&nbsp;
	<span style="color: #a61390;">id</span> originatingObject;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>originatingValueName;
	<span style="color: #a61390;">id</span> destinationObject;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>destinationValueName;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>showTheStringInLog<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>FauxBindObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>destObject valueName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>destValueName toObject<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>origObject valueName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>origValueName;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>receivedNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>modifyTheStringToValueOfTextField<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>Open MainMenu.nib in Interface Builder and add a second text field along with a second button, and connect these to the new outlet and action. My interface looks like this:<br />
<img src="http://www.timisted.net/blog/wp-content/uploads/2008/08/fauxbindingsmainwindow2.gif" alt="The revised window for Faux Bindings in Interface Builder" title="fauxbindingsmainwindow2" width="348" height="273" class="size-full wp-image-214" /></li>
<li>Return to Xcode and open BindController.m, adding in the implementation for the <code>modifyTheStringToValueOfTextField:</code> method:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>modifyTheStringToValueOfTextField<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>secondTextField stringValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>Notice that we set the value of &#8216;theString&#8217; using the KVC setter method and not directly. Why we do this will become clear in a minute&#8230;</p>
<p>If at this point you build and run the application, you&#8217;ll find that nothing happens at all when you enter text in the second field and click the button other than a console log that <code>theString</code> was set to a new value. Why doesn&#8217;t the supposedly-bound text field update to match the new value of <code>theString</code>? Because no notification is ever sent that its value changed.</p>
<ul>
<li>Change the <code>setTheString:</code> method to the following:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Setting theString to value: %@&quot;</span>, value<span style="color: #002200;">&#41;</span>;
	theString <span style="color: #002200;">=</span> value;
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> postNotificationName<span style="color: #002200;">:</span>NSControlTextDidEndEditingNotification object<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>Now, when you type something in the second text field (and press enter to commit it), then click the button, our notification method tries to obtain the new value of theString but complains that our BindController object is not KVC compliant for theString:</p>
<pre>
2008-08-27 15:48:19.487 FauxBindings[2869:10b] Setting theString to value: something
2008-08-27 15:48:19.494 FauxBindings[2869:10b] Received a notification that an object changed
2008-08-27 15:48:19.514 FauxBindings[2869:10b] Key to fetch is: theString
2008-08-27 15:48:19.521 FauxBindings[2869:10b] Originating Method to test is: theString
2008-08-27 15:48:19.523 FauxBindings[2869:10b] Originating Object is not KVC compliant for the key: theString
</pre>
<p> Why? Because there is no getter method defined in the BindController object.</p>
<ul>
<li>Open BindController.h and add a declaration for the getter method underneath its setter declaration:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theString;</pre></div></div>

</li>
<li>Implement this method in BindController.m thus:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theString
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> theString;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>This time when you run the application, you should find that everything functions as expected. Type something in the second field, press enter and then press the button. The first text field updates its content to match.</p>
<p>This is where the <strong>KVO</strong> comes from &#8212; standing for <strong>Key-Value-Observing</strong>. For an object to allow Observation of its Key Values, its setter methods must announce when they are changing a particular value so that notifications can be picked up and changes propagated.</p>
<h4>One more Check</h4>
<p>Just one final way to demonstrate that this functionality works is to modify the awakeFromNib: method to connect the two text fields directly.</p>
<ul>
<li>Modify <code>awakeFromNib:</code> to this:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>awakeFromNib
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self FauxBindObject<span style="color: #002200;">:</span>secondTextField valueName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;stringValue&quot;</span> toObject<span style="color: #002200;">:</span>theTextField valueName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;stringValue&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>This time, if you type something in the first text field and press enter, the second text field will change its value; note that there is no <code>BindController theString</code> value changing here, it&#8217;s only the two text fields. If you want the first text field to update when the second changes too, you would have to initiate that binding separately.</p>
<h3>Starting to Replace Code</h3>
<p>Assuming that an object is KVC-compliant (remember, that&#8217;s when it has suitably named accessors), you can access a named value (key) from that object by using the method <code>valueForKey:</code> and set it using <code>setValue:forKey:</code>. These methods perform all the necessary magic to find out if an object has KVC accessors and, assuming they do, call them to access the values.</p>
<ul>
<li>We can simplify our receivedNotification: method somewhat by changing it to the following:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>receivedNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Received a notification that an object changed&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>notification object<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> originatingObject <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Key to fetch is: %@&quot;</span>, originatingValueName<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #a61390;">id</span> fetchedValue <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>originatingObject valueForKey<span style="color: #002200;">:</span>originatingValueName<span style="color: #002200;">&#93;</span>;
&nbsp;
		NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Destination's Key to change is: %@&quot;</span>, destinationValueName<span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #002200;">&#91;</span>destinationObject setValue<span style="color: #002200;">:</span>fetchedValue forKey<span style="color: #002200;">:</span>destinationValueName<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
</ul>
<p>If you run using this code, you&#8217;ll see that everything happens as before, only we&#8217;re using the <code>valueForKey:</code> and <code>setValue:forKey:</code> methods instead of our previous logic.</p>
<h4>Proper KVO</h4>
<p>You&#8217;ll remember that earlier we &#8216;faked&#8217; an <code>NSControlTextDidEndEditing</code> notification to force our demonstration to work when our <code>theString</code> key was modified &#8212; whilst not being problematic in this particular demonstration, it&#8217;s not good practice to post notifications that aren&#8217;t legitimate. It would be better in this case to post a &#8220;theStringValueDidChange&#8221; notification that is more suitably named (and, if you did this, you&#8217;d obviously also need to change the notification registration code to specify the new notification type that the object was hoping to receive).</p>
<p>For information on naming conventions for notifications (and other things), see: <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingIvarsAndTypes.html" target="_blank">Apple Developer Connection : Naming Instance Variables and Data Types</a>.</p>
<p>In a real bindings scenario, you use two methods <code>willChangeValueForKey:</code> and <code>didChangeValueForKey:</code> to tell the bindings mechanism that suitable notifications should be sent out. Our setTheString: method should really look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Setting theString to value: %@&quot;</span>, value<span style="color: #002200;">&#41;</span>;
	theString <span style="color: #002200;">=</span> value;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Again, these methods are available to any subclass of NSObject.</p>
<p>When writing Core Data NSManagedObject subclasses, you&#8217;ll find that you also need to use <code>willAccessValueForKey:</code> and <code>didAccessValueForKey:</code> in your <strong>getter</strong> accessor methods &#8212; these inform the managed object that it needs to fetch the relevant data from the store if necessary.</p>
<h3>Real Bindings</h3>
<p>Now that we know what&#8217;s going on behind the scenes, let&#8217;s change our sample application to use real bindings rather than our faux bindings.</p>
<ul>
<li>First off, our BindController object now doesn&#8217;t need to do anything about notifications so we can remove the methods that handle them. We&#8217;ll also take out the second text field and button methods. Change the BindController.h file to the following:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;cocoa/Cocoa.h&gt;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@interface</span> BindController <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	IBOutlet <span style="color: #400080;">NSTextField</span> <span style="color: #002200;">*</span>theTextField;
&nbsp;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>theString;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>showTheStringInLog<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theString;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
<li>In Interface Builder, remove the second text field and button from the Window.</li>
<li>We also need to remove the unused methods from BindController.m and modify our <code>awakeFromNib:</code> method to use proper Cocoa Bindings. Change the file to the following:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;BindController.h&quot;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@implementation</span> BindController
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>showTheStringInLog<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Value of theString is: %@&quot;</span>, theString<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>awakeFromNib
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>theTextField bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;value&quot;</span> toObject<span style="color: #002200;">:</span>self withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Setting theString to value: %@&quot;</span>, value<span style="color: #002200;">&#41;</span>;
	theString <span style="color: #002200;">=</span> value;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theString
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> theString;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
</ul>
<p>In the <code>awakeFromNib:</code> method, we bind the text field&#8217;s value to our <code>theString</code> key. All the magic is handled for us and the application behaves as expected &#8212; typing text in the text field and pressing enter will update <code>theString</code>.</p>
<p>To be able to do this the other way around using Cocoa Bindings (i.e. change the value of the text field when <code>theString</code> changes), you need to make a further modification.</p>
<ul>
<li>Change the BindController.m file to the following:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;BindController.h&quot;</span>
&nbsp;
&nbsp;
<span style="color: #a61390;">@implementation</span> BindController
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>initialize
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self exposeBinding<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>showTheStringInLog<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender
<span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Value of theString is: %@&quot;</span>, theString<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>awakeFromNib
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span> toObject<span style="color: #002200;">:</span>theTextField withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;stringValue&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Setting theString to value: %@&quot;</span>, value<span style="color: #002200;">&#41;</span>;
	theString <span style="color: #002200;">=</span> value;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theString
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> theString;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

</li>
</ul>
<p>You&#8217;ll notice that there is also now an &#8216;initialize&#8217; <strong>class method</strong>. If you&#8217;re not sure what this means, a class method is one that is called on a class rather than an instance of that class and is designated as such with a &#8216;+&#8217; in front of the method declaration rather than a &#8216;-&#8217;. The <code>+ (void)initialize</code> method can be called like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	<span style="color: #002200;">&#91;</span>BindController initialize<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>whereas <code>- (void)awakeFromNib</code>, for example, must be called on an instance of the object like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	BindController <span style="color: #002200;">*</span>bcInstance <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>BindController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>bcInstance awakeFromNib<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>(and yes, there&#8217;s a memory leak just in that little code snippet &#8212; &#8220;if you&#8217;re alloc-initing, you should be releasing&#8221; [or "auto-releasing"...])</p>
<p>The <code>+initialize</code> method is called before any instances of our object are used in code so that features of the class are known for the future; here we&#8217;re exposing the fact that our <code>BindController</code> object has a bindable key called &#8216;theString&#8217;.</p>
<p>In the <code>awakeFromNib:</code> method, we then actually bind this bindable key to the <code>stringValue</code> key on our text field.</p>
<p>If you build and run the application with the binding set up this way round, obviously you&#8217;ll find that changing the value of the text field doesn&#8217;t have any effect on our <code>theString</code> key. But, remember earlier in this article that when our <code>theString</code> attribute hadn&#8217;t yet been initialized to any value, the &#8216;Log theString&#8217; button would cause a &#8216;Value of theString is: (null)&#8217; to be displayed. This time, however, if you click the button, you&#8217;ll find that it outputs &#8216;Value of theString is: &#8216; with a blank string (@&#8221;" in object terms) as the value (and indeed, outputs this to the console when you first run the application). You might like to philosophize to yourself about where that comes from&#8230;</p>
<h3>Things to Note</h3>
<p>There are a few further points worth noting from all this:</p>
<ul>
<li>You could bind to the BindController &#8216;theString&#8217; key from another object rather than in the BindController&#8217;s <code>awakeFromNib:</code> method since its binding is &#8216;exposed&#8217;. For example:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	<span style="color: #400080;">NSTextField</span> <span style="color: #002200;">*</span>someTextField <span style="color: #002200;">=</span> <span style="color: #11740a; font-style: italic;">//get a text field from somewhere</span>
	BindController <span style="color: #002200;">*</span>bcInstance <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>BindController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>someTextField bind<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;stringValue&quot;</span> toObject<span style="color: #002200;">:</span>bcInstance withKeyPath<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span> options<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

</li>
<li>The name of the attribute that holds a value does not need to be the same as the key for that value. It is common practice to follow a different naming convention for attributes that are in the interface for the class rather than attributes allocated inside methods, one example being to put an underscore character on the front of class attributes. Storage of the value for our theString key could be done like this:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Setting theString to value: %@&quot;</span>, value<span style="color: #002200;">&#41;</span>;
	_theString <span style="color: #002200;">=</span> value;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theString
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> _theString;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>There are various other features in KVC and KVO that I haven&#8217;t discussed here such as if one key&#8217;s value depends on another key&#8217;s value, you can specify using another class method of the form <code>keyPathsForValuesAffecting...</code> such that if that depended-upon value changes, the Bindings mechanism will automatically notify objects bound to your dependent key of the change. Consider the following example where there is a second string key value exposed to which objects can bind called &#8220;dependentString&#8221;. It simply returns a modified version of theString so if an object is bound to that and theString changes, they need to be told so that they can request the new version of dependentString, all of which is accomplished quite simply, like this:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>value
<span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self willChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Setting theString to value: %@&quot;</span>, value<span style="color: #002200;">&#41;</span>;
	_theString <span style="color: #002200;">=</span> value;
	<span style="color: #002200;">&#91;</span>self didChangeValueForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>theString
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> _theString;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>dependentString
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self theString<span style="color: #002200;">&#93;</span> stringByAppendingString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot; Plus Something Else&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #11740a; font-style: italic;">// note dependentString has no setter</span>
&nbsp;
<span style="color: #002200;">+</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>keyPathsForValuesAffectingDependentString
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSSet</span> setWithObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;theString&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>There are also various validation methods that, if they exist, can be called automatically to make sure that a value is suitable before allowing it to be set for a particular key. See <a href="http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/Validation.html" target="_blank">Apple Developer Connection : KVC Key-Value Validation</a> for more details but here is a small example:

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>validateTheString<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>valueRef error<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span> <span style="color: #002200;">**</span><span style="color: #002200;">&#41;</span>outError
<span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Insert custom validation logic here.</span>
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>; <span style="color: #11740a; font-style: italic;">// YES if is valid, NO otherwise</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

</li>
<li>It&#8217;s worth noting that if an object doesn&#8217;t appear to be KVC-compliant for a particular value, the system will call the &#8216;valueForUndefinedKey:&#8217; method on an object which, on NSObject, raises an exception (and thus halts program execution). You can override this method, if you wish, to prevent exceptions being raised and to provide some sort of &#8216;default&#8217; value if such a thing is meaningful.</li>
<li>There is also a very useful &#8216;dictionaryWithValuesForKeys:&#8217; method which will build an NSDictionary containing the specified keys and their respective values &#8212; useful if you need to access, persist or copy several values and don&#8217;t want to call &#8216;valueForKey:&#8217; etc on each one. The equally useful setter equivalent of this is &#8217;setValuesForKeysWithDictionary:&#8217; which does exactly what it sounds like.</li>
</ul>
<h3>The End</h3>
<p>I hope that this article has been of some use and/or interest if you&#8217;ve made it this far. Obviously I haven&#8217;t talked here about KVC compliance for accessing arrays, sets and scalar values etc but these aren&#8217;t at all complicated, provided the fundamental ideas are understood. It is also worth thinking about whether accessor methods should retain and release their values under traditional memory management.</p>
<p>Finally, since most accessor methods follow standard formats (i.e. broadcast that you&#8217;re changing a value, change the value, broadcast that you&#8217;ve changed the value) it&#8217;s possible to automate the process of accessor creation, most notably using Objective-C 2.0 properties. Using @property and @synthesize are both powerful features of the language for all sorts of reasons although many developers remain unconvinced of their usefulness. Using Objective-C properties offers faster (in terms of processing cycles) access to an object&#8217;s keys than either using traditional accessors, or (even slower) using valueForKey: etc. But, because of the need for terms used to specify how a @property is @synthesized, it is easy to pick the wrong words (e.g. atomic, retain, copy, readwrite etc) and not be able to work out why a particular KVC procedure or Bindings technique is crashing. Use of Objective-C 2.0 code also means your application won&#8217;t run on anything earlier than Mac OS X 10.5 Leopard.</p>
<p>Should you be interested in reading more about views on Objective-C 2.0 properties, take a look at: <a href="http://cocoawithlove.com/2008/08/in-defense-of-objective-c-20-properties.html" target="_blank">Matt Gallagher &#8212; In defense of Objective-C 2.0 Properties</a>.</p>
<p>This post contains links to various other posts that attack properties, and also clarifies their purpose, notably, &#8220;<em>not</em> to provide auto-generated getter and setter methods&#8221; (although this is certainly one <em>side effect</em> of their use!).</p>
<p>As always, all comments are welcomed either as Blog post comments below or by email.</p>
<p>Happy Binding!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.timisted.net/blog/archive/on-cocoa-bindings/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
