<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>André&#039;s Blog</title>
	<atom:link href="http://andrehoffmann.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://andrehoffmann.wordpress.com</link>
	<description>PHP, Zend Framework, Mac/Cocoa Programming</description>
	<lastBuildDate>Fri, 13 Jan 2012 11:48:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='andrehoffmann.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>André&#039;s Blog</title>
		<link>http://andrehoffmann.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://andrehoffmann.wordpress.com/osd.xml" title="André&#039;s Blog" />
	<atom:link rel='hub' href='http://andrehoffmann.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Phonebook/NSTableView Tutorial for Dummies(Xcode 3.1.3)</title>
		<link>http://andrehoffmann.wordpress.com/2009/09/03/phonebook-tutorial-for-dummiesxcode-3-1-3/</link>
		<comments>http://andrehoffmann.wordpress.com/2009/09/03/phonebook-tutorial-for-dummiesxcode-3-1-3/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 13:02:17 +0000</pubDate>
		<dc:creator>andrehoffmann</dc:creator>
				<category><![CDATA[cocoa]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[drawer]]></category>
		<category><![CDATA[nsarray]]></category>
		<category><![CDATA[nsarraycontroller]]></category>
		<category><![CDATA[nsdictionary]]></category>
		<category><![CDATA[nspredicate]]></category>
		<category><![CDATA[NSSearchField]]></category>
		<category><![CDATA[nstableview]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[phonebook]]></category>
		<category><![CDATA[plist]]></category>
		<category><![CDATA[property]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://andrehoffmann.wordpress.com/?p=74</guid>
		<description><![CDATA[Introduction This tutorial explains how to create a simple phonebook with a search function which will look similar to this: The following things will be covered by this tutorial: creating a window with a drawer feeding a NSTableView with an NSArrayController search in the table using NSPredicate to filter the array Getting Started Creating the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrehoffmann.wordpress.com&amp;blog=440791&amp;post=74&amp;subd=andrehoffmann&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>This tutorial explains how to create a simple phonebook with a search function which will look similar to this:</p>
<p><img class="alignnone size-full wp-image-73" title="Phonebook Tut Step 1" src="http://andrehoffmann.files.wordpress.com/2009/08/phonebook-tut-step-1.png?w=700" alt="Phonebook Tut Step 1"   /></p>
<p>The following things will be covered by this tutorial:</p>
<ul>
<li>creating a window with a drawer</li>
<li>feeding a NSTableView with an NSArrayController</li>
<li>search in the table using NSPredicate to filter the array</li>
</ul>
<h2><span id="more-74"></span></h2>
<h2>Getting Started</h2>
<h3>Creating the Interface</h3>
<p>Start by &#8211; this should come as no surprise to you &#8211; creating a new Cocoa project and open the MainMenu.xib in IB. Remove the Window from the Document Window and instead drag in a &#8220;Window and Drawer&#8221; (Library-&gt;Cocoa-&gt;Application-&gt;Windows).</p>
<p>Click the new window twice to open it and drag a Toolbar(Library-&gt;Cocoa-&gt;Application-&gt;Toolbars) onto it.  In Library-&gt;Cocoa-&gt;Views &amp; Cells-&gt;Inputs &amp; Values which you can add to the toolbar(beforehand you need to click on the toolbar twice to open the configurator).</p>
<p>Remove everything unnecessary from the toolbar and add a &#8220;Image Toolbar Item&#8221;. When you select this item you can change its image in the attributes tab. Set it to &#8220;NSAddTemplate&#8221;. There you can also change the label.</p>
<p><img class="alignnone size-full wp-image-75" title="Phonebook Tut Step 2" src="http://andrehoffmann.files.wordpress.com/2009/08/phonebook-tut-step-2.png?w=700" alt="Phonebook Tut Step 2"   /></p>
<p>Next, add a NSTableView to the window so that it fills the remaining space. Select the ScrollView surrounding the NSTableView and go to the Size tab where you should enable autosizing by clicking on the inner double arrows in the rectangle:</p>
<p><img class="alignnone size-full wp-image-76" title="Phonebook Tut Step 3" src="http://andrehoffmann.files.wordpress.com/2009/08/phonebook-tut-step-3.png?w=700" alt="Phonebook Tut Step 3"   /></p>
<p>Now select the TableView and click on the Attributes tab here you can change the number of columns to 3. Resize the columns so that they look okay and rename them(click &#8216;em twice) to &#8220;First Name&#8221;, &#8220;Last Name&#8221;, &#8220;Phone Number&#8221;.</p>
<p>Click twice on the &#8220;Drawer Content View&#8221; in the Document Window so that it opens and add some labels and textfields so that it looks like this(altough I encourage you to make it look better):</p>
<p><img class="alignnone size-full wp-image-77" title="Phonebook Tut Step 4" src="http://andrehoffmann.files.wordpress.com/2009/08/phonebook-tut-step-4.png?w=700" alt="Phonebook Tut Step 4"   /></p>
<h3>Adding Outlets/Actions</h3>
<p>Add an ArrayController(Library-&gt;Cocoa-&gt;Objects &amp; Controllers-&gt;Controllers) and an Object(same place) to the Document Window. Change the &#8220;Class Name&#8221; of the Object to &#8220;WindowController&#8221;(Identity tab) in the same tab add the following things:</p>
<p><img class="alignright" style="border:0 initial initial;" title="Phonebook Tut Step 5" src="http://andrehoffmann.files.wordpress.com/2009/08/phonebook-tut-step-5.png?w=222&#038;h=343" alt="Phonebook Tut Step 5" width="222" height="343" /></p>
<ul>
<li>2 Actions:
<ul>
<li> addContactAction</li>
<li>updateFilterAction</li>
</ul>
</li>
<li>6 Outlets:
<ul>
<li>arrayContacts(NSArrayController)</li>
<li>txtSearchField(NSSearchField)</li>
<li>tblContacts(NSTableView)</li>
<li>txtFirstName(NSTextField)</li>
<li>txtLastName(NSTextField)</li>
<li>txtPhoneNumber(NSTextField)</li>
</ul>
</li>
</ul>
<p>Finally go to File-&gt;Write Class Files.. while having the WindowController selected.</p>
<p>Click the toolbar twice, so that the configuration comes up and link the Add Contact button to the Drawer&#8217;s(in the Document Window) toggle action, so that it toggles the drawer&#8217;s state between in and out every time the button is clicked.</p>
<p>Now connect every Class Outlet and every Class Action that you created before(if you don&#8217;t know how to do that check out my other tutorials).</p>
<h2>The Actual Implementation</h2>
<h3>Adding a property</h3>
<p>Now we need to add a property for our contacts(If you want to learn more about properties see chapter 9 on <a href="http://cocoadevcentral.com/d/learn_objectivec/">this</a> page). To add it open up WindowController.h and add a NSMutableArray contacts and a associated property &#8220;contacts&#8221;. Also specify the superclass of the WindowController which we choose to be NSWindowController:</p>
<p><pre class="brush: cpp;">
@interface WindowController : NSWindowController {

    IBOutlet NSArrayController *arrayContacts;
    IBOutlet NSTableView *tblContacts;
    IBOutlet NSTextField *txtFirstName;
    IBOutlet NSTextField *txtLastName;
    IBOutlet NSTextField *txtPhoneNumber;
    IBOutlet NSSearchField *txtSearchField;

    NSMutableArray* contacts;
}

- (IBAction)addContactAction:(id)sender;
- (IBAction)updateFilterAction:(id)sender;

@property (copy) NSArray* contacts;

@end
</pre></p>
<p>In the WindowController.m add a corresponding synthesize statement:</p>
<p><pre class="brush: cpp;">
...

@implementation WindowController

@synthesize contacts;

...
</pre></p>
<h3>Loading the Contacts from a file</h3>
<p>Whenever you run the application we want to the contacts to be read from a .plist and added to the table, so let&#8217;s add a awakeFromNib to the WindowController.m which is called when the interface finished loading.</p>
<p><pre class="brush: cpp;">
...
@implementation WindowController

@synthesize contacts;

NSString *filePath;

...

- (void)awakeFromNib {
	//set up the path of the plist(which will reside in the Resource folder of our app
	filePath	= [[[NSBundle mainBundle] pathForResource:@&quot;Contacts&quot; ofType:@&quot;plist&quot;] retain];

	//initialize the contacts array
	contacts  = [[NSMutableArray alloc] init];

	//check if the file Contacts.plist exists
	if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {

		//if it does read its contents into an array and add the contacts to the arrayController wich will then feed the table
		NSMutableArray *contactsFile = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
		for (id contact in contactsFile) {
			[arrayContacts addObject:contact];
		}
	}
}
...
</pre></p>
<h3>Connecting the ArrayController to the TableView</h3>
<p>Now we need to connect the ArrayController to the property we created, so open up Interface Builder and go to the Bindings tab while having the ArrayController selected. There go to &#8220;Content Array&#8221; and bind it to the Window Controller. As the &#8220;Model Key Path&#8221; choose &#8220;contacts&#8221;(the name of our property).</p>
<p>Keep the Bindings tab open and select the &#8220;First Name&#8221; column of the table, go to &#8220;Value&#8221; and bind it to the arrayController use &#8220;arrangedObjects&#8221; as the Controller Key and &#8220;firstName&#8221; as the Model Key Path.</p>
<p><img class="alignnone size-full wp-image-83" title="Phonebook Tut Step 6" src="http://andrehoffmann.files.wordpress.com/2009/09/phonebook-tut-step-6.png?w=700" alt="Phonebook Tut Step 6"   /></p>
<p>Repeat this procedure for the other two columns and choose &#8220;lastName&#8221; and &#8220;phoneNumber&#8221; as Model Key Path.</p>
<p>Now whenever a new entry is added to the arrayController the table is filled with the informations you just binded to it.</p>
<h3>The First Contact</h3>
<p>You could run this to see if it works if you already had a Contacts.plist. If you want to try it, copy this to &#8220;build/Debug/Phonebook.app/Contents/Resources/Contacts.plist&#8221;:</p>
<p><pre class="brush: xml;">
&lt; ?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt; !DOCTYPE plist PUBLIC &quot;-//Apple//DTD PLIST 1.0//EN&quot; &quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;&gt;
&lt;plist version=&quot;1.0&quot;&gt;
&lt;array&gt;
	&lt;dict&gt;
		&lt;key&gt;firstName&lt;/key&gt;
		&lt;string&gt;John&lt;/string&gt;
		&lt;key&gt;lastName&lt;/key&gt;
		&lt;string&gt;Doe&lt;/string&gt;
		&lt;key&gt;phoneNumber&lt;/key&gt;
		&lt;string&gt;0123456789&lt;/string&gt;
	&lt;/dict&gt;
&lt;/array&gt;
&lt;/plist&gt;
</pre></p>
<p>Note that this is <strong>not</strong> necessary and only serves the purpose of having some sense of achievement.</p>
<h3>Adding Contacts</h3>
<p>To be able to add new contacts all we have to do is to fill the body of the addContactAction:</p>
<p><pre class="brush: cpp;">
- (IBAction)addContactAction:(id)sender {
	//create a new contact based on the input values
	NSDictionary *dict =[NSDictionary dictionaryWithObjectsAndKeys:
						 [txtFirstName stringValue], @&quot;firstName&quot;,
						 [txtLastName stringValue], @&quot;lastName&quot;,
						 [txtPhoneNumber stringValue], @&quot;phoneNumber&quot;,
						 nil];

	//add it to the arrayController
	[arrayContacts addObject:dict];

	//save the changes to the plist
	if ( ![[NSFileManager defaultManager] fileExistsAtPath:filePath] || [[NSFileManager defaultManager] isWritableFileAtPath:filePath]) {
		[[arrayContacts arrangedObjects] writeToFile:filePath atomically:YES];
	}
}
</pre></p>
<p>Try it out! Also try to restart the application to see if your changes have been saved.</p>
<h3>Making the Seach work</h3>
<p>We&#8217;ll be using predicates to filter the TableView. To learn more about the NSPredicate class see the <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSPredicate_Class/Reference/NSPredicate.html#//apple_ref/occ/instm/NSPredicate/predicateWithSubstitutionVariables:">Mac OS X  Reference Library</a>.  It&#8217;s pretty similar to SQL if you&#8217;ve been using that before.</p>
<p>Here is how it works: First we create a &#8220;template&#8221; which contains the query but no actual values. We&#8217;ll do that in the awakeFromNib method:</p>
<p><pre class="brush: cpp;">
...
@implementation WindowController
...
NSPredicate *predicateTemplate;
...

- (void)awakeFromNib {
	//creating a predicate with the condition that either firstName, lastName or phoneNumber contains the searchString (case- and diacritic-insensitive)
	predicateTemplate = [[NSPredicate predicateWithFormat:@&quot;(firstName contains[cd] $searchString) or (lastName contains[cd] $searchString) or (phoneNumber contains[cd] $searchString)&quot;] retain];
...
</pre></p>
<p>Whenever the value in the seach field changes the updateFilterAction is called, so let&#8217;s put our search logic there:</p>
<p><pre class="brush: cpp;">
- (IBAction)updateFilterAction:(id)sender {

	NSString *searchString = [txtSearchField stringValue];
	NSPredicate *predicate;

	//if the search field is empty do not search!
	if ([searchString isEqualToString:@&quot;&quot;]) {
		predicate = nil;
	} else {
		//create a new dictionary with the search string
		NSMutableDictionary *bindVariables = [[NSMutableDictionary alloc] init];
		[bindVariables setObject:searchString forKey:@&quot;searchString&quot;];

		//and create a predicate from the template by replacing the variable with its actual value
		predicate = [predicateTemplate predicateWithSubstitutionVariables:bindVariables];
	}

	//apply the predicate to the array controller
	[arrayContacts setFilterPredicate:predicate];
}
</pre></p>
<p>That&#8217;s all that needed to be done to make the search field work. Check it out!</p>
<h2>The Source</h2>
<p>The final Xcode project can be download from <a href="http://antiHERO2006.googlepages.com/Phonebook.zip">here</a>.</p>
<p>Have fun!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/andrehoffmann.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/andrehoffmann.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/andrehoffmann.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/andrehoffmann.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/andrehoffmann.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/andrehoffmann.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/andrehoffmann.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/andrehoffmann.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/andrehoffmann.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/andrehoffmann.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/andrehoffmann.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/andrehoffmann.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/andrehoffmann.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/andrehoffmann.wordpress.com/74/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrehoffmann.wordpress.com&amp;blog=440791&amp;post=74&amp;subd=andrehoffmann&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://andrehoffmann.wordpress.com/2009/09/03/phonebook-tutorial-for-dummiesxcode-3-1-3/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4652e44d290083315bee7e5bd846337d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">André Hoffmann</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/phonebook-tut-step-1.png" medium="image">
			<media:title type="html">Phonebook Tut Step 1</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/phonebook-tut-step-2.png" medium="image">
			<media:title type="html">Phonebook Tut Step 2</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/phonebook-tut-step-3.png" medium="image">
			<media:title type="html">Phonebook Tut Step 3</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/phonebook-tut-step-4.png" medium="image">
			<media:title type="html">Phonebook Tut Step 4</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/phonebook-tut-step-5.png" medium="image">
			<media:title type="html">Phonebook Tut Step 5</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/09/phonebook-tut-step-6.png" medium="image">
			<media:title type="html">Phonebook Tut Step 6</media:title>
		</media:content>
	</item>
		<item>
		<title>NumberStepper Tutorial for Dummies(Xcode 3.1.3)</title>
		<link>http://andrehoffmann.wordpress.com/2009/08/29/numberstepper-tutorial-for-dummiesxcode-3-1-3/</link>
		<comments>http://andrehoffmann.wordpress.com/2009/08/29/numberstepper-tutorial-for-dummiesxcode-3-1-3/#comments</comments>
		<pubDate>Sat, 29 Aug 2009 20:19:44 +0000</pubDate>
		<dc:creator>andrehoffmann</dc:creator>
				<category><![CDATA[cocoa]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[delegate]]></category>
		<category><![CDATA[nsstepper]]></category>
		<category><![CDATA[nstextfield]]></category>
		<category><![CDATA[numberformatter]]></category>
		<category><![CDATA[numberstepper]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://andrehoffmann.wordpress.com/?p=62</guid>
		<description><![CDATA[Introduction This tutorial will show you how you can combine a NSTextField and a NSStepper to create a control that can both be typed into and stepped up/down. Getting Started Create a new project and open the MainMenu.xib in IB. Drag a Text Field and a Stepper right next to it. (Both can be found [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrehoffmann.wordpress.com&amp;blog=440791&amp;post=62&amp;subd=andrehoffmann&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>This tutorial will show you how you can combine a NSTextField and a NSStepper to create a control that can both be typed into and stepped up/down.</p>
<p><img class="alignnone size-full wp-image-87" title="NumberStepper Tut Step 4" src="http://andrehoffmann.files.wordpress.com/2009/08/numberstepper-tut-step-4.png?w=700" alt="NumberStepper Tut Step 4"   /></p>
<p><span id="more-62"></span></p>
<h2>Getting Started</h2>
<p>Create a new project and open the MainMenu.xib in IB. Drag a Text Field and a Stepper right next to it. (Both can be found in <em>Library-&gt;Cocoa-&gt;Views &amp; Cells-&gt;Inputs &amp; Values</em>) so that it looks like this:</p>
<p><img class="alignnone size-full wp-image-63" title="NumberStepper Tut Step 1" src="http://andrehoffmann.files.wordpress.com/2009/08/numberstepper-tut-step-1.png?w=700" alt="NumberStepper Tut Step 1"   /></p>
<p>Also drag an Object to the Document Window(<em>Library-&gt;Cocoa-&gt;Objects &amp; Controller-&gt;Object</em>) and change the Class Identity(Identity tab) to WindowController. In the same tab add two outlets: &#8220;input&#8221; (NSTextField) and &#8220;stepper&#8221; (NSStepper) and one action &#8220;stepperAction&#8221;.</p>
<p><img class="alignnone size-full wp-image-64" title="NumberStepper Tut Step 2" src="http://andrehoffmann.files.wordpress.com/2009/08/numberstepper-tut-step-2.png?w=700" alt="NumberStepper Tut Step 2"   /></p>
<p>Go to File-&gt;Write Class Files.. to create the WindowController.m/h.</p>
<p>Open the WindowController.h in Xcode and set the superclass to NSWindowController. Then reload the classes files in IB and connect the textfield, the stepper and the window to the Window Controller. Also connect the stepper to the stepperAction of the WindowController.</p>
<p><img class="alignnone size-full wp-image-67" title="NumberStepper Tut Step 3" src="http://andrehoffmann.files.wordpress.com/2009/08/numberstepper-tut-step-32.png?w=700" alt="NumberStepper Tut Step 3"   /></p>
<p>Search for the Number Formatter (Library-&gt;Cocoa-&gt;Views &amp; Cells-&gt;Formatters) and drag it right on the textfield. Set the standard value of the textfield to 0 by clicking on it twice.</p>
<p>Back to the WindowController.m:</p>
<p><pre class="brush: cpp;">
#import &quot;WindowController.h&quot;

@implementation WindowController

- (IBAction)stepperAction:(id)sender {
	[input setIntValue: [stepper intValue]];
}

@end

</pre></p>
<p>This updates the textfield value whenever you click the stepper. Run it and give it a try.</p>
<p>But what happens when you change the number in the textfield and then click on the stepper? Right. It takes the old value and increases/decreases it.</p>
<p>To avoid that we&#8217;ll need to update the stepper value whenever the value in the textfield is changed. This only works via delegates. A look at the <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSTextField_Class/Reference/Reference.html">reference entry for NSTextfield</a> gives us the name of our delegate: <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ApplicationKit/Classes/NSTextField_Class/Reference/Reference.html#//apple_ref/occ/instm/NSTextField/textDidChange:">textDidChange</a>. So open up the WindowController.m and add the following things:</p>
<p><pre class="brush: cpp;">
- (void) awakeFromNib {
	[input setDelegate:self];
}

- (void)controlTextDidChange:(NSNotification *)aNotification {
	if ([[aNotification name] isEqualToString:@&quot;NSControlTextDidChangeNotification&quot;]) {

		if ( [aNotification object] == input ) {
			[stepper setIntValue:[input intValue]];
		}
	}
}
</pre></p>
<p>That should do it. Try it out!</p>
<h2>Additional Informations</h2>
<p>Just as a side note: The Number Formatter makes sure that only valid numbers can be entered into the textfield. If you enter invalid characters it will prevent you from clicking somewhere else until you made the input valid. (To try that you&#8217;d have to add any other controls that you could click on, as there is currently only one on our view)<br />
If you&#8217;d want to change the interval or the increment rate you&#8217;ll have to change both the settings of the NSStepper and the Number Formatter. The formatter can be accessed by clicking onto the textfield and then on the symbol on the lower right.</p>
<p>I hope this helps someone.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/andrehoffmann.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/andrehoffmann.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/andrehoffmann.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/andrehoffmann.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/andrehoffmann.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/andrehoffmann.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/andrehoffmann.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/andrehoffmann.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/andrehoffmann.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/andrehoffmann.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/andrehoffmann.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/andrehoffmann.wordpress.com/62/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/andrehoffmann.wordpress.com/62/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/andrehoffmann.wordpress.com/62/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrehoffmann.wordpress.com&amp;blog=440791&amp;post=62&amp;subd=andrehoffmann&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://andrehoffmann.wordpress.com/2009/08/29/numberstepper-tutorial-for-dummiesxcode-3-1-3/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4652e44d290083315bee7e5bd846337d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">André Hoffmann</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/numberstepper-tut-step-4.png" medium="image">
			<media:title type="html">NumberStepper Tut Step 4</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/numberstepper-tut-step-1.png" medium="image">
			<media:title type="html">NumberStepper Tut Step 1</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/numberstepper-tut-step-2.png" medium="image">
			<media:title type="html">NumberStepper Tut Step 2</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/numberstepper-tut-step-32.png" medium="image">
			<media:title type="html">NumberStepper Tut Step 3</media:title>
		</media:content>
	</item>
		<item>
		<title>NSCollectionView Tutorial for Dummies (Xcode 3.1.3)</title>
		<link>http://andrehoffmann.wordpress.com/2009/08/29/nscollectionview-tutorial-for-dummies-xcode-3-1-3/</link>
		<comments>http://andrehoffmann.wordpress.com/2009/08/29/nscollectionview-tutorial-for-dummies-xcode-3-1-3/#comments</comments>
		<pubDate>Sat, 29 Aug 2009 17:29:42 +0000</pubDate>
		<dc:creator>andrehoffmann</dc:creator>
				<category><![CDATA[cocoa]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[arraycontroller]]></category>
		<category><![CDATA[nscollectionview]]></category>
		<category><![CDATA[nscollectionviewitem]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://andrehoffmann.wordpress.com/?p=8</guid>
		<description><![CDATA[Note: I wrote this tutorial since I believe that there are way too few resources on the net covering this topic and I would have been wasting way less time if I had found something like this. Please keep in mind that I&#8217;m a ObjC-beginner myself. Introduction This tutorial is written in a way that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrehoffmann.wordpress.com&amp;blog=440791&amp;post=8&amp;subd=andrehoffmann&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Note: I wrote this tutorial since I believe that there are way too few resources on the net covering this topic and I would have been wasting way less time if I had found something like this. Please keep in mind that I&#8217;m a ObjC-beginner myself.</p>
<h2>Introduction</h2>
<p>This tutorial is written in a way that it doesn&#8217;t require much knowledge of cocoa/objective-c, but since I&#8217;m not explaining most of the code, programming experiences will be helpful to understand what you are doing.</p>
<p>The following things will be covered by this tutorial:</p>
<ul>
<li>designing a user interface with buttons and a NSCollectionView</li>
<li>designing a Subview which should be displayed in the CollectionView</li>
<li>feeding the CollectionView using an ArrayController</li>
<li>making the items in the CollectionView selectable</li>
</ul>
<p>This is what the final application at the end of the tutorial will look like:</p>
<p><img class="alignnone size-full wp-image-55" title="NSCollectionView Tut Step 13" src="http://andrehoffmann.files.wordpress.com/2009/08/bild-13.png?w=700" alt="NSCollectionView Tut Step 13"   /></p>
<p><span id="more-8"></span></p>
<h2>Getting Started</h2>
<p>Let&#8217;s start off by creating a fresh project and opening up the MainMenu.nib. Next drag a Collection View from the Library to the main view. It can be found under Library-&gt;Cocoa-&gt;Views &amp; Cells-&gt;Data Views-&gt;Collection View<span style="font-style:normal;">.</span></p>
<p>Do the same with 2 buttons which you can get from Library-&gt;Cocoa-&gt;Views &amp; Cells-&gt;Buttons<span style="font-style:normal;">. Give them a label(&#8220;add&#8221; and &#8220;remove&#8221;) by clicking them twice.</span></p>
<p>Your window should now look similar to this:</p>
<p><img class="alignnone size-full wp-image-13" title="NSCollectionView Tut Step 1" src="http://andrehoffmann.files.wordpress.com/2009/08/bild-1.png?w=700" alt="NSCollectionView Tut Step 1"   /></p>
<p>Also we&#8217;ll be needing a Window Controller: To add it go to Library-&gt;Cocoa-&gt;Objects &amp; Controllers-&gt;Controllers <span style="font-style:normal;">and drag an Object into the Document Window. While having it selected in the Document Window, go to the Identity tab of the Inspector and change the class name to something like &#8220;WindowController&#8221;.</span></p>
<p><span style="font-style:normal;"><br />
</span></p>
<p><img class="alignnone size-full wp-image-14" title="NSCollectionView Tut Step 2" src="http://andrehoffmann.files.wordpress.com/2009/08/bild-2.png?w=700" alt="NSCollectionView Tut Step 2"   /></p>
<p>Keep it selected and go to File-&gt;Write Class Files, accept to save the &#8220;WindowController.m&#8221; and to create a &#8220;WindowController.h&#8221;. Now you should be able to see those 2 files in Xcode. Open up the &#8220;WindowController.h&#8221; which should look like this:</p>
<p><pre class="brush: cpp;">
#import &lt; Cocoa /Cocoa.h&gt;

@interface WindowController : /* Specify a superclass (eg: NSObject or NSView) */ {

}

@end
</pre></p>
<p>As you can see we&#8217;ll have to specify the superclass of our WindowController to do this replace the comment with NSWindowController, save and in the Interface Builder select File-&gt;Reload All Class Files.</p>
<h2>Setting Up The Bindings</h2>
<p><img class="size-full wp-image-22 alignleft" title="NSCollectionView Tut Step 3" src="http://andrehoffmann.files.wordpress.com/2009/08/bild-3.png?w=700" alt="NSCollectionView Tut Step 3"   />Now that we&#8217;re done with the basic interface, let&#8217;s connect the controls to our WindowController.</p>
<p>To do this select the WindowController and open the Identity tab in the Inspector.</p>
<p>Here add two class actions(addAction and removeAction) and one class outlet collectionView of the type NSCollectionView as seen on the left.</p>
<p>Finally, go to File-&gt;Write Class File and merge the changes.</p>
<p>Our WindowController should now look like this:</p>
<h3>WindowController.h</h3>
<p><pre class="brush: cpp;">
#import &lt; Cocoa /Cocoa.h&gt;

@interface WindowController : NSWindowController {
    IBOutlet NSCollectionView *collectionView;
}
- (IBAction)addAction:(id)sender;
- (IBAction)removeAction:(id)sender;
@end
</pre></p>
<h3>WindowController.m</h3>
<p><pre class="brush: cpp;">
#import &quot;WindowController.h&quot;

@implementation WindowController
- (IBAction)addAction:(id)sender {

}

- (IBAction)removeAction:(id)sender {

}
@end
</pre></p>
<p>Go back to the Interface Builder and hold the Ctrl-key while &#8220;drawing&#8221; a line from the add button to the WindowController(in the Document Window).<br />
<img class="alignnone size-full wp-image-28" title="NSCollectionView Tut Step 4" src="http://andrehoffmann.files.wordpress.com/2009/08/bild-4-edit.png?w=700" alt="NSCollectionView Tut Step 4"   /><br />
Release the mouse over the WindowController and select &#8220;addAction&#8221;. The function addAction is now being called whenever the add button is clicked. Repeat this for the remove button and its corresponding removeAction.</p>
<p>Now we&#8217;ll be doing the same just the other way around: Draw a line from the WindowController to the CollectionView and select &#8220;collectionView&#8221;. Do the same with the background of the window and connect it to &#8220;window&#8221;. This will make both the window and the CollectionView accessible from our WindowController.</p>
<p>If you would like to see what connections have been made you could right-click for example on the WindowController and should see this:</p>
<p><img class="alignnone size-full wp-image-30" title="NSCollectionView Tut Step 5" src="http://andrehoffmann.files.wordpress.com/2009/08/bild-5.png?w=700" alt="NSCollectionView Tut Step 5"   /></p>
<h2>Designing the NSCollectionViewItem</h2>
<p>Next up, we would like to design the interface of an item in our CollectionView. Therefore, go to Xcode and select File-&gt;New File-&gt;View XIB, name it however you like(f.e. collectionItem) and open it up in Interface Builder.</p>
<p>Make the View a little bit smaller &#8211; I chose 150&#215;150(you can set that in the Size tab of the Inspector).</p>
<p>Drag a Label(Library-&gt;Cocoa-&gt;Views &amp; Cells-&gt;Inputs &amp; Values) and a Segmented Control(same place as the Label) onto the view. Change the text of the label to &#8220;Joe&#8221;, change the number of segments of the Segmented Control(Attributes tab of the Inspector) to &#8217;2&#8242; and change their labels to &#8220;male&#8221; and &#8220;female&#8221;. This it what you should have got:</p>
<p><img class="alignnone size-full wp-image-31" title="NSCollectionView Tut Step 6" src="http://andrehoffmann.files.wordpress.com/2009/08/bild-6.png?w=700" alt="NSCollectionView Tut Step 6"   /></p>
<h2>Setting Up the Bindings of the CollectionViewItem</h2>
<p>First change the Class Identity of the File&#8217;s Owner(in the Document Window) to &#8220;CollectionItem&#8221;(in the Identity tab of the Inspector). At the same place add a Class Action &#8220;updateGender&#8221; and two Class Outlets gender(NSSegmentedControl) and name(NSTextField):</p>
<p><img class="alignnone size-full wp-image-32" title="NSCollectionView Tut Step 7" src="http://andrehoffmann.files.wordpress.com/2009/08/bild-7.png?w=700" alt="NSCollectionView Tut Step 7"   /></p>
<p>Now you should go to File-&gt;Write Class Files. In the &#8220;CollectionViewItem.h&#8221; you&#8217;ll have to specify the superclass of the CollectionItem(remember the WindowController.h?) . This time we want to extend NSCollectionViewItem so you need to put that there instead of the comment:</p>
<h3>CollectionItem.h</h3>
<p><pre class="brush: cpp;">
#import &lt; Cocoa /Cocoa.h&gt;

@interface CollectionItem : NSCollectionViewItem {
    IBOutlet NSSegmentedControl *gender;
    IBOutlet NSTextField *name;
}
- (IBAction)updateGender:(id)sender;
@end
</pre></p>
<h3>CollectionItem.m</h3>
<p><pre class="brush: cpp;">
#import &quot;CollectionItem.h&quot;

@implementation CollectionItem
- (IBAction)updateGender:(id)sender {

}
@end
</pre></p>
<p>Now all that&#8217;s left for us to do in this view is to connect File&#8217;s owner with the text field(name) and with the seg-control(gender), the seg-control with the File&#8217;s owner(updateGender) and finally the File&#8217;s owner with the view itself(view &#8211; click in the background).</p>
<p><img class="alignnone size-full wp-image-46" title="NSCollectionView Tut Step 8" src="http://andrehoffmann.files.wordpress.com/2009/08/bild-81.png?w=700" alt="NSCollectionView Tut Step 8"   /></p>
<h2>Make the CollectionItem load its Interface</h2>
<p>Everytime we add a new CollectionItem to the CollectionView a new subinterface has to be loaded(the collectionItem.xib). To do this we&#8217;ll have to add the copyWithZone method to our CollectionItem:</p>
<h3>CollectionItem.h</h3>
<p><pre class="brush: cpp;">
#import &lt; Cocoa /Cocoa.h&gt;

@interface CollectionItem : NSCollectionViewItem {
    IBOutlet NSSegmentedControl *gender;
    IBOutlet NSTextField *name;
}
- (IBAction)updateGender:(id)sender;
- (id)copyWithZone:(NSZone *)zone;
@end
</pre></p>
<h3>CollectionItem.m</h3>
<p><pre class="brush: cpp;">
#import &quot;CollectionItem.h&quot;

@implementation CollectionItem
- (IBAction)updateGender:(id)sender {

}

-(id)copyWithZone:(NSZone *)zone
{
	id result = [super copyWithZone:zone];

	[NSBundle loadNibNamed:@&quot;collectionItem&quot; owner:result];

	return result;
}
@end
</pre></p>
<p>Where @&#8221;collectionItem&#8221;  refers to the name of the &#8220;collectionItem.xib&#8221;.</p>
<p>Adding an ArrayController</p>
<p>To be able to feed the CollectionView with items we&#8217;ll need to add an ArrayController.<br />
To do that go to our MainMenu.xib in Interface Builder and drag an ArrayController(Library-&gt;Cocoa-&gt;Objects &amp; Controllers-&gt;Controllers) to the Document Window.</p>
<p>This might also be a good moment to change the Class Identity of the Collection View Item(which has been automatically created when we added the NSCollectionView)  from NSCollectionViewItem to &#8220;CollectionItem&#8221;.</p>
<p>In Xcode open the WindowController.h and add the outlet arrayController, the mutableArray collection and the property collection:</p>
<h3 style="font-size:1.17em;">WindowController.h</h3>
<p><pre class="brush: cpp;">
#import &lt; Cocoa /Cocoa.h&gt;

@interface WindowController : NSWindowController {
    IBOutlet NSCollectionView *collectionView;
	IBOutlet NSArrayController *arrayController;

	NSMutableArray* collection;
}
- (IBAction)addAction:(id)sender;
- (IBAction)removeAction:(id)sender;

- (void) awakeFromNib;

@property (copy) NSArray* collection;

@end
</pre></p>
<p>In the CollectionItem.m the method awakeFromNib needs to implemented and a synthesize statement needs to be added:</p>
<h3 style="font-size:1.17em;">WindowController.m</h3>
<p><pre class="brush: cpp;">
#import &quot;WindowController.h&quot;

@implementation WindowController

@synthesize collection;

- (IBAction)addAction:(id)sender {

}

- (IBAction)removeAction:(id)sender {

}

- (void) awakeFromNib {

	collection = [[NSMutableArray alloc] init];
	NSSize size = NSMakeSize(150, 150);
	[collectionView setMinItemSize:size];
	[collectionView setMaxItemSize:size];
}

@end
</pre></p>
<p>Save and switch to the Interface Builder where you should reload the class files(File-&gt;Reload All Class Files).<br />
Select the ArrayController and in the Bindings Pane of the Inspector and under &#8220;Content Array&#8221; set &#8220;Bind to:&#8221; to &#8220;Window Controller&#8221; and the &#8220;Model Key Path&#8221; to controller(as we named the property earlier).<br />
<img class="alignnone size-full wp-image-41" title="NSCollectionView Tut Step 9" src="http://andrehoffmann.files.wordpress.com/2009/08/bild-9.png?w=700" alt="NSCollectionView Tut Step 9"   /></p>
<p>Hold Ctrl and draw a line from the WindowController to the ArrayController and bind it to &#8220;arrayController&#8221;.<br />
We&#8217;re almost done all that&#8217;s left to do is to connect the CollectionView to the Array:<br />
Click on the CollectionView multiple times and make sure it really is the CollectionView and not the ScrollView surrounding it. Go to the Bindings tab and bind &#8220;Content&#8221; to the ArrayController with arrangedObjects as the &#8220;Controller Key&#8221;. Also bind &#8220;Selection Indexes&#8221; to the ArrayController with the &#8220;Controller Key&#8221; set to &#8220;selectionIndexes&#8221;.<br />
<img class="alignnone size-full wp-image-42" title="NSCollectionView Tut Step 10" src="http://andrehoffmann.files.wordpress.com/2009/08/bild-10.png?w=700" alt="NSCollectionView Tut Step 10"   /></p>
<h2>Finally:  Adding items to the CollectionView</h2>
<p>The time has come to implement the addAction in the WindowController and take our application for a test run.</p>
<p>Open up the WindowController.m in Xcode and copy over the body of the addAction method:</p>
<p><pre class="brush: cpp;">
- (IBAction)addAction:(id)sender {
	int index = [[arrayController arrayController &quot;&gt;arrangedObjects] count];
	[arrayController insertObject: [NSDictionary dictionaryWithObjectsAndKeys:@&quot;Jon&quot;, @&quot;Name&quot;, @&quot;Male&quot;, @&quot;Gender&quot;,nil] atArrangedObjectIndex:index];
}

- (IBAction)removeAction:(id)sender {
 	int index = [[arrayController arrangedObjects] count];
	if ( index &gt; 0) {
		index--;
		[arrayController removeObjectAtArrangedObjectIndex:index];
	}
}
</pre></p>
<p>Now you can finally start and play with it. Even though it doesn&#8217;t do much yet, I hope you can already see how much potential  and how many use cases the CollectionView actually has. At least that&#8217;s what I thought when I played around with it for the first time.</p>
<p>Just for you to check if you did everything the way I described it(at least how I tried to):</p>
<p><img class="alignnone size-full wp-image-50" title="NSCollectionView Tut Step 11" src="http://andrehoffmann.files.wordpress.com/2009/08/bild-11.png?w=700" alt="NSCollectionView Tut Step 11"   /></p>
<h2>Enuf with the Joes</h2>
<p>Now we want to have other names than Joe.</p>
<p>We&#8217;ll start by adding a new class(File-&gt;New File-&gt;Objective-C class) and name it &#8220;Person.m&#8221;:</p>
<h3>Person.h</h3>
<p><pre class="brush: cpp;">
#import &lt; Cocoa /Cocoa.h&gt;

@interface Person : NSObject {

}

+ (NSString*)getRandomMaleName;
+ (NSString*)getRandomFemaleName;
+ (NSString*)getRandomGender;

@end
</pre></p>
<h3>Person.m</h3>
<p><pre class="brush: cpp;">
#import &quot;Person.h&quot;
#include &lt; stdlib .h&gt;

@implementation Person

+ (NSString*)getRandomMaleName {
	NSMutableArray *names = [NSMutableArray NSMutableArray &quot;&gt;arrayWithObjects:@&quot;Jon&quot;, @&quot;Steve&quot;, @&quot;Benjamin&quot;, @&quot;Chris&quot;, @&quot;Andre&quot;, @&quot;Tom&quot;, @&quot;Paul&quot;, @&quot;Peter&quot;, @&quot;Ron&quot;, @&quot;Robert&quot;, @&quot;Matthew&quot;, @&quot;Max&quot;, @&quot;Alex&quot;, @&quot;Sebastian&quot;, nil];

	int randIndex = arc4random() % [names count];
	return [names objectAtIndex:randIndex];
}

+ (NSString*)getRandomFemaleName {
	NSMutableArray *names = [NSMutableArray arrayWithObjects:@&quot;Abbey&quot;, @&quot;Alice&quot;, @&quot;Cindy&quot;, @&quot;Chrissy&quot;, @&quot;Gina&quot;, @&quot;Hannah&quot;, @&quot;Lena&quot;, @&quot;Lilli&quot;, @&quot;Linda&quot;, @&quot;Monica&quot;, @&quot;Nadine&quot;, @&quot;Nancy&quot;, @&quot;Theresa&quot;, @&quot;Tiffany&quot;, nil];

	int randIndex = arc4random() % [names count];
	return [names objectAtIndex:randIndex];
}

+ (NSString*)getRandomGender {
	NSMutableArray *genders = [NSMutableArray arrayWithObjects:@&quot;male&quot;, @&quot;female&quot;, nil];

	int randIndex = arc4random() % [genders count];
	return [genders objectAtIndex:randIndex];
}

@end
</pre></p>
<p>The class Person offers 3 static methods:</p>
<ul>
<li>getRandomMaleName: randomly returns a male&#8217;s first name</li>
<li>getRandomFemaleName: randomly returns a female&#8217;s first name</li>
<li>getRandomGender: randomly returns either @&#8221;male&#8221; or @&#8221;female&#8221;</li>
</ul>
<p>Now we can slightly modify the addAction in the WindowController.m to generate a random name for a random gender when adding the item:</p>
<p>Add the following line at the top of the file:</p>
<p><pre class="brush: cpp;">
#import &quot;Person.h&quot;
</pre></p>
<p>Add copy over the body of the addAction method:</p>
<p><pre class="brush: cpp;">
- (IBAction)addAction:(id)sender {
	int index = [[arrayController arrayController &quot;&gt;arrangedObjects] count];
	NSString *gender = [Person getRandomGender];
	NSString *name;

	if ([gender isEqualToString:@&quot;male&quot;]) {
		name = [Person getRandomMaleName];
	} else {
		name = [Person getRandomFemaleName];
	}

	[arrayController	insertObject: [NSDictionary dictionaryWithObjectsAndKeys:
										name,		@&quot;Name&quot;,
										gender,		@&quot;Gender&quot;,
										nil]
						atArrangedObjectIndex:index];
}
</pre></p>
<p>But our CollectionItem doesn&#8217;t use the supplied informations yet, so we&#8217;ll add the method setRepresentedObject in CollectionItem. Also we&#8217;ll implement the updateGender method which gets called whenever you click on the segmented control:</p>
<h3>CollectionItem.h</h3>
<p><pre class="brush: cpp;">
#import &lt; Cocoa/ Cocoa.h&gt;

@interface CollectionItem : NSCollectionViewItem {
    IBOutlet NSSegmentedControl *gender;
    IBOutlet NSTextField *name;
}
- (IBAction)updateGender:(id)sender;
- (id)copyWithZone:(NSZone *)zone;
- (void)setRepresentedObject:(id)object;

@end
</pre></p>
<h3>CollectionItem.h</h3>
<p><pre class="brush: cpp;">
#import &quot;CollectionItem.h&quot;
#import &quot;Person.h&quot;

@implementation CollectionItem
- (IBAction)updateGender:(id)sender {
	NSString *newName;
    switch ([gender	selectedSegment]) {
		case 0:
			NSString &quot;&gt;newName = [Person getRandomMaleName];
			break;
		default:
			newName = [Person getRandomFemaleName];
			break;
	}

	[name setStringValue:newName];
}

-(id)copyWithZone:(NSZone *)zone
{
	id result = [super copyWithZone:zone];

	[NSBundle loadNibNamed:@&quot;collectionItem&quot; owner:result];

	return result;
}

- (void)setRepresentedObject:(id)object {
	[super setRepresentedObject:	object];

	if (object == nil)
		return;

	NSDictionary* data	= (NSDictionary*) [self representedObject];
	NSString* newName	= (NSString*)[data valueForKey:@&quot;Name&quot;];
	NSString* newGender	= (NSString*)[data valueForKey:@&quot;Gender&quot;];

	[name setStringValue:newName];

	if ([newGender isEqualToString:@&quot;male&quot;]) {
		[gender setSelectedSegment:0];
	} else {
		[gender setSelectedSegment:1];
	}
}

@end
</pre></p>
<p>You can now compile &amp; run the application.</p>
<p><img class="alignnone size-full wp-image-51" title="NSCollectionView Tut Step 12" src="http://andrehoffmann.files.wordpress.com/2009/08/bild-12.png?w=700" alt="NSCollectionView Tut Step 12"   /></p>
<h3>Making the Items Selectable</h3>
<p>To make the items selectable open the main interface(mainMenu.xib) in Interface Builder and while having the CollectionView selected go to the Attributes tab and enable &#8220;Selectable&#8221;.</p>
<p>Now our items are selectable, but we don&#8217;t see when they are yet. We&#8217;ll have to change the background color when an item is being selected.</p>
<p>Here&#8217;s how:  Open the collectionItem.xib and select the View in the Document Window. Change its Class Identity(Identity tab) to NSBox.</p>
<p>Next add those two functions to the CollectionItem.m:</p>
<p><pre class="brush: cpp;">
- (void)setSelected:(BOOL)flag {
	[super setSelected:	flag];

	NSBox *view	= (NSBox*) [self view];
	NSColor *color;
	NSColor *lineColor;

	if (flag) {
		color		= [NSColor selectedControlColor];
		lineColor	= [NSColor blackColor];
	} else {
		color		= [NSColor controlBackgroundColor];
		lineColor	= [NSColor controlBackgroundColor];
	}

	[view setBorderColor:lineColor];
	[view setFillColor:color];
}

- (void) awakeFromNib {
	NSBox *view	= (NSBox*) [self view];
	[view setTitlePosition:NSNoTitle];
	[view setBoxType:NSBoxCustom];
	[view setCornerRadius:8.0];
	[view setBorderType:NSLineBorder];
}
</pre></p>
<p>That&#8217;s it!<br />
<img class="alignnone size-full wp-image-55" title="NSCollectionView Tut Step 13" src="http://andrehoffmann.files.wordpress.com/2009/08/bild-13.png?w=700" alt="NSCollectionView Tut Step 13"   /><br />
BTW: You can also use the arrow keys.</p>
<h2>Conclusion</h2>
<p>That&#8217;s all I wanted to show in this tutorial. I hope you enjoyed it, even though the final app doesn&#8217;t do anything useful. It&#8217;s main purpose was to show how the NSCollectionView works and what can be done with it. The cool thing about the CollectionView is that you can dynamically create a set of views that can be customized for its purpose and create a nice user experience.</p>
<p>The final source code can be downloaded <a href="http://antiHERO2006.googlepages.com/NSCollectionViewTest.zip">here</a>.</p>
<p>One last thing: If you want to know what item is currently selected use [collectionView selectionIndexes] which returns a <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSIndexSet_Class/Reference/Reference.html">NSIndexSet</a>.</p>
<p>Feel free to leave suggestions or the like in the comments.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/andrehoffmann.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/andrehoffmann.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/andrehoffmann.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/andrehoffmann.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/andrehoffmann.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/andrehoffmann.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/andrehoffmann.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/andrehoffmann.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/andrehoffmann.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/andrehoffmann.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/andrehoffmann.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/andrehoffmann.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/andrehoffmann.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/andrehoffmann.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=andrehoffmann.wordpress.com&amp;blog=440791&amp;post=8&amp;subd=andrehoffmann&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://andrehoffmann.wordpress.com/2009/08/29/nscollectionview-tutorial-for-dummies-xcode-3-1-3/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4652e44d290083315bee7e5bd846337d?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">André Hoffmann</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/bild-13.png" medium="image">
			<media:title type="html">NSCollectionView Tut Step 13</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/bild-1.png" medium="image">
			<media:title type="html">NSCollectionView Tut Step 1</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/bild-2.png" medium="image">
			<media:title type="html">NSCollectionView Tut Step 2</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/bild-3.png" medium="image">
			<media:title type="html">NSCollectionView Tut Step 3</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/bild-4-edit.png" medium="image">
			<media:title type="html">NSCollectionView Tut Step 4</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/bild-5.png" medium="image">
			<media:title type="html">NSCollectionView Tut Step 5</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/bild-6.png" medium="image">
			<media:title type="html">NSCollectionView Tut Step 6</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/bild-7.png" medium="image">
			<media:title type="html">NSCollectionView Tut Step 7</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/bild-81.png" medium="image">
			<media:title type="html">NSCollectionView Tut Step 8</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/bild-9.png" medium="image">
			<media:title type="html">NSCollectionView Tut Step 9</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/bild-10.png" medium="image">
			<media:title type="html">NSCollectionView Tut Step 10</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/bild-11.png" medium="image">
			<media:title type="html">NSCollectionView Tut Step 11</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/bild-12.png" medium="image">
			<media:title type="html">NSCollectionView Tut Step 12</media:title>
		</media:content>

		<media:content url="http://andrehoffmann.files.wordpress.com/2009/08/bild-13.png" medium="image">
			<media:title type="html">NSCollectionView Tut Step 13</media:title>
		</media:content>
	</item>
	</channel>
</rss>
