<?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>Learning Monomac</title>
	<atom:link href="http://blog.ac-graphic.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ac-graphic.net</link>
	<description>Use Monomac or Xamarin.Mac to build native mac apps</description>
	<lastBuildDate>Fri, 10 Jun 2016 09:31:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.5.4</generator>
	<item>
		<title>Cocoa Programming L22: Saving Documents</title>
		<link>http://blog.ac-graphic.net/cocoa-programming-l22-saving-documents/</link>
		<pubDate>Thu, 29 Aug 2013 10:00:51 +0000</pubDate>
		<dc:creator><![CDATA[Antoine Cailliau]]></dc:creator>
				<category><![CDATA[Cocoa Programming]]></category>

		<guid isPermaLink="false">http://blog.ac-graphic.net/?p=308</guid>
		<description><![CDATA[In this tutorial, we will see how we can save the information of our document-based application. This tutorial continues the work we started in the previous lesson. As recall, our data are stored in the Assignments property of the MyDocument class. As in the video, the first step is to implement the two methods related &#8230; <a href="http://blog.ac-graphic.net/cocoa-programming-l22-saving-documents/" class="more-link">Continue reading<span class="screen-reader-text"> "Cocoa Programming L22: Saving Documents"</span></a>]]></description>
				<content:encoded><![CDATA[<p>In this tutorial, we will see how we can save the information of our document-based application. This tutorial continues the work we started in the previous <a title="Cocoa Programming L20: NSTableView Bindings" href="http://blog.ac-graphic.net/cocoa-programming-l20-nstableview-bindings/">lesson</a>.</p>
<p><iframe width="840" height="630" src="https://www.youtube.com/embed/yfiOGQmUVjE?feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>As recall, our data are stored in the <code>Assignments</code> property of the <code>MyDocument</code> class. As in the video, the first step is to implement the two methods related to our encoding/decoding in the <code>Assignment</code> class. The first is a constructor that will build the instance based on the encoded data.</p>
<pre class="prettyprint">[Export ("initWithCoder:")]
public Assignment (NSCoder coder)
{
	Name = coder.DecodeObject ("name") as NSString;
	Grade = coder.DecodeInt ("grade");
}</pre>
<p>The second returns the encoded data corresponding to the instance.</p>
<pre class="prettyprint">[Export ("encodeWithCoder:")]
public void Encode (NSCoder coder)
{
	coder.Encode(new NSString (Name), "name");
	coder.Encode(Grade, "grade");
}</pre>
<p>Note that both method need to be exported using the <code>Export</code> attribute to make sure that the backend knows what and when to call.</p>
<p>Now that our <code>Assignment</code> class contains the methods for decoding and encoding data we can save and load the list. Go back to the <code>MyDocument</code> class. We need to override <code>GetAsData</code> and <code>ReadFromData</code>. Let&#8217;s start with the method that will encode the data.</p>
<pre class="prettyprint">public override NSData GetAsData (string documentType, out NSError outError)
{
	outError = NSError.FromDomain (NSError.OsStatusErrorDomain, -4);

	var mutableData = new NSMutableData();
	var archiver = new NSKeyedArchiver (mutableData);
	Assignments.EncodeTo (archiver);
	archiver.FinishEncoding ();

	return mutableData;
}</pre>
<p>Next, we need to load the data and update the <code>Assignments</code> property.</p>
<pre class="prettyprint">public override bool ReadFromData (NSData data, string typeName, out NSError outError)
{
	outError = NSError.FromDomain (NSError.OsStatusErrorDomain, -4);
	var unarchiver = new NSKeyedUnarchiver (data);
	Assignments = Activator.CreateInstance(typeof(NSMutableArray), 
	                                       new object[] { unarchiver }) as NSMutableArray;
	return true;
}</pre>
<p>We can enjoy the Cocoa framework that manage the open/save windows, and the saving of the file. All we need to implement is archiving and de-archiving the datas.</p>
<p>To edit the info about the document type, go to Xamarin Studio and right-click on the project and then select <em>Options</em>.</p>
<p><a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-29-at-09.20.24.png"><img class="aligncenter size-medium wp-image-311" alt="Screen Shot 2013-08-29 at 09.20.24" src="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-29-at-09.20.24-172x300.png" width="172" height="300" /></a></p>
<p>In the new window, click on <em>Mac OS X Application</em> and go the <em>Advanced</em> tab. There you can find the same options as the one provided in Xcode.</p>
<p><a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-29-at-09.19.46.png"><img class="aligncenter size-medium wp-image-312" alt="Screen Shot 2013-08-29 at 09.19.46" src="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-29-at-09.19.46-300x193.png" width="300" height="193" srcset="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-29-at-09.19.46-300x193.png 300w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-29-at-09.19.46-1024x659.png 1024w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-29-at-09.19.46-624x401.png 624w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-29-at-09.19.46.png 1036w" sizes="(max-width: 300px) 85vw, 300px" /></a></p>
<p>And we&#8217;re done. You can now save and open files.</p>
<p><a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-29-at-09.32.24.png"><img src="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-29-at-09.32.24-300x265.png" alt="Screen Shot 2013-08-29 at 09.32.24" width="300" height="265" class="aligncenter size-medium wp-image-315" srcset="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-29-at-09.32.24-300x265.png 300w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-29-at-09.32.24.png 621w" sizes="(max-width: 300px) 85vw, 300px" /></a></p>
<p>As usual, you can download the code here. Stay tuned by liking our <a href="http://www.facebook.com/learning.monomac">Facebook page</a> and following us on <a href="https://twitter.com/LearningMonoMac">Twitter</a>.</p>
]]></content:encoded>
			</item>
		<item>
		<title>Cocoa Programming L20: NSTableView Bindings</title>
		<link>http://blog.ac-graphic.net/cocoa-programming-l20-nstableview-bindings/</link>
		<comments>http://blog.ac-graphic.net/cocoa-programming-l20-nstableview-bindings/#comments</comments>
		<pubDate>Wed, 28 Aug 2013 10:00:32 +0000</pubDate>
		<dc:creator><![CDATA[Antoine Cailliau]]></dc:creator>
				<category><![CDATA[Cocoa Programming]]></category>

		<guid isPermaLink="false">http://blog.ac-graphic.net/?p=300</guid>
		<description><![CDATA[In this tutorial, we will see how bindings can work with NSTableView. We already covered NSTableView in the Lesson 13, Lesson 14, and Lesson 15. First, we will create a document-based project. To create such a project, select Monomac Document Project when creating the new project. First, create the class that will represent an assignment. &#8230; <a href="http://blog.ac-graphic.net/cocoa-programming-l20-nstableview-bindings/" class="more-link">Continue reading<span class="screen-reader-text"> "Cocoa Programming L20: NSTableView Bindings"</span></a>]]></description>
				<content:encoded><![CDATA[<p>In this tutorial, we will see how bindings can work with <code>NSTableView</code>. We already covered <code>NSTableView</code> in the <a title="Cocoa Programming L13: NSTableView Intro" href="http://blog.ac-graphic.net/cocoa-programming-l13-nstableview-intro/">Lesson 13</a>, <a title="Cocoa Programming L14: NSTableView adding objects" href="http://blog.ac-graphic.net/cocoa-programming-l14-nstableview-adding-objects/">Lesson 14</a>, and <a title="Cocoa Programming L15: NSTableView editing values" href="http://blog.ac-graphic.net/cocoa-programming-l15-nstableview-editing-values/">Lesson 15</a>.</p>
<p><iframe width="840" height="630" src="https://www.youtube.com/embed/LJ6gP3HxVc8?feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>First, we will create a document-based project. To create such a project, select <em>Monomac Document Project</em> when creating the new project.</p>
<p><a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-28-at-13.52.31.png"><img class="aligncenter size-medium wp-image-301" alt="Screen Shot 2013-08-28 at 13.52.31" src="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-28-at-13.52.31-300x182.png" width="300" height="182" srcset="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-28-at-13.52.31-300x182.png 300w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-28-at-13.52.31-624x379.png 624w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-28-at-13.52.31.png 943w" sizes="(max-width: 300px) 85vw, 300px" /></a></p>
<p>First, create the class that will represent an assignment. Note that it is important that the class extend <code>NSObject</code> and to be registered using the <code>Register</code> attribute. The properties needs to be exported (as explained in <a href="http://blog.ac-graphic.net/cocoa-programming-l19-cocoa-bindings/" title="Cocoa Programming L19: Cocoa Bindings">Lesson 19</a>). </p>
<pre class="prettyprint">[Register("assignment")]
public class Assignment : NSObject
{
	[Export("name")]
	public string Name {
		get;
		set;
	}

	[Export("grade")]
	public int Grade {
		get;
		set;
	}

	public Assignment ()
	{
		Name = "Calc Quizz";
		Grade = 95;
	}
}</pre>
<p>In the <em>MyDocument.cs</em> file, add a new property that will contain the list of assignments. Since we will be using the Cocoa Bindings, you need to export the property.</p>
<pre class="prettyprint">[Export("assignments")]
public NSMutableArray Assignments {
	get;
	set;
}</pre>
<p>Of course, you need to initialize it:</p>
<pre class="prettyprint">void Initialize() {
	Assignments = new NSMutableArray ();
}</pre>
<p>Don&#8217;t forget to call <code>Initialize</code> in the constructors. The rest of the video is only about the interface builder <img src="https://s.w.org/images/core/emoji/72x72/1f642.png" alt="&#x1f642;" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p><a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-28-at-14.18.40.png"><img src="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-28-at-14.18.40-300x265.png" alt="Screen Shot 2013-08-28 at 14.18.40" width="300" height="265" class="aligncenter size-medium wp-image-306" srcset="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-28-at-14.18.40-300x265.png 300w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-28-at-14.18.40.png 621w" sizes="(max-width: 300px) 85vw, 300px" /></a></p>
<p>You can download the code <a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Lesson20.zip">here</a>. Don&#8217;t forget to like our <a href="http://www.facebook.com/learning.monomac">Facebook page</a> and to follow us on <a href="https://twitter.com/LearningMonoMac">Twitter</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ac-graphic.net/cocoa-programming-l20-nstableview-bindings/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cocoa Programming L19: Cocoa Bindings</title>
		<link>http://blog.ac-graphic.net/cocoa-programming-l19-cocoa-bindings/</link>
		<comments>http://blog.ac-graphic.net/cocoa-programming-l19-cocoa-bindings/#comments</comments>
		<pubDate>Tue, 27 Aug 2013 10:00:41 +0000</pubDate>
		<dc:creator><![CDATA[Antoine Cailliau]]></dc:creator>
				<category><![CDATA[Cocoa Programming]]></category>

		<guid isPermaLink="false">http://blog.ac-graphic.net/?p=294</guid>
		<description><![CDATA[In this tutorial, we will have a look at Cocoa bindings. These bindings help in reducing the amount of code to write. This tutorial will also present key-value coding and key-value observing. First, build the interface, as indicated in the video. Next, we will declare the necessary properties in the window controller. public bool CheckBoxIsEnabled &#8230; <a href="http://blog.ac-graphic.net/cocoa-programming-l19-cocoa-bindings/" class="more-link">Continue reading<span class="screen-reader-text"> "Cocoa Programming L19: Cocoa Bindings"</span></a>]]></description>
				<content:encoded><![CDATA[<p>In this tutorial, we will have a look at Cocoa bindings. These bindings help in reducing the amount of code to write. This tutorial will also present key-value coding and key-value observing.</p>
<p><iframe width="840" height="630" src="https://www.youtube.com/embed/ESk6YLDtGR8?feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>First, build the interface, as indicated in the video. Next, we will declare the necessary properties in the window controller. </p>
<pre class="prettyprint">public bool CheckBoxIsEnabled {
	get;
	set;
}

public int Amount {
	get;
	set;
}</pre>
<p>To reference these properties in the interface builder, we need to export them. So, for both properties, add the <code>Export</code> attribute. For example</p>
<pre class="prettyprint">[Export("checkBoxIsEnabled")]
public bool CheckBoxIsEnabled {
	get;
	set;
}</pre>
<p>In the interface builder, you can now make the connection in the Binding inspector. Make sure you select <em>File&#8217;s Owner</em> in the list for <em>Bind to:</em>. The model key path shall correspond to the exported value.</p>
<p><a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-28-at-13.39.32.png"><img src="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-28-at-13.39.32.png" alt="Screen Shot 2013-08-28 at 13.39.32" width="241" height="150" class="aligncenter size-full wp-image-295" /></a></p>
<p>And we&#8217;re done. Remember to check the box <em>Continuous</em> if you want the slider to update the value when you move the knob. To set initial values, you can define them in the <code>Initialize</code> method.</p>
<pre class="prettyprint">void Initialize ()
{
	CheckBoxIsEnabled = true;
	Amount = 20;
}</pre>
<p><a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-28-at-13.47.31.png"><img src="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-28-at-13.47.31-300x135.png" alt="Screen Shot 2013-08-28 at 13.47.31" width="300" height="135" class="aligncenter size-medium wp-image-298" srcset="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-28-at-13.47.31-300x135.png 300w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-28-at-13.47.31.png 484w" sizes="(max-width: 300px) 85vw, 300px" /></a></p>
<p>You can download the code <a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Lesson19.zip">here</a>. Don&#8217;t forget to like our <a href="http://www.facebook.com/learning.monomac">Facebook page</a> and to follow us on <a href="https://twitter.com/LearningMonoMac">Twitter</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ac-graphic.net/cocoa-programming-l19-cocoa-bindings/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Cocoa Programming L18: Multiple Windows</title>
		<link>http://blog.ac-graphic.net/cocoa-programming-l18-multiple-windows/</link>
		<pubDate>Mon, 26 Aug 2013 10:00:57 +0000</pubDate>
		<dc:creator><![CDATA[Antoine Cailliau]]></dc:creator>
				<category><![CDATA[Cocoa Programming]]></category>

		<guid isPermaLink="false">http://blog.ac-graphic.net/?p=259</guid>
		<description><![CDATA[In this tutorial, we will learn how to use multiple windows in one single application. To create a new window, right-click on the project item in the left sidebar, go to Add and hit New File&#8230;. In the new window, choose Cocoa Window with Controller, specify a name and hit New You can now edit &#8230; <a href="http://blog.ac-graphic.net/cocoa-programming-l18-multiple-windows/" class="more-link">Continue reading<span class="screen-reader-text"> "Cocoa Programming L18: Multiple Windows"</span></a>]]></description>
				<content:encoded><![CDATA[<p>In this tutorial, we will learn how to use multiple windows in one single application. </p>
<p><iframe width="840" height="630" src="https://www.youtube.com/embed/Z1Erw7aP0EQ?feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>To create a new window, right-click on the project item in the left sidebar, go to <em>Add</em> and hit <em>New File&#8230;</em>. </p>
<p><a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-26-at-09.45.53.png"><img src="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-26-at-09.45.53-285x300.png" alt="Screen Shot 2013-08-26 at 09.45.53" width="285" height="300" class="aligncenter size-medium wp-image-287" srcset="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-26-at-09.45.53-285x300.png 285w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-26-at-09.45.53.png 502w" sizes="(max-width: 285px) 85vw, 285px" /></a></p>
<p>In the new window, choose <em>Cocoa Window with Controller</em>, specify a name and hit <em>New</em></p>
<p><a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-26-at-09.46.13.png"><img src="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-26-at-09.46.13-300x229.png" alt="Screen Shot 2013-08-26 at 09.46.13" width="300" height="229" class="aligncenter size-medium wp-image-288" srcset="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-26-at-09.46.13-300x229.png 300w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-26-at-09.46.13-624x478.png 624w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-26-at-09.46.13.png 741w" sizes="(max-width: 300px) 85vw, 300px" /></a></p>
<p>You can now edit the window by double-clicking on the <em>Downloads.xib</em> file. Note that you don&#8217;t need to create the controller by hand, it is automatically created by Xamarin. Same applies for the window outlet.</p>
<p>You can create an action connected to the menu item in your <em>AppDelegate.h</em> file.</p>
<p><a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-26-at-09.54.43.png"><img src="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-26-at-09.54.43-300x77.png" alt="Screen Shot 2013-08-26 at 09.54.43" width="300" height="77" class="aligncenter size-medium wp-image-290" srcset="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-26-at-09.54.43-300x77.png 300w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-26-at-09.54.43.png 527w" sizes="(max-width: 300px) 85vw, 300px" /></a></p>
<p>Go back to Xamarin Studio to implement the method. As usual, go the the corresponding file (<em>AppDelegate.cs</em> in our case), type <code>partial</code> and hit space. This will trigger the autocompletion, select the method to complete and hit enter.</p>
<p><a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-26-at-09.55.38.png"><img src="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-26-at-09.55.38-300x131.png" alt="Screen Shot 2013-08-26 at 09.55.38" width="300" height="131" class="aligncenter size-medium wp-image-291" srcset="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-26-at-09.55.38-300x131.png 300w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-26-at-09.55.38.png 492w" sizes="(max-width: 300px) 85vw, 300px" /></a></p>
<p>All you need to implement is to create a download controller and call the <code>ShowWindow</code> method with <code>null</code> as parameter. To avoid creating the controller multiple time, we will instantiate it if the field <code></code> contains <code>null</code>. Your code should then look like this:</p>
<pre class="prettyprint">DownloadsController downloadController;

partial void showDownloads (NSObject sender)
{
	if (downloadController == null)
		downloadController = new DownloadsController ();
	downloadController.ShowWindow (sender);
}</pre>
<p>And, we are done. As usual, you can download the code <a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Lesson18.zip">here</a>. Don&#8217;t forget to like <a href="https://www.facebook.com/learning.monomac">our Facebook page</a> and to follow us on <a href="https://twitter.com/LearningMonoMac">Twitter</a>.</p>
]]></content:encoded>
			</item>
		<item>
		<title>Core Animation L1: Intro to Animation</title>
		<link>http://blog.ac-graphic.net/core-animation-l1-intro-to-animation/</link>
		<pubDate>Sun, 25 Aug 2013 10:00:26 +0000</pubDate>
		<dc:creator><![CDATA[Antoine Cailliau]]></dc:creator>
				<category><![CDATA[Core Animation]]></category>

		<guid isPermaLink="false">http://blog.ac-graphic.net/?p=262</guid>
		<description><![CDATA[This is the first tutorial about Core Animation. Core Animation is a technology developed by Apple to animate interface in Cocoa. You can do simple and basic animations as well as complex ones. I&#8217;ll we be using Xcode and Xamarin Studio to develop the examples. If you want to know more, the tutorial and the &#8230; <a href="http://blog.ac-graphic.net/core-animation-l1-intro-to-animation/" class="more-link">Continue reading<span class="screen-reader-text"> "Core Animation L1: Intro to Animation"</span></a>]]></description>
				<content:encoded><![CDATA[<p><iframe width="840" height="630" src="https://www.youtube.com/embed/8WqVCNkM7VM?feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>This is the first tutorial about Core Animation. Core Animation is a technology developed by Apple to animate interface in Cocoa. You can do simple and basic animations as well as complex ones. I&#8217;ll we be using Xcode and Xamarin Studio to develop the examples.</p>
<p>If you want to know more, the tutorial and the original video is based on the (sadly out-of-print) book by Bill Dudney <a href="http://pragprog.com/book/bdcora/core-animation-for-mac-os-x-and-the-iphone">&#8220;Core Animation for Mac OS X and the iPhone: Creating Compelling Dynamic User Interfaces&#8221;</a>.</p>
<p>We will first start with an example where we do not have an animation so I make sure that you understand what is an animation. So, consider the following where the image is on the left and then on the right with no elapsed time between the two. There is no smooth transition from one position to the other.</p>
<p><iframe width="840" height="630" src="https://www.youtube.com/embed/EKOVt7AFCT8?feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>Animation is the idea that we progressively change the value over time. So we consider a set of intermediate step between the first and last positions (and other properties). In the following video, we animated the size, position and opacity of the image.</p>
<p><iframe width="840" height="630" src="https://www.youtube.com/embed/eqeyHgfncqY?feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>Now, let&#8217;s dive into the real work and code that using Xamarin Studio and Xcode. So start Xamarin and create an new empty project. See the <a title="Cocoa Programming L1: Getting Started" href="http://blog.ac-graphic.net/cocoa-programming-l1-getting-started/">Lesson 1</a> in our Cocoa programming serie for how-to create such a project.</p>
<p>Once your project is created we will first build the interface. So double-click, in the left sidebar, on the file <em>MainWindow.xib</em> to open Xcode. Once Xcode is loaded, drag&#8217;n&#8217;drop an NSView in the window. See <a title="Cocoa Programming L10: NSImageView" href="http://blog.ac-graphic.net/cocoa-programming-l10-nsimageview/">Cocoa Programming L10: NSImageView</a> for more details about how to create a window with an <code>NSView</code> in it. Once drag&#8217;n&#8217;dropped, go to the <em>Identity inspector</em> in the right toolbar of Xcode and edit the field <em>Class</em> to be set to <code>MoveView</code>.</p>
<p><a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-21-at-10.32.26.png"><img class="aligncenter size-full wp-image-266" alt="Screen Shot 2013-08-21 at 10.32.26" src="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-21-at-10.32.26.png" width="263" height="71" /></a></p>
<p><code>MoveView</code> will be our custom <code>NSView</code> where the animation will take place. Now, we will create that class. Go back to Xamarin Studio and create a new file (hit ⌘N or <em>File</em> &gt; <em>New File&#8230;</em>). Then select <em>MonoMac</em> in the left sidebar and <em>Cocoa View</em> in the right one. Give it the name <code>MoveView</code> and hit <em>New</em>.</p>
<p><a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-21-at-10.34.30.png"><img class="aligncenter size-medium wp-image-268" alt="Screen Shot 2013-08-21 at 10.34.30" src="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-21-at-10.34.30-300x229.png" width="300" height="229" srcset="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-21-at-10.34.30-300x229.png 300w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-21-at-10.34.30-624x476.png 624w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-21-at-10.34.30.png 747w" sizes="(max-width: 300px) 85vw, 300px" /></a></p>
<p>For this tutorial, we will use the constructor taking a rectangle (or a frame) as parameter. This constructor is not added by default in your new file. So add a new constructor with a <code>RectangleF frame</code> as parameter. We also need to export it with the <code>Export</code> attribute so that Xamarin.Mac framework knows that this C# constructor corresponds to the Objective-C constructor. So basically your constructor should look like this:</p>
<pre class="prettyprint">[Export ("initWithFrame:")]
public MoveView (RectangleF frame) : base (frame)
{
	Initialize ();
}</pre>
<p>This view is a sort of canvas for our animation. We will move an <code>NSViewImage</code> from one rectangle to another, as shown in the previous video. First, we will create our <code>imageView</code> and our two frames: one for the start and one for the end. Frames are represented by <code>RectangleF</code>. To do that, declare the three private fields.</p>
<pre class="prettyprint">NSImageView imageView;
RectangleF startFrame;
RectangleF endFrame;</pre>
<p>The key idea is to create the start frame as rectangle on the bottom left (see video) and the end frame at the center right. The size of the frames will be different. Our first frame will be a third of the width and height of the window and the end one will be half of the width of the window. In our constructor, we can first get the width and the height of the window using the frame parameter.</p>
<pre class="prettyprint">var width = frame.Width;
var height = frame.Height;</pre>
<p>Now that we have these two local variable set, we can create our two frames. As explained before, the first frame will be anchored in (0,0) and its width will correspond to the third of the window width whereas the height will be a third of the window height.</p>
<pre class="prettyprint">startFrame = new RectangleF (0, 0, width/3f, height/3f);</pre>
<p>Our end frame is created very similarly</p>
<pre class="prettyprint">endFrame = new RectangleF (width/2f, 100, width/2f, height/3f);</pre>
<p>Now, we can focus on the image. First, we need to include the image in our project. To do so, you can drag&#8217;n&#8217;drop the image from the finder to Xamarin Studio. So click on the image in the finder and drop it on the project name in the left sidebar of Xamarin Studio. When you drop the file, Xamarin Studio asks you what to do with the file. Select <em>Copy</em> and hit <em>Ok</em>.</p>
<p><a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-21-at-11.14.40.png"><img class="aligncenter size-medium wp-image-269" alt="Screen Shot 2013-08-21 at 11.14.40" src="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-21-at-11.14.40-300x130.png" width="300" height="130" srcset="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-21-at-11.14.40-300x130.png 300w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-21-at-11.14.40.png 538w" sizes="(max-width: 300px) 85vw, 300px" /></a></p>
<p>One last thing to do before being able to access the image is to make it available as a resource. Right-click on the image, go to <em>Build Action</em> and select <em>BundleResource</em>. Now, we can access it easily in our code.</p>
<p>We now want to create an NSImageView that will contain our image and display it in the correct frame. First, let&#8217;s instantiate a new <code>NSImageView</code> with <code>startFrame</code> as parameter. The second step is to tell the image view the image to display. This is achieved by setting the <code>Image</code> property to a new <code>NSImage</code> instance. When an image is known as a BundleResource, you can use <code>NSImage.ImageNamed ("image.jpg")</code> to create a new instance corresponding to the image named as specified in parameter. The last step concerning our image view is to allow the framework to scale the image to the frame dimensions. Set the <code>ImageScaling</code> property to <code>NSImageScale.AxesIndependently</code>.</p>
<pre class="prettyprint">imageView = new NSImageView (startFrame);
imageView.Image = NSImage.ImageNamed ("image.jpg");
imageView.ImageScaling = NSImageScale.AxesIndependently;</pre>
<p>To display our image, we need to add the <code>NSImageView</code> as a subview of our <code>MoveView</code>.</p>
<pre class="prettyprint">AddSubview (imageView);</pre>
<p>Now, we have our constructor implemented. It should look like this:</p>
<pre class="prettyprint">[Export ("initWithFrame:")]
public MoveView (RectangleF frame) : base (frame)
{
	Initialize ();

	var width = frame.Width;
	var height = frame.Height;

	startFrame = new RectangleF (0, 0, width/3f, height/3f);
	endFrame = new RectangleF (width/2f, 100, width/2f, height/3f);

	imageView = new NSImageView (startFrame);
	imageView.Image = NSImage.ImageNamed ("image.jpg");
	imageView.ImageScaling = NSImageScale.AxesIndependently;

	AddSubview (imageView);
}</pre>
<p>If you launch the application, you will have a window with the image at the bottom left.</p>
<p><a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-21-at-14.35.44.png"><img class="aligncenter size-medium wp-image-272" alt="Screen Shot 2013-08-21 at 14.35.44" src="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-21-at-14.35.44-300x204.png" width="300" height="204" srcset="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-21-at-14.35.44-300x204.png 300w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-21-at-14.35.44-624x424.png 624w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-21-at-14.35.44.png 720w" sizes="(max-width: 300px) 85vw, 300px" /></a></p>
<p>Now, we want that the image change when a key is pressed. The first step is to allow our view to respond to keys. The second step is to change the frame of the <code>NSImageView</code> subview. To allow the view to catch key events, you need to make it accept first responder. To enable that, override the property <code>AcceptsFirstResponder</code>.</p>
<pre class="prettyprint">public override bool AcceptsFirstResponder ()
{
	return true;
}</pre>
<p>Next, we can react when a key is pressed. To react, override the method <code>KeyDown</code> to change the <code>Frame</code> property.</p>
<pre class="prettyprint">public override void KeyDown (NSEvent theEvent)
{
	imageView.Frame = endFrame;
}</pre>
<p>If we want the animation to got back and forth we need to introduce a boolean field memorising whether we are at start or not. So add a field, set it to true in the constructor and update <code>KeyDown</code> method to react differently whether we are at start or not.</p>
<pre class="prettyprint">public override void KeyDown (NSEvent theEvent)
{
	if (atStart) {
		imageView.Frame = endFrame;
	} else {
		imageView.Frame = startFrame;
	}
	atStart = !atStart;
}</pre>
<p>Now, we can change the opacity of the image. To change the opacity, you need to update the property <code>AlphaValue</code> of the <code>imageView</code>. For example, the following line set the opacity to 25%.</p>
<pre class="prettyprint">imageView.AlphaValue = .25f;</pre>
<p>Now we have the version without the animation. This amount to what was presented in the first demo video.</p>
<p>The next tutorial will present in details what is an Animator Proxy but this tutorial will simply show you how we can use the default, basic, animator for moving, scaling and fading our image. All we need to do is to update not the imageView itself but the animator of the imageView. For example:</p>
<pre class="prettyprint">public override void KeyDown (NSEvent theEvent)
{
	if (atStart) {
		((NSImageView) imageView.Animator).Frame = endFrame;
		((NSImageView) imageView.Animator).AlphaValue = .25f;
	} else {
		((NSImageView) imageView.Animator).Frame = startFrame;
		((NSImageView) imageView.Animator).AlphaValue = 1;
	}
	atStart = !atStart;
}</pre>
<p>Note that the animator has the same type as our image view so it is really convenient! And, now, we are done. We created our first animation in Cocoa.</p>
<p>To sum up, what did we learned in that tutorial?</p>
<ul>
<li>How to create a project</li>
<li>How to add an image to an NSView</li>
<li>How to change the image location, size and opacity in an NSView</li>
<li>How to use the proxy animator to make smooth transitions</li>
</ul>
<p>See you in the next tutorial <img src="https://s.w.org/images/core/emoji/72x72/1f609.png" alt="&#x1f609;" class="wp-smiley" style="height: 1em; max-height: 1em;" /> If you need, you can download the code <a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/CoreAnimation-Lesson1.zip">CoreAnimation-Lesson1</a>. Don&#8217;t forget to like our <a href="https://www.facebook.com/learning.monomac">facebook page</a>, to follow us on <a href="https://twitter.com/LearningMonoMac">twitter</a> and to let comments or ask questions in the comments!</p>
]]></content:encoded>
			</item>
		<item>
		<title>Cocoa Programming L17: NSToolbar</title>
		<link>http://blog.ac-graphic.net/cocoa-programming-l17-nstoolbar/</link>
		<pubDate>Sat, 24 Aug 2013 10:00:48 +0000</pubDate>
		<dc:creator><![CDATA[Antoine Cailliau]]></dc:creator>
				<category><![CDATA[Cocoa Programming]]></category>

		<guid isPermaLink="false">http://blog.ac-graphic.net/?p=254</guid>
		<description><![CDATA[In this video, we will learn how to use NSToolbar. Many applications use toolbars. The toolbar is composed of the toolbar itself and its item. In this tutorial, we will create a toolbar, add buttons and connect them into our code. As in the previous lesson, we first build the interface in the interface builder. &#8230; <a href="http://blog.ac-graphic.net/cocoa-programming-l17-nstoolbar/" class="more-link">Continue reading<span class="screen-reader-text"> "Cocoa Programming L17: NSToolbar"</span></a>]]></description>
				<content:encoded><![CDATA[<p>In this video, we will learn how to use NSToolbar. Many applications use toolbars. The toolbar is composed of the toolbar itself and its item. In this tutorial, we will create a toolbar, add buttons and connect them into our code.</p>
<p><iframe width="840" height="630" src="https://www.youtube.com/embed/i021PhLS54E?feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>As in the previous lesson, we first build the interface in the interface builder. Then create an outlet for the label and bind the two buttons to the same action. Once you&#8217;re done, you can go back to Xamarin Studio, open the <em>MainWindowController.cs</em> file, and start implementing the partial method <code>toolbarAction</code> we just created in IB.</p>
<p>The <code>sender</code> is bound the element that caused the action to be raised. In this case, the sender is then a <code>NSToolbarItem</code>. This class has a property called <code>tag</code> that correspond to the tag we set in the IB. Using this tag, you can deduce what button was clicked.</p>
<pre class="prettyprint">partial void toolbarAction (NSObject sender)
{
	var item = sender as NSToolbarItem;

	if (item.Tag == 0) 
		this.label.StringValue = "Hello!";

	if (item.Tag == 1) 
		this.label.StringValue = "Computer!";
}</pre>
<p><a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-21-at-09.00.29.png"><img src="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-21-at-09.00.29-300x255.png" alt="Screen Shot 2013-08-21 at 09.00.29" width="300" height="255" class="aligncenter size-medium wp-image-257" srcset="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-21-at-09.00.29-300x255.png 300w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-21-at-09.00.29.png 540w" sizes="(max-width: 300px) 85vw, 300px" /></a></p>
<p>And we are done. As usual, you can download the source code <a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Lesson17.zip">here</a>. Don&#8217;t forget to like our <a href="https://www.facebook.com/learning.monomac">Facebook page</a> and to <a href="https://twitter.com/LearningMonoMac">follow our tweets</a>.</p>
]]></content:encoded>
			</item>
		<item>
		<title>Cocoa Programming L16: Menu Items</title>
		<link>http://blog.ac-graphic.net/cocoa-programming-l16-menu-items/</link>
		<pubDate>Fri, 23 Aug 2013 12:00:19 +0000</pubDate>
		<dc:creator><![CDATA[Antoine Cailliau]]></dc:creator>
				<category><![CDATA[Cocoa Programming]]></category>

		<guid isPermaLink="false">http://blog.ac-graphic.net/?p=248</guid>
		<description><![CDATA[In this video we will learn how to build menus for our applications. To add the label to the main window, proceed as we did before by editing the MainWindow.xib file. To edit the menu, you need to edit the MainMenu.xib file. Now that our example menu is created, we will create the needed outlet &#8230; <a href="http://blog.ac-graphic.net/cocoa-programming-l16-menu-items/" class="more-link">Continue reading<span class="screen-reader-text"> "Cocoa Programming L16: Menu Items"</span></a>]]></description>
				<content:encoded><![CDATA[<p>In this video we will learn how to build menus for our applications.</p>
<p><iframe width="840" height="630" src="https://www.youtube.com/embed/hF-sRs1_2-0?feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>To add the label to the main window, proceed as we did before by editing the <em>MainWindow.xib</em> file. To edit the menu, you need to edit the <em>MainMenu.xib</em> file. Now that our example menu is created, we will create the needed outlet and actions.</p>
<p>We will proceed slightly differently from the video as we do not have an <em>AppController</em>. Create an outlet in the <em>MainWindowController</em> for the label, as we usually did. So, your <em>MainWindowController.h</em> shall look like</p>
<pre class="prettyprint">@interface MainWindowController : NSWindowController {
    NSTextField *label;
}
@property (assign) IBOutlet NSTextField *label;

@end</pre>
<p>For the actions, we will use the <em>AppDelegate.h</em> file. After creating two actions for our menu, you should have a file that looks like this:</p>
<pre class="prettyprint">@interface AppDelegate : NSObject {
}
- (IBAction)sayGoodbye:(id)sender;
- (IBAction)sayHello:(id)sender;
@end</pre>
<p>Now, we can go back to Xamarin Studio and implement the behavior. What will do for the actions is to simply forward the call to the main window controller.</p>
<pre class="prettyprint">partial void sayGoodbye (NSObject sender)
{
	mainWindowController.SayGoodbye ();
}

partial void sayHello (NSObject sender)
{
	mainWindowController.SayHello ();
}</pre>
<p>We now have to extra methods to write in our controller for the main window. These methods will contain the real behavior for the actions.</p>
<pre class="prettyprint">public void SayGoodbye ()
{
	this.label.StringValue = "Goodbye!";
}

public void SayHello ()
{
	this.label.StringValue = "Hello!";
}</pre>
<p>And we are done. As usual, the code is accessible <a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Lesson16.zip">here</a> and don&#8217;t forget to like our <a href="https://www.facebook.com/learning.monomac">facebook page</a> and to follow us on <a href="https://twitter.com/LearningMonoMac">Twitter</a>.</p>
]]></content:encoded>
			</item>
		<item>
		<title>Cocoa Programming L15: NSTableView editing values</title>
		<link>http://blog.ac-graphic.net/cocoa-programming-l15-nstableview-editing-values/</link>
		<pubDate>Thu, 22 Aug 2013 12:00:27 +0000</pubDate>
		<dc:creator><![CDATA[Antoine Cailliau]]></dc:creator>
				<category><![CDATA[Cocoa Programming]]></category>

		<guid isPermaLink="false">http://blog.ac-graphic.net/?p=237</guid>
		<description><![CDATA[Based on the two previous lessons, we will now see how we can edit and remove the values contained in the table. The first step is to add a new action to our MainWindowController.h for the button Remove. So open Xcode and drag&#8217;n&#8217;drop the button to create such action. Your file should now look like &#8230; <a href="http://blog.ac-graphic.net/cocoa-programming-l15-nstableview-editing-values/" class="more-link">Continue reading<span class="screen-reader-text"> "Cocoa Programming L15: NSTableView editing values"</span></a>]]></description>
				<content:encoded><![CDATA[<p>Based on the two previous lessons, we will now see how we can edit and remove the values contained in the table. </p>
<p><iframe width="840" height="630" src="https://www.youtube.com/embed/8Pm1CfmQxfE?feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>The first step is to add a new action to our MainWindowController.h for the button Remove. So open Xcode and drag&#8217;n&#8217;drop the button to create such action. Your file should now look like this</p>
<pre class="prettyprint">@interface MainWindowController : NSWindowController {
	NSTableView *_tableView;
}

@property (nonatomic, retain) IBOutlet NSTableView *tableView;

- (IBAction)add:(id)sender;
- (IBAction)remove:(id)sender;

@end</pre>
<p>Now we can specify the behavior. So go back to Xamarin Studio and edit the file <em>MainWindowController.cs</em> to implement the partial method remove we just created in Interface Builder. Here we just want to remove the selected item. To know what is the selected row, you can use the property <code>SelectedRow</code>. So in our architecture, we will first implement the partial method <code>add</code> in <code>MainWindowController</code>. This method will delegate most of the work to our table controller.</p>
<pre class="prettyprint">partial void remove (NSObject sender)
{
	if (this.tableView.SelectedRow > 0)
		this.tableController.Remove (this.tableView.SelectedRow);
}</pre>
<p>In our table controller, we will remove the element at specified index and tell the table to reload its data.</p>
<pre class="prettyprint">public void Remove (int selectedRow)
{
	list.RemoveAt (selectedRow);
	tableView.ReloadData ();
}</pre>
<p>And now, we can remove data from our table. Our next step is to edit values in the table. To do so, we need to implement a new method in our table controller. Open the class <code>TableController</code> and override the method <code>SetObjectValue</code>. Here, the code is very similar to the one we developed in previous lesson for returning the value contained at a specific row and column position.</p>
<pre class="prettyprint">public override void SetObjectValue (NSTableView tableView, 
                                     NSObject theObject, 
                                     NSTableColumn tableColumn, 
                                     int row)
{
	if (tableColumn.Identifier == "name")
		list[row].Name = ((NSString) theObject);

	else if (tableColumn.Identifier == "age")
		list[row].Age = int.Parse (((NSString) theObject));

	else
		throw new NotImplementedException (string.Format ("{0} is not recognized", 
		                                                  tableColumn.Identifier));
}</pre>
<p>To fix the last problem depicted in the video, just add the following line to the <code>remove</code> method in the <code>MainWindowController</code> class.</p>
<pre class="prettyprint">this.tableView.AbortEditing ();</pre>
<p>Feel free to download the code <a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Lesson15.zip">Lesson15</a>, like our <a href="https://www.facebook.com/learning.monomac">facebook page</a> and follow us on <a href="https://twitter.com/LearningMonoMac">Twitter</a>! Also, don&#8217;t hesitate to make comments and ask questions <img src="https://s.w.org/images/core/emoji/72x72/1f609.png" alt="&#x1f609;" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
]]></content:encoded>
			</item>
		<item>
		<title>Cocoa Programming L14: NSTableView adding objects</title>
		<link>http://blog.ac-graphic.net/cocoa-programming-l14-nstableview-adding-objects/</link>
		<comments>http://blog.ac-graphic.net/cocoa-programming-l14-nstableview-adding-objects/#comments</comments>
		<pubDate>Wed, 21 Aug 2013 12:00:26 +0000</pubDate>
		<dc:creator><![CDATA[Antoine Cailliau]]></dc:creator>
				<category><![CDATA[Cocoa Programming]]></category>

		<guid isPermaLink="false">http://blog.ac-graphic.net/?p=227</guid>
		<description><![CDATA[This post continues the work we started in the previous lesson. We will connect elements together and program the add action. Before continuing the video, I&#8217;ll use a different architecture for this project. We have our MainWindowController that will contain an instance of TableController. TableController will contains all the actions related to the table (no &#8230; <a href="http://blog.ac-graphic.net/cocoa-programming-l14-nstableview-adding-objects/" class="more-link">Continue reading<span class="screen-reader-text"> "Cocoa Programming L14: NSTableView adding objects"</span></a>]]></description>
				<content:encoded><![CDATA[<p>This post continues the work we started in the <a title="Cocoa Programming L13: NSTableView Intro" href="http://blog.ac-graphic.net/cocoa-programming-l13-nstableview-intro/">previous lesson</a>. We will connect elements together and program the add action.</p>
<p><iframe width="840" height="630" src="https://www.youtube.com/embed/p5U94-uRCOo?feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>Before continuing the video, I&#8217;ll use a different architecture for this project. We have our <code>MainWindowController</code> that will contain an instance of <code>TableController</code>. <code>TableController</code> will contains all the actions related to the table (no surprise) and the <code>MainWindowController</code> will just delegate the generated actions to the corresponding action in <code>TableController</code>. This helps in keeping a clean separation of concerns between classes, and also works better with the Xamarin Studio/Xcode integration. So first, create an outlet for the table view in <code>MainWindowController</code>. Your <em>MainWindowController.h</em> file shall look like this:</p>
<pre class="prettyprint">@interface MainWindowController : NSWindowController {
    NSTableView *tableView;
}
@property (assign) IBOutlet NSTableView *tableView;

@end</pre>
<p>Back to Xamarin Studio, we add a new private variable holding the table controller and initialise it with our corresponding instance.</p>
<pre class="prettyprint">private TableController tableController;
public override void AwakeFromNib ()
{
	base.AwakeFromNib ();
	this.tableController = new TableController (this.tableView);
}</pre>
<p>Now that this setup is done, we can continue to follow the video instructions. So, the first step is to add an action on the <em>Add</em> button. So open the <em>MainWindow.xib</em> file and drag&#8217;n&#8217;drop the button, while holding <em>Ctrl</em> key, in the file <em>MainWindowController.h</em>. Select action and give it a name (here <em>add</em>). Your file should look like this</p>
<pre class="prettyprint">@interface MainWindowController : NSWindowController {
    NSTableView *tableView;
}
@property (assign) IBOutlet NSTableView *tableView;
- (IBAction)add:(id)sender;

@end</pre>
<p>Now, go back to Xamarin Studio and open the file <em>MainWindowController.cs</em> to implement the method <code>add</code>. As I explained before, we will invoke the corresponding method on our table view controller.</p>
<pre class="prettyprint">partial void add (NSObject sender)
{
	this.tableController.Add ();
}</pre>
<p>For now, the method is not yet defined. You can right click on <code>Add ()</code>, select <em>Refactor</em> and hit <em>Create method</em>.</p>
<p><a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-20-at-09.10.44.png"><img class="aligncenter size-medium wp-image-228" alt="Screen Shot 2013-08-20 at 09.10.44" src="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-20-at-09.10.44-300x36.png" width="300" height="36" srcset="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-20-at-09.10.44-300x36.png 300w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-20-at-09.10.44.png 436w" sizes="(max-width: 300px) 85vw, 300px" /></a></p>
<p>Then select the best place to implement the method and hit enter.</p>
<p><a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-20-at-09.11.15.png"><img class="aligncenter size-medium wp-image-229" alt="Screen Shot 2013-08-20 at 09.11.15" src="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-20-at-09.11.15-300x94.png" width="300" height="94" srcset="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-20-at-09.11.15-300x94.png 300w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-20-at-09.11.15-624x195.png 624w, http://blog.ac-graphic.net/wp-content/uploads/2013/08/Screen-Shot-2013-08-20-at-09.11.15.png 858w" sizes="(max-width: 300px) 85vw, 300px" /></a></p>
<p>You will then a auto-generated method stub</p>
<pre class="prettyprint">public void Add ()
{
	throw new NotImplementedException ();
}</pre>
<p>There, we will just add a new element to our list.</p>
<pre class="prettyprint">public void Add ()
{
	list.Add (new Person ());
}</pre>
<p>Now, we have a list that will populated every time we hit the <em>Add</em> button. For now, nothing is displayed in our table. We have to implement the interface <code>NSTableViewDataSource</code>. So let&#8217;s edit our table view controller.</p>
<pre class="prettyprint">public class TableController : NSTableViewDataSource
{
  ...
}</pre>
<p>Note that <code>NSTableViewDataSource</code> is a class that already extend <code>NSObject</code>, you then need to get rid of the previous <code>NSObject</code> reference. The two methods we will override are <code>GetRowCount</code> and <code>GetObjectValue</code>.</p>
<p>The first method returns the number of elements contained in our list. This is a reasonable implementation:</p>
<pre class="prettyprint">public override int GetRowCount (NSTableView tableView)
{
	return list.Count;
}</pre>
<p>The second method asks for a specific element. For instance, the age in the second row. First, let&#8217;s setup the identifiers in the interface builder. So go back to Xcode and set the identifiers as explained in the video: select the column, go to the Identity inspector and specify the identifier in the corresponding field. Once the identifiers were specified, go back to Xamarin Studio and continue editing our table controller.</p>
<p>For our method <code>GetObjectValue</code>, we will get the element corresponding to the row and return the property value corresponding to the column. Note that such pattern is common in Apple development and it is known as <em>key-value coding</em>. We need to return instances of <code>NSObject</code>, I decided here to return basic <code>NSString</code>.</p>
<pre class="prettyprint">public override NSObject GetObjectValue (NSTableView tableView, 
                                         NSTableColumn tableColumn, 
                                         int row)
{
  if (tableColumn.Identifier == "name")
    return new NSString (list[row].Name);

  if (tableColumn.Identifier == "age")
    return new NSString (list[row].Age.ToString ());

  throw new NotImplementedException (string.Format ("{0} is not recognized", 
                                     tableColumn.Identifier));
}</pre>
<p>We are mostly done. If you followed correctly, our two new methods are not bound nor called by anyone. This is the last step for that post. We need to proceed differently that the way presented in the video. In the video, you can setup the datasource by connecting elements in Interface builder. In C#, we need to do it programatically. According our architecture, it is best to bound the table controller and the table view in the <code>TableController</code> constructor. So edit the file <em>TableController.cs</em> and update the constructor to set the <code>DataSource</code> property of our <code>tableView</code>.</p>
<pre class="prettyprint">public TableController (NSTableView tableView)
{
	this.tableView = tableView;
	list = new List ();

	this.tableView.DataSource = this;
}</pre>
<p>Now, if you run the application you will not see anything displayed in our table. Even if you hit the <em>Add</em> button several times. In fact, when a new data is added to the table, the view is not notified for this change and we need to do it manually. To do so, go to the <code>Add</code> method the <code>TableController</code> class and add the following line :</p>
<pre class="prettyprint">tableView.ReloadData ();</pre>
<p>Now you&#8217;re done <img src="https://s.w.org/images/core/emoji/72x72/1f642.png" alt="&#x1f642;" class="wp-smiley" style="height: 1em; max-height: 1em;" /> As usual, you can download the code <a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Lesson14.zip">Lesson14</a>. And don&#8217;t forget to like our <a href="https://www.facebook.com/learning.monomac">Facebook page</a> and to follow us on <a href="https://twitter.com/LearningMonoMac">Twitter</a> so that you don&#8217;t miss an update.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ac-graphic.net/cocoa-programming-l14-nstableview-adding-objects/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cocoa Programming L13: NSTableView Intro</title>
		<link>http://blog.ac-graphic.net/cocoa-programming-l13-nstableview-intro/</link>
		<comments>http://blog.ac-graphic.net/cocoa-programming-l13-nstableview-intro/#comments</comments>
		<pubDate>Tue, 20 Aug 2013 12:00:23 +0000</pubDate>
		<dc:creator><![CDATA[Antoine Cailliau]]></dc:creator>
				<category><![CDATA[Cocoa Programming]]></category>

		<guid isPermaLink="false">http://blog.ac-graphic.net/?p=225</guid>
		<description><![CDATA[In this video, we will learn how to use NSTableView. One of the best known example of NSTableView is the song list in iTunes. So we create a new project, and build the interface. As recall, to build the interface, double-click on the file MainWindow.xib. Create a new class, named Person and contains two properties: &#8230; <a href="http://blog.ac-graphic.net/cocoa-programming-l13-nstableview-intro/" class="more-link">Continue reading<span class="screen-reader-text"> "Cocoa Programming L13: NSTableView Intro"</span></a>]]></description>
				<content:encoded><![CDATA[<p>In this video, we will learn how to use <code>NSTableView</code>. One of the best known example of <code>NSTableView</code> is the song list in iTunes.</p>
<p><iframe width="840" height="630" src="https://www.youtube.com/embed/_Qbjwx0hB6A?feature=oembed" frameborder="0" allowfullscreen></iframe></p>
<p>So we create a new project, and build the interface. As recall, to build the interface, double-click on the file <em>MainWindow.xib</em>.</p>
<p>Create a new class, named <em>Person</em> and contains two properties: a <code>string</code> <em>Name</em> and an <code>int</code> <em>Age</em>. You end up with a similar code</p>
<pre class="prettyprint">public class Person
{
	public string Name {
		get;
		set;
	}

	public int Age {
		get;
		set;
	}

	public Person ()
	{
		Name = "Yoda";
		Age = 300;
	}
}</pre>
<p>We now create the controller for the table. So we create a new class, named <em>TableController</em>. This class also extends <code>NSObject</code> and contains two private variables: one for referencing the table view and the other for maintaining the list of datas.</p>
<pre class="prettyprint">public class TableController : NSObject
{
	private NSTableView tableView;
	private IList<Person> list;

	public TableController (NSTableView tableView)
	{
		this.tableView = tableView;
		list = new List<Person> ();
	}
}</pre>
<p>You can download the code <a href="http://blog.ac-graphic.net/wp-content/uploads/2013/08/Lesson13.zip">Lesson13</a>. And don&#8217;t forget to like our <a href="https://www.facebook.com/learning.monomac">Facebook page</a> and to follow us on <a href="https://twitter.com/LearningMonoMac">Twitter</a> so that you don&#8217;t miss an update.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.ac-graphic.net/cocoa-programming-l13-nstableview-intro/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
