<?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>Luminis Software Development &#187; Interface Builder</title>
	<atom:link href="http://lsd.luminis.eu/en/tag/interface-builder/feed/" rel="self" type="application/rss+xml" />
	<link>http://lsd.luminis.eu</link>
	<description></description>
	<lastBuildDate>Wed, 28 Dec 2011 20:44:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Building iPhone applications using MonoTouch, howto: using a view without a controller</title>
		<link>http://lsd.luminis.eu/en/building-iphone-applications-using-monotouch-howto-using-a-view-without-a-controller/</link>
		<comments>http://lsd.luminis.eu/en/building-iphone-applications-using-monotouch-howto-using-a-view-without-a-controller/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 20:30:13 +0000</pubDate>
		<dc:creator>Richard de Zwart</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[mobility]]></category>
		<category><![CDATA[technical]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[MonoDevelop]]></category>
		<category><![CDATA[monotouch]]></category>
		<category><![CDATA[NIB]]></category>

		<guid isPermaLink="false">http://lsd.luminis.nl/?p=615</guid>
		<description><![CDATA[Of course, a view without a controller is not very useful in itself, but in this post I want to show you how I solved a small re-usability problem.
I&#8217;m working on a small open-source project on Codeplex that aims to build a player for the Hanselminutes podcasts.  It is nothing very special, but it [...]]]></description>
			<content:encoded><![CDATA[<p>Of course, a view without a controller is not very useful in itself, but in this post I want to show you how I solved a small re-usability problem.</p>
<p>I&#8217;m working on a small open-source project on <a href="http://hanselminutesiphone.codeplex.com" target="_blank">Codeplex</a> that aims to build a player for the <a href="http://www.hanselminutes.com/"  target="_blank">Hanselminutes podcasts</a>.  It is nothing very special, but it gives me a nice playground to find out new stuff.</p>
<p>So there is a &#8220;Loading playlist&#8221; message and a &#8220;Buffering audio&#8221; message displayed at the appropriate moments. You can do that in a lot of ways, but in this application, a semi-transparent view was chosen that is overlaying the current view. It has a text (of course) and a UIActivityIndicatorView (who comes up with these names????).</p>
<p>I try to follow three rules when building the UI:</p>
<ol>
<li>All the UI is done with the Interface Builder. That allows me to leave the actual design to someone who knows designing.</li>
<li>Every view is in a separate NIB. That way not all the UI has to be loaded at startup, which improves user-perceived performance</li>
<li>Loosely couple the view, to make reuse easier. That means a simple interface (as in &#8216;programming interface&#8217;) to the rest of the world, and all the view-related code inside the NIB</li>
</ol>
<p>In this case I want a view that can be called from different controllers (re-usability), so when I add a file to my MonoDevelop project I choose &#8220;View Interface Definition&#8221;:</p>
<p><a href="http://lsd.luminis.nl/wp-content/uploads/2010/01/Screen-shot-2010-01-25-at-8.34.03-PM.png"><img class="alignleft size-medium wp-image-616" style="margin-left: 10px; margin-right: 10px;" title="Screen shot 2010-01-25 at 8.34.03 PM" src="http://lsd.luminis.nl/wp-content/uploads/2010/01/Screen-shot-2010-01-25-at-8.34.03-PM-300x196.png" alt="Screen shot 2010-01-25 at 8.34.03 PM" width="300" height="196" /></a></p>
<p>It&#8217;s no so different form what you do (at least, what I do) most of the time when you choose View Interface Definition with Controller. You add your UI-elements, define some outlets, but then  you want to activate this view from some controller, any controller.</p>
<p>What I came up with, is the following.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> UIHelper
<span style="color: #000000;">&#123;</span>
  UIViewController _controller<span style="color: #008000;">;</span>
  BusyView _busyView<span style="color: #008000;">;</span>
  <span style="color: #0600FF;">public</span> UIHelper <span style="color: #000000;">&#40;</span>UIViewController controller<span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    _controller <span style="color: #008000;">=</span> controller<span style="color: #008000;">;</span>
    NSArray views <span style="color: #008000;">=</span> NSBundle.<span style="color: #0000FF;">MainBundle</span>.<span style="color: #0000FF;">LoadNib</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;BusyView&quot;</span>, _controller, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    _busyView <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> BusyView<span style="color: #000000;">&#40;</span>views.<span style="color: #0000FF;">ValueAt</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    _controller.<span style="color: #0000FF;">View</span>.<span style="color: #0000FF;">AddSubview</span><span style="color: #000000;">&#40;</span>views<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    _controller.<span style="color: #0000FF;">View</span>.<span style="color: #0000FF;">SendSubviewToBack</span><span style="color: #000000;">&#40;</span>_busyView<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>It is a helper class that loads the view at construction time. It gets passed in the controller that will display the view. Then I load the NIB where the view is in. This gives me back an array. Of what? Well as the <a href="http://developer.apple.com/IPhone/library/documentation/UIKit/Reference/NSBundle_UIKitAdditions/Introduction/Introduction.html" target="_blank">documentation</a> says:</p>
<blockquote><p>An array containing the top-level objects in the nib file. The array does not contain references to the File’s Owner or any proxy objects; it contains only those objects that were instantiated when the nib file was unarchived. You should retain either the returned array or the objects it contains manually to prevent the nib file objects from being released prematurely.</p></blockquote>
<p>So you get the top-elements (actually: the IntPtr&#8217;s of them) from the NIB. In a file with only one view the first one is probably (&#8230;fingers crossed) the right one.<br />
I add the view to the SubViews of the controller and make sure it does not show before it was meant to.</p>
<h3>Showing the view</h3>
<p>Whenever I need the view to display I call:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">	_busyView.<span style="color: #0000FF;">Show</span><span style="color: #000000;">&#40;</span>message<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	_controller.<span style="color: #0000FF;">View</span>.<span style="color: #0000FF;">BringSubviewToFront</span><span style="color: #000000;">&#40;</span>_busyView<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The first line calls a method on the partial class in the NIB to set the text of a label. The second line makes the view visible.</p>
<h3>What&#8217;s not to like about this solution</h3>
<p>Well, the magical string with the name of the NIB, &#8220;BusyView&#8221;. And the magical number 0 the denotes the right object in the list of objects in the NIB.</p>
<p>Any ideas for improvement? Please react!</p>
<p><a href='http://lsd.luminis.nl/wp-content/uploads/2010/01/NibDemo.zip'>Download the code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://lsd.luminis.eu/en/building-iphone-applications-using-monotouch-howto-using-a-view-without-a-controller/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Building iPhone applications using MonoTouch, part 5: software design considerations</title>
		<link>http://lsd.luminis.eu/en/building-iphone-applications-using-monotouch-part-5-software-design-considerations/</link>
		<comments>http://lsd.luminis.eu/en/building-iphone-applications-using-monotouch-part-5-software-design-considerations/#comments</comments>
		<pubDate>Sat, 21 Nov 2009 21:26:55 +0000</pubDate>
		<dc:creator>Richard de Zwart</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[mobility]]></category>
		<category><![CDATA[technical]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[MonoDevelop]]></category>
		<category><![CDATA[monotouch]]></category>

		<guid isPermaLink="false">http://lsd.luminis.nl/?p=574</guid>
		<description><![CDATA[In this post I try to determine where some of my code should go and how I can keep the my application loosely coupled and tightly coherent.]]></description>
			<content:encoded><![CDATA[<p>In the previous 4 posts (<a href="http://lsd.luminis.nl/building-iphone-applications-using-monotouch-part-4-the-uisearchdisplaycontroller/">4</a>, <a href="http://lsd.luminis.nl/building-iphone-applications-using-monotouch-part-3-the-interface-builder/">3</a>, <a href="http://lsd.luminis.nl/building-iphone-applications-using-monotouch-2/">2</a>, <a href="http://lsd.luminis.nl/building-iphone-applications-using-monotouch-1/">1</a>) I gave a lot of attention to the overall structure of an iPhone application. In this post I want to talk about  a topic that is more concerned with general software design issues: where do I do what?</p>
<h3>Get SOLID</h3>
<p>There are two things that I always try to keep in mind when making software: loose coupling and tight cohesion. In other words:</p>
<ol>
<li>make sure your classes don&#8217;t know things that they don&#8217;t need to know</li>
<li>make sure your classes are good at one thing and one thing only</li>
</ol>
<p>These guidelines are part of the <a href="http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod">5 SOLID principles</a> of class design by <a href="http://www.objectmentor.com/omTeam/martin_r.html">Uncle Bob Martin</a>:</p>
<ol>
<li>Single Responsibility Principle</li>
<li>Open Closed Principle</li>
<li>Liskov Substitution Principle</li>
<li>Interface Segregation Principle</li>
<li>Dependency Inversion Principle</li>
</ol>
<p>This stuff is really interesting and sort-of scientific, but it is also like making your database comply to the 5-th Normal Form: nobody does that. You&#8217;re happy with a 3rd Normal Form database. So I&#8217;m happy when I see code that complies with at least two of the above principles.</p>
<p>So what I do when building my iPhone apps is constantly asking myself: should this code be in this place? Sometimes I know right away that the answer is No, but still leave it there until I have a better idea on where to put it then. And with the (for me) rather unusual structure that the Cocao Framework forces upon me, it may take some time before I eventually find out where to put it.</p>
<p>Let me give you an example.</p>
<p>When you want to load data into a UITableView you must create a UITableViewDataSource and assign that to the DataSource property of your UITableViewController. Like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">bool</span> FinishedLaunching <span style="color: #000000;">&#40;</span>UIApplication app, NSDictionary options<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	tableView.<span style="color: #0000FF;">DataSource</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LeesPlankjeDataSource<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	window.<span style="color: #0000FF;">AddSubview</span> <span style="color: #000000;">&#40;</span>tableView<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	window.<span style="color: #0000FF;">MakeKeyAndVisible</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The class instantiated at line 3 is something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> LeesPlankjeDataSource <span style="color: #008000;">:</span> UITableViewDataSource
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> woordjes <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;aap&quot;</span>, <span style="color: #666666;">&quot;noot&quot;</span>, <span style="color: #666666;">&quot;mies&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> UITableViewCell GetCell <span style="color: #000000;">&#40;</span>UITableView tableView, MonoTouch.<span style="color: #0000FF;">Foundation</span>.<span style="color: #0000FF;">NSIndexPath</span> indexPath<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		UITableViewCell cell <span style="color: #008000;">=</span> tableView.<span style="color: #0000FF;">DequeueReusableCell</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;plankje&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>cell <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			cell <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> UITableViewCell<span style="color: #000000;">&#40;</span>UITableViewCellStyle.<span style="color: #0600FF;">Default</span>, <span style="color: #666666;">&quot;plankje&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
		cell.<span style="color: #0000FF;">TextLabel</span>.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> woordjes<span style="color: #000000;">&#91;</span>indexPath.<span style="color: #0000FF;">Row</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">return</span> cell<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">int</span> RowsInSection <span style="color: #000000;">&#40;</span>UITableView tableview, <span style="color: #FF0000;">int</span> section<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> woordjes.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>On  line 3 you see the actual &#8220;data store&#8221;, and on line 4 an important override. This method gets called by Cocoa when loading data in your UITableView. So the controller has a data source and the appropriate methods get called by the framework.</p>
<h3>On to a more realistic implementation</h3>
<p>If I want to advance my class a bit, I could imagine that the data is not a fixed array of strings, but gets passed in at construction time. Maybe I read from a file or from a URL.</p>
<p>That changes my class to this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> LeesPlankjeDataSource <span style="color: #008000;">:</span> UITableViewDataSource
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> _dataStorage<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> LeesPlankjeDataSource<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> data<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		_dataStorage <span style="color: #008000;">=</span> data<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> UITableViewCell GetCell <span style="color: #000000;">&#40;</span>UITableView tableView, MonoTouch.<span style="color: #0000FF;">Foundation</span>.<span style="color: #0000FF;">NSIndexPath</span> indexPath<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		UITableViewCell cell <span style="color: #008000;">=</span> tableView.<span style="color: #0000FF;">DequeueReusableCell</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;plankje&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>cell <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			cell <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> UITableViewCell<span style="color: #000000;">&#40;</span>UITableViewCellStyle.<span style="color: #0600FF;">Default</span>, <span style="color: #666666;">&quot;plankje&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
		cell.<span style="color: #0000FF;">TextLabel</span>.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> _dataStorage<span style="color: #000000;">&#91;</span>indexPath.<span style="color: #0000FF;">Row</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">return</span> cell<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">int</span> RowsInSection <span style="color: #000000;">&#40;</span>UITableView tableview, <span style="color: #FF0000;">int</span> section<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> _dataStorage.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And the main.cs gets these lines:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">	<span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> woordjes <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">ReadAllLines</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;woordjes.txt&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	tableView.<span style="color: #0000FF;">DataSource</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LeesPlankjeDataSource<span style="color: #000000;">&#40;</span>woordjes<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Make sure when you have files that you want to be deployed with your app to set the build-action on the file to &#8220;content&#8221;:</p>
<p><a href="http://lsd.luminis.nl/wp-content/uploads/2009/11/BuildAction.png"><img class="alignleft size-medium wp-image-577" style="margin-left: 5px; margin-right: 5px;" title="BuildAction" src="http://lsd.luminis.nl/wp-content/uploads/2009/11/BuildAction-300x255.png" alt="BuildAction" width="300" height="255" /></a>That&#8217;s all neat. The DataSource gets it data from the outside and doesn&#8217;t care if it comes from a file or a network-connection.</p>
<p>So now we want some action when the user taps a row. You have no choice but to implement this in a delegate (a UITableViewDelegate of course) and then override the RowSelected() method. This method is called for you by Cocoa and as parameters you get a UITableView that the tapping happened on and the Row number that was tapped:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> LeesPlankjeViewDelegate <span style="color: #008000;">:</span> UITableViewDelegate
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> RowSelected <span style="color: #000000;">&#40;</span>UITableView tableView, MonoTouch.<span style="color: #0000FF;">Foundation</span>.<span style="color: #0000FF;">NSIndexPath</span> indexPath<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>But what can I do in this method? Let&#8217;s say I want to do a MessageBox-ish thing to show what word was chosen. All I have is an index to the row in my data source. But I don&#8217;t have the data source itself in this class. I need some way to acces my original data store.</p>
<p>I could do somehting like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> RowSelected <span style="color: #000000;">&#40;</span>UITableView tableView, MonoTouch.<span style="color: #0000FF;">Foundation</span>.<span style="color: #0000FF;">NSIndexPath</span> indexPath<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #FF0000;">string</span> message <span style="color: #008000;">=</span> tableView.<span style="color: #0000FF;">DataSource</span>.<span style="color: #0000FF;">GetCell</span><span style="color: #000000;">&#40;</span>tableView,  indexPath<span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">TextLabel</span>.<span style="color: #0000FF;">Text</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var alert <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> UIAlertView <span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;&quot;</span>, message, <span style="color: #0600FF;">null</span>, <span style="color: #666666;">&quot;OK&quot;</span>, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		alert.<span style="color: #0000FF;">Show</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>So I ask the TableView for its data source, then ask the data source to give me the cell that is on the given index, and then ask the cell for the text of the label. It works, but it is butt-ugly. Why? Because now the delegate knows about the data source too. And I think it shouldn&#8217;t, because all the delegate needs to do is handle UI-interactions from the user. It should say &#8220;Hey, someone tapped me on this row, do something with it&#8221; and then leave the actual work to someone else.</p>
<p>I think it would be reasonable to leave the work to the controller. For me, a controller is always the man-in-the-middle, doing the real work brokering between the model and the view. But if I choose that, I have to pass in the controller when constructing the Delegate, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">tableView.<span style="color: #FF0000;">Delegate</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LeesPlankjeViewDelegate<span style="color: #000000;">&#40;</span>tableViewController<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>And since the tableViewController is only known so far in the Interface Builder, I have to make the controller available in my code by creating an outlet. Sigh&#8230;. even more code in my FinishedLaunching() method. I don&#8217;t want that, I want to hook up UI-parts with each other using the Interface Builder, not in code.</p>
<p>So, what to do now? I don&#8217;t know yet. Let me first deal with a problem that I didn&#8217;t tell you about yet. It is in the code of the LeesPlankjeDataSource. I gave my LeesPlankjeDataSource a constuctor that accepts a string array, thereby enabling me to pass in the data that the data source needs to build a UITableView from.</p>
<p>The problem is, that the UISearchDisplayController re-uses my UITableViewController, including the DataSource. When I click search, it first simply overlays my view, but when I start typing in the search box, the ShouldReloadForSearchString () method gets called and that one resets the SearchResultDataSource with a filtered version of my LeesPlankjeDataSource:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">bool</span> ShouldReloadForSearchString <span style="color: #000000;">&#40;</span>UISearchDisplayController controller, <span style="color: #FF0000;">string</span> forSearchString<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;In ShouldReloadForSearchtring&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	controller.<span style="color: #0000FF;">SearchResultsDataSource</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LeesPlankjeDataSource<span style="color: #000000;">&#40;</span>forSearchString<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>And how am I gonna feed this baby with the right data? How is the SearchResultsDataSource going to get a filtered list of the words in my &#8220;woordjes.txt&#8221; file? I chose to filter by using an overloaded constructor, but I could move that code to a normal method. That allows me to use the other constructor, passing in the data as before in the main.cs:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">bool</span> ShouldReloadForSearchString <span style="color: #000000;">&#40;</span>UISearchDisplayController controller, <span style="color: #FF0000;">string</span> forSearchString<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;In ShouldReloadForSearchString&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	var woordjes <span style="color: #008000;">=</span> <span style="color: #008000;">?????????</span>
	var data <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> LeesPlankjeDataSource<span style="color: #000000;">&#40;</span>woordjes<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	controller.<span style="color: #0000FF;">SearchResultsDataSource</span> <span style="color: #008000;">=</span> data.<span style="color: #0000FF;">FilterOn</span><span style="color: #000000;">&#40;</span>forSearchString<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>But where am I gonna get the &#8220;woordjes&#8221; variable from? Should I load the file again, like I did in main.cs? Or maybe my DataSource should know something about the model? In that case I will not pass data from the outside, but let the DataSource find out itself where to get the data. But that is so against the Dependency Inversion Principle (or Inversion of Control)! If classes depend on other classes, these dependencies had best be passed in from the outside, probably on construction time.</p>
<p>Shoot, I love the IoC pattern. Should I let it go, for the sake of simplicity? After all, I argued before that even the MVC-pattern was maybe to much of a burden for the simple applications we build on the iPhone most of the time.</p>
<p>For tonight, I give up. The <a href="http://lsd.luminis.nl/wp-content/uploads/2009/11/SearchProblems2.zip">code you can download</a> contains the solution that goes against IoC, but works none the less.</p>
<p>I would love to hear your ideas about how to build iPhone applications that are tightly coherent and loosely coupled. I know that we can get in some philosophical or religious discussions, but we&#8217;ll see what to do then. I just want to learn from you guys, as much as I want to teach you where I can.</p>
<p>P.S.<br />
Thanks to <a href="http://www.alexyork.net/blog/post/UINavigationController-with-MonoTouch-Building-a-simple-RSS-reader-Part-1.aspx">Alex York&#8217;s excellent post</a> (see his comment below) I was able to improve on my code. I had seen the idea of nesting the DataSource and Delegate into the UITableViewController before, but didn&#8217;t like it then. That dislike mostly came from the fact that you have to inherit from another class. There is no tighter couling between two classes then inheritance, so I believe you should only use it when absolutely necessary. Well, by now I think that it is absolutely necessary to inherit from the classes in the Cocoa framework. It is simply the way you work.</p>
<p>When working on the new solution I also renamed some classes (no more Dutch names, only Dutch words in the data&#8230;) and added a class that implements the model:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> WordsModel
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> List _dataStorage<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> WordsModel <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		_dataStorage <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">ReadAllLines</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;woordjes.txt&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">ToList</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		_dataStorage.<span style="color: #0000FF;">Sort</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> Data <span style="color: #000000;">&#123;</span>
		get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> _dataStorage.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> FilterOn<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> searchText<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		searchText <span style="color: #008000;">=</span> searchText.<span style="color: #0000FF;">ToLower</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		var result <span style="color: #008000;">=</span> _dataStorage.<span style="color: #0000FF;">Where</span><span style="color: #000000;">&#40;</span>t <span style="color: #008000;">=&amp;</span>gt<span style="color: #008000;">;</span> t.<span style="color: #0000FF;">ToLowerInvariant</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">StartsWith</span><span style="color: #000000;">&#40;</span>searchText<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">return</span> result.<span style="color: #0000FF;">ToArray</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>It is the place where the knowledge of words, where they come from and how you filter them, resides.</p>
<p>But Alex&#8217;s solution did not solve the problem that is typical of the use of the UISearchDisplayController: you have two instances of your DataSource: the one for your initial view, that just displays all the data (words in my case), and the one that is called upon by the UISearchDelegate when you start to search and filter.</p>
<p>I solved this by implementing a general WordsDataSource that uses the WordsModel and has a constuctor for normal use and for filtering:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> WordsDataSource <span style="color: #008000;">:</span> UITableViewDataSource
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">private</span> WordsModel model <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WordsModel<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #0600FF;">private</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> _dataStorage<span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> WordsDataSource<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		_dataStorage <span style="color: #008000;">=</span> model.<span style="color: #0000FF;">Data</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> WordsDataSource<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> filter<span style="color: #000000;">&#41;</span> <span style="color: #008000;">:</span> <span style="color: #0600FF;">this</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		_dataStorage <span style="color: #008000;">=</span> model.<span style="color: #0000FF;">FilterOn</span><span style="color: #000000;">&#40;</span>filter<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> GetAt<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> position<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> _dataStorage<span style="color: #000000;">&#91;</span>position<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> UITableViewCell GetCell <span style="color: #000000;">&#40;</span>UITableView tableView, MonoTouch.<span style="color: #0000FF;">Foundation</span>.<span style="color: #0000FF;">NSIndexPath</span> indexPath<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		UITableViewCell cell <span style="color: #008000;">=</span> tableView.<span style="color: #0000FF;">DequeueReusableCell</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;plankje&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>cell <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			cell <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> UITableViewCell<span style="color: #000000;">&#40;</span>UITableViewCellStyle.<span style="color: #0600FF;">Default</span>, <span style="color: #666666;">&quot;plankje&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
		cell.<span style="color: #0000FF;">TextLabel</span>.<span style="color: #0000FF;">Text</span> <span style="color: #008000;">=</span> _dataStorage<span style="color: #000000;">&#91;</span>indexPath.<span style="color: #0000FF;">Row</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">return</span> cell<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">int</span> RowsInSection <span style="color: #000000;">&#40;</span>UITableView tableview, <span style="color: #FF0000;">int</span> section<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">return</span> _dataStorage.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>It inherits (yep I used the I-word) from UITableViewDataSource so can be called upon by the Cocoa framework when rendering the data for the UITableView.</p>
<p>And then I used that class in two places:</p>
<ol>
<li>as an internal property in my WordsTableViewController</li>
<li>as a helper-class in the delegate of the UISearchDisplayController.</li>
</ol>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>MonoTouch.<span style="color: #0000FF;">Foundation</span>.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;WordsTableViewController&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">partial</span> <span style="color: #FF0000;">class</span> WordsTableViewController <span style="color: #008000;">:</span> UITableViewController
<span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Constructor invoked from the NIB loader</span>
    <span style="color: #0600FF;">public</span> WordsTableViewController <span style="color: #000000;">&#40;</span>IntPtr p<span style="color: #000000;">&#41;</span> <span style="color: #008000;">:</span> <span style="color: #0600FF;">base</span> <span style="color: #000000;">&#40;</span>p<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// The data source for our TableView</span>
    <span style="color: #0600FF;">private</span> WordsDataSource TableDataSource
    <span style="color: #000000;">&#123;</span>
	get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TableView</span>.<span style="color: #0000FF;">DataSource</span> <span style="color: #0600FF;">as</span> WordsDataSource<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
	set <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TableView</span>.<span style="color: #0000FF;">DataSource</span> <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// This class receives notifications that happen on the UITableView</span>
    <span style="color: #FF0000;">class</span> TableDelegate <span style="color: #008000;">:</span> UITableViewDelegate
    <span style="color: #000000;">&#123;</span>
	WordsTableViewController parentView<span style="color: #008000;">;</span>
        <span style="color: #0600FF;">public</span> TableDelegate <span style="color: #000000;">&#40;</span>WordsTableViewController tableViewController<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
		parentView <span style="color: #008000;">=</span> tableViewController<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> RowSelected <span style="color: #000000;">&#40;</span>UITableView tableView, NSIndexPath indexPath<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
		<span style="color: #FF0000;">string</span> selectedWord <span style="color: #008000;">=</span> parentView.<span style="color: #0000FF;">TableDataSource</span>.<span style="color: #0000FF;">GetAt</span><span style="color: #000000;">&#40;</span>indexPath.<span style="color: #0000FF;">Row</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		<span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>var alert <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> UIAlertView <span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Selected&quot;</span>, selectedWord, <span style="color: #0600FF;">null</span>, <span style="color: #666666;">&quot;OK&quot;</span>, <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
			alert.<span style="color: #0000FF;">Show</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> ViewDidLoad <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">ViewDidLoad</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        TableView.<span style="color: #FF0000;">Delegate</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TableDelegate <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">TableDataSource</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WordsDataSource <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>MonoTouch.<span style="color: #0000FF;">Foundation</span>.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;WordSearchDelegate&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> WordSearchDelegate <span style="color: #008000;">:</span> UISearchDisplayDelegate
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">bool</span> ShouldReloadForSearchString <span style="color: #000000;">&#40;</span>UISearchDisplayController controller, <span style="color: #FF0000;">string</span> forSearchString<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		UITableViewDataSource data <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> WordsDataSource<span style="color: #000000;">&#40;</span>forSearchString<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
		controller.<span style="color: #0000FF;">SearchResultsDataSource</span> <span style="color: #008000;">=</span> data<span style="color: #008000;">;</span>
		<span style="color: #0600FF;">return</span> true<span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>I&#8217;m pretty happy with what I got by now. You can <a href="http://lsd.luminis.nl/wp-content/uploads/2009/11/SearchProblems3.zip">download the new version</a>, if you like.</p>
]]></content:encoded>
			<wfw:commentRss>http://lsd.luminis.eu/en/building-iphone-applications-using-monotouch-part-5-software-design-considerations/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

