<?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>João Prado Maia's Weblog &#187; Objective-C</title>
	<atom:link href="http://pessoal.org/blog/category/objective-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://pessoal.org/blog</link>
	<description></description>
	<lastBuildDate>Tue, 11 Aug 2009 00:42:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Customizing the background/border colors of a UITableView (grouped style)</title>
		<link>http://pessoal.org/blog/2009/02/25/customizing-the-background-border-colors-of-a-uitableview/</link>
		<comments>http://pessoal.org/blog/2009/02/25/customizing-the-background-border-colors-of-a-uitableview/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 02:51:19 +0000</pubDate>
		<dc:creator>jpm</dc:creator>
				<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[iPhone SDK]]></category>

		<guid isPermaLink="false">http://pessoal.org/blog/?p=149</guid>
		<description><![CDATA[Customizing the background color of a UITableView is somewhat easy, but only if you use the plain style. If you use the grouped style of a table view, it starts to get way more complex. There are no easy ways to quickly set a property for the border color and the background color of the [...]]]></description>
			<content:encoded><![CDATA[<p>Customizing the background color of a UITableView is somewhat easy, but only if you use the plain style. If you use the grouped style of a table view, it starts to get way more complex. There are no easy ways to quickly set a property for the border color and the background color of the whole table view cell. Well, there are, but if you try to change some of these values and try to use the grouped style, it will look completely wrong.</p>
<p>Below you have a proper grouped table view using the standard Apple theme:</p>
<p><a href="http://pessoal.org/blog/wp-content/uploads/2009/02/standard.png"><img src="http://pessoal.org/blog/wp-content/uploads/2009/02/standard-200x300.png" alt="Standard style" title="Standard style" width="200" height="300" class="alignleft size-medium wp-image-191" /></a></p>
<p>Looks pretty good, and it is also very simple to build that in objective-c. Assuming you use Interface Builder for your UI work, just set the table view as a &#8220;grouped&#8221;, and then set the title property of each cell, like this:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>CellIdentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Cell&quot;</span>;
&nbsp;
    UITableViewCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>cell <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectZero reuseIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    cell.accessoryType <span style="color: #002200;">=</span> UITableViewCellAccessoryDisclosureIndicator;
&nbsp;
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>list <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;First&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Second&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Third&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    cell.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>list objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>So now let&#8217;s try something tricky and simply change the backgroundColor property of cell.contentView, as that works fine when dealing with &#8220;plain&#8221; style table views. The code would be just a bit different:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>CellIdentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Cell&quot;</span>;
&nbsp;
    UITableViewCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>cell <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectZero reuseIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    cell.accessoryType <span style="color: #002200;">=</span> UITableViewCellAccessoryDisclosureIndicator;
&nbsp;
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>list <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;First&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Second&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Third&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    cell.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>list objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span>;
&nbsp;
    cell.contentView.backgroundColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor redColor<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>And this is what it looks like:</p>
<p><a href="http://pessoal.org/blog/wp-content/uploads/2009/02/broken.png"><img src="http://pessoal.org/blog/wp-content/uploads/2009/02/broken-200x300.png" alt="Broken results" title="Broken results" width="200" height="300" class="alignleft size-medium wp-image-190" /></a></p>
<p>At first I thought about changing cell.backgroundView, but that doesn&#8217;t work. So you can see that changing the contentView doesn&#8217;t work either, and it actually breaks a bunch of things:</p>
<ul>
<li>No rounded corners on the left side of the table view anymore</li>
<li>The background color on the disclosure image is still set to white</li>
<li>How do we change the border color anyway?</li>
</ul>
<p>So after researching this problem for a while, and asking around for a solution, it seemed like there was no way to customize this stuff without a lot of manual work. Indeed that was the case, but <a href="http://www.michaelakers.net/">Mike Akers</a> (another <a href="http://stackoverflow.com/questions/tagged/iphone">StackOverflow.com</a> member) posted the source code to a solution that he came up with, and that works very well so far. <a href="http://stackoverflow.com/questions/400965/how-to-customize-the-background-border-colors-of-a-grouped-table-view">You can see his source code here</a> for a custom background view with a border.</p>
<p>Here it is a modified version of my code to use his class:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>CellIdentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Cell&quot;</span>;
&nbsp;
    UITableViewCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>cell <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectZero reuseIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    cell.accessoryType <span style="color: #002200;">=</span> UITableViewCellAccessoryDisclosureIndicator;
&nbsp;
    UILabel <span style="color: #002200;">*</span>label <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UILabel alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    label.font <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIFont boldSystemFontOfSize<span style="color: #002200;">:</span>16.0f<span style="color: #002200;">&#93;</span>;
    label.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span>40.0f, 10.0f, 220.0f, 22.0f<span style="color: #002200;">&#41;</span>;
    label.textColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span>;
    label.backgroundColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor clearColor<span style="color: #002200;">&#93;</span>;
    label.opaque <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
&nbsp;
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>list <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;First&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Second&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Third&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    label.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>list objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>cell.contentView addSubview<span style="color: #002200;">:</span>label<span style="color: #002200;">&#93;</span>;
&nbsp;
    CustomCellBackgroundView <span style="color: #002200;">*</span>bgView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CustomCellBackgroundView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectZero<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>indexPath.row <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        bgView.position <span style="color: #002200;">=</span> CustomCellBackgroundViewPositionTop;
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>indexPath.row <span style="color: #002200;">==</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        bgView.position <span style="color: #002200;">=</span> CustomCellBackgroundViewPositionMiddle;
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
        bgView.position <span style="color: #002200;">=</span> CustomCellBackgroundViewPositionBottom;
    <span style="color: #002200;">&#125;</span>
    cell.backgroundView <span style="color: #002200;">=</span> bgView;
    cell.accessoryView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImageView alloc<span style="color: #002200;">&#93;</span> initWithImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;more_arrow.png&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>And the resulting interface:</p>
<p><a href="http://pessoal.org/blog/wp-content/uploads/2009/02/proper.png"><img src="http://pessoal.org/blog/wp-content/uploads/2009/02/proper-200x300.png" alt="Proper background and border colors" title="Proper background and border colors" width="200" height="300" class="alignleft size-medium wp-image-198" /></a></p>
<p>Ugly colors, but you get the idea. The code is also way more complex than before, but at least it&#8217;s possible to customize the background and border colors of a table view cell. This is more or less what we use on <a href="http://impleo.net/kneecap.html">Knee Cap</a>, our iPhone app that handles day to day money loans. This is what our custom table view looks like:</p>
<p><a href="http://pessoal.org/blog/wp-content/uploads/2009/02/primary.jpg"><img src="http://pessoal.org/blog/wp-content/uploads/2009/02/primary-208x300.jpg" alt="Knee Cap" title="Knee Cap" width="208" height="300" class="alignleft size-medium wp-image-170" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://pessoal.org/blog/2009/02/25/customizing-the-background-border-colors-of-a-uitableview/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>iPhone SDK: Setting up a SQLite database before first use</title>
		<link>http://pessoal.org/blog/2009/02/24/iphone-sdk-setting-up-a-sqlite-database-before-first-use/</link>
		<comments>http://pessoal.org/blog/2009/02/24/iphone-sdk-setting-up-a-sqlite-database-before-first-use/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 03:34:53 +0000</pubDate>
		<dc:creator>jpm</dc:creator>
				<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[iPhone SDK]]></category>

		<guid isPermaLink="false">http://pessoal.org/blog/?p=183</guid>
		<description><![CDATA[It&#8217;s quite common to use SQLite databases in iPhone apps to serve as the backend for your product. While there is a way to create the database file dynamically from your objective-c code, it&#8217;s way simpler to create it in your Mac development machine, add it to your Xcode project, and then simply write the [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s quite common to use SQLite databases in iPhone apps to serve as the backend for your product. While there is a way to create the database file dynamically from your objective-c code, it&#8217;s way simpler to create it in your Mac development machine, add it to your Xcode project, and then simply write the code to copy the database file from your app bundle to your app&#8217;s document directory.</p>
<p>I use the following code in my projects to do just that:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>copyDBToFinalPath <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>originalDBPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> pathForResource<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;database_filename_here&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;db&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>path <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>paths <span style="color: #002200;">=</span> NSSearchPathForDirectoriesInDomains<span style="color: #002200;">&#40;</span>NSDocumentDirectory, NSUserDomainMask, <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>appSupportDir <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>paths objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>appBundleName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> infoDictionary<span style="color: #002200;">&#93;</span> objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;CFBundleName&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>dbNameDir <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@/%@&quot;</span>, appSupportDir, appBundleName<span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSFileManager</span> <span style="color: #002200;">*</span>fileManager <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSFileManager</span> defaultManager<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">BOOL</span> isDir <span style="color: #002200;">=</span> <span style="color: #a61390;">NO</span>;
    <span style="color: #a61390;">BOOL</span> dirExists <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>fileManager fileExistsAtPath<span style="color: #002200;">:</span>dbNameDir isDirectory<span style="color: #002200;">:&amp;</span>isDir<span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>dbPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@/database_filename_here.db&quot;</span>, dbNameDir<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>dirExists <span style="color: #002200;">&amp;&amp;</span> isDir<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">BOOL</span> dbExists <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>fileManager fileExistsAtPath<span style="color: #002200;">:</span>dbPath<span style="color: #002200;">&#93;</span>;
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>dbExists<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
            <span style="color: #a61390;">BOOL</span> success <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>fileManager copyItemAtPath<span style="color: #002200;">:</span>originalDBPath toPath<span style="color: #002200;">:</span>dbPath error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span>;
            <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>success<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
                NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;error = %@&quot;</span>, error<span style="color: #002200;">&#41;</span>;
            <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
                path <span style="color: #002200;">=</span> dbPath;
            <span style="color: #002200;">&#125;</span>
        <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
            path <span style="color: #002200;">=</span> dbPath;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>dirExists<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
        <span style="color: #a61390;">BOOL</span> success <span style="color: #002200;">=</span><span style="color: #002200;">&#91;</span>fileManager createDirectoryAtPath<span style="color: #002200;">:</span>dbNameDir attributes<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>success<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;failed to create dir&quot;</span><span style="color: #002200;">&#41;</span>;
        <span style="color: #002200;">&#125;</span>
        success <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>fileManager copyItemAtPath<span style="color: #002200;">:</span>originalDBPath toPath<span style="color: #002200;">:</span>dbPath error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span>;
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>success<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;error = %@&quot;</span>, error<span style="color: #002200;">&#41;</span>;
        <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
            path <span style="color: #002200;">=</span> dbPath;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> path;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>I use that function like so:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>applicationDidFinishLaunching<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>dbPath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self copyDBToFinalPath<span style="color: #002200;">&#93;</span>;
    self.db <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>FMDatabase databaseWithPath<span style="color: #002200;">:</span>dbPath<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span><span style="color: #002200;">&#91;</span>self.db open<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Could not open database.&quot;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #11740a; font-style: italic;">//[self.db setTraceExecution:YES];</span>
    <span style="color: #11740a; font-style: italic;">//[self.db setLogsErrors:YES];</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// ...</span>
    <span style="color: #11740a; font-style: italic;">// rest of my code here</span>
    <span style="color: #11740a; font-style: italic;">// ...</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>To create that original SQLite database file, just use the standard &#8220;sqlite3&#8243; command found in Mac OS X like so:</p>
<pre>
$ sqlite3 database_filename_here.db
SQLite version 3.4.0
Enter ".help" for instructions
sqlite> .read ./schema.sql
</pre>
<p>The &#8220;schema.sql&#8221; file is where I store my table definitions, and standard inserts.</p>
]]></content:encoded>
			<wfw:commentRss>http://pessoal.org/blog/2009/02/24/iphone-sdk-setting-up-a-sqlite-database-before-first-use/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>iPhone SDK: formatting a numeric value with NSNumberFormatter</title>
		<link>http://pessoal.org/blog/2009/02/09/iphone-sdk-formatting-a-numeric-value-with-nsnumberformatter/</link>
		<comments>http://pessoal.org/blog/2009/02/09/iphone-sdk-formatting-a-numeric-value-with-nsnumberformatter/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 04:16:27 +0000</pubDate>
		<dc:creator>jpm</dc:creator>
				<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[iPhone SDK]]></category>

		<guid isPermaLink="false">http://pessoal.org/blog/?p=162</guid>
		<description><![CDATA[While working on an application that downloads a PDF document into a UIWebView, I wrote some code to display a progress bar while the user waits for the file to be downloaded. It turned out to be very simple to do this, but I hit a snag after trying to also display the amount of [...]]]></description>
			<content:encoded><![CDATA[<p>While working on an application that downloads a PDF document into a UIWebView, I wrote some code to display a progress bar while the user waits for the file to be downloaded. It turned out to be very simple to do this, but I hit a snag after trying to also display the amount of bytes that has been retrieved so far. Something like &#8220;0.45 Mb of 4.56 Mb&#8221;.</p>
<p>Problem was that by default NSNumberFormatter displays number with 3 decimal places, and I needed to customize that a bit, so I could get only 2 decimal places, and always a number before the decimal separator. For instance, getting &#8220;0.45 Mb&#8221; instead of just &#8220;.45 Mb&#8221;. The magical solution was the setPositiveFormat method, which allows the developer to specify these parameters.</p>
<p>Here&#8217;s the code that I ended up using, and it works great:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>createProgressionAlertWithMessage<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>message <span style="color: #002200;">&#123;</span>
    progressAlert <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span>message message<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Please wait...&quot;</span> delegate<span style="color: #002200;">:</span>self cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> otherButtonTitles<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">// Create the progress bar and add it to the alert</span>
    progressView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIProgressView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span>30.0f, 80.0f, 225.0f, 90.0f<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>progressAlert addSubview<span style="color: #002200;">:</span>progressView<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>progressView setProgressViewStyle<span style="color: #002200;">:</span>UIProgressViewStyleBar<span style="color: #002200;">&#93;</span>;
&nbsp;
    UILabel <span style="color: #002200;">*</span>label <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UILabel alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span>90.0f, 90.0f, 225.0f, 40.0f<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    label.backgroundColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor clearColor<span style="color: #002200;">&#93;</span>;
    label.textColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span>;
    label.font <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIFont systemFontOfSize<span style="color: #002200;">:</span>12.0f<span style="color: #002200;">&#93;</span>;
    label.text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span>;
    label.tag <span style="color: #002200;">=</span> kDownloadCounterTag;
    <span style="color: #002200;">&#91;</span>progressAlert addSubview<span style="color: #002200;">:</span>label<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>progressAlert show<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>progressAlert release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>That&#8217;s the method that creates the UIAlertView that will hold the progress bar, and also the UIProgressView itself to display the progress of the download. I added an extra label in there to finally display the actual bytes of the file as it is being streamed over.</p>
<p>For the actual download I&#8217;m using NSURLConnection so I can download the file asynchronously, and receive information while the download is progressing.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewWillAppear<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>animated <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>file <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://domain.com/download?id=%@&quot;</span>, self.docID<span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>fileURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span>file<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #400080;">NSURLRequest</span> <span style="color: #002200;">*</span>req <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLRequest</span> requestWithURL<span style="color: #002200;">:</span>fileURL<span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSURLConnection</span> <span style="color: #002200;">*</span>conn <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLConnection</span> connectionWithRequest<span style="color: #002200;">:</span>req delegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>self createProgressionAlertWithMessage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Downloading document&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>connection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURLConnection</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connection didReceiveResponse<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURLResponse</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>response <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>self.fileData setLength<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
    self.totalFileSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithLongLong<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>response expectedContentLength<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>connection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURLConnection</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connection didReceiveData<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>data <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>self.fileData appendData<span style="color: #002200;">:</span>data<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>resourceLength <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithUnsignedInteger<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self.fileData length<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>progress <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>resourceLength floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">/</span> <span style="color: #002200;">&#91;</span>self.totalFileSize floatValue<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    progressView.progress <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>progress floatValue<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">const</span> <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">int</span> bytes <span style="color: #002200;">=</span> <span style="color: #2400d9;">1024</span> <span style="color: #002200;">*</span> <span style="color: #2400d9;">1024</span>;
    UILabel <span style="color: #002200;">*</span>label <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>UILabel <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>progressAlert viewWithTag<span style="color: #002200;">:</span>kDownloadCounterTag<span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSNumberFormatter</span> <span style="color: #002200;">*</span>formatter <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumberFormatter</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>formatter setNumberStyle<span style="color: #002200;">:</span>NSNumberFormatterDecimalStyle<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>formatter setPositiveFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;##0.00&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>partial <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>resourceLength floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">/</span> bytes<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>total <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self.totalFileSize floatValue<span style="color: #002200;">&#93;</span> <span style="color: #002200;">/</span> bytes<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    label.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@ MB of %@ MB&quot;</span>, <span style="color: #002200;">&#91;</span>formatter stringFromNumber<span style="color: #002200;">:</span>partial<span style="color: #002200;">&#93;</span>, <span style="color: #002200;">&#91;</span>formatter stringFromNumber<span style="color: #002200;">:</span>total<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>formatter release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>connection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURLConnection</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connection didFailWithError<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>error <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>progressAlert dismissWithClickedButtonIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span> animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>I&#8217;m hoping this is useful to someone else. Let me know if there&#8217;s a better to do some of the things I&#8217;m doing here.</p>
]]></content:encoded>
			<wfw:commentRss>http://pessoal.org/blog/2009/02/09/iphone-sdk-formatting-a-numeric-value-with-nsnumberformatter/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>iPhone SDK: Customizing back button title</title>
		<link>http://pessoal.org/blog/2009/02/03/iphone-sdk-customizing-back-button-title/</link>
		<comments>http://pessoal.org/blog/2009/02/03/iphone-sdk-customizing-back-button-title/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 02:31:36 +0000</pubDate>
		<dc:creator>jpm</dc:creator>
				<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[iPhone SDK]]></category>

		<guid isPermaLink="false">http://pessoal.org/blog/?p=152</guid>
		<description><![CDATA[When developing navigation controller-based apps, it&#8217;s pretty common to want to customize the title of the back button that is displayed on the navigation bar. Usually the button title is set to the parent view controller&#8217;s title, but you can customize that.
All you need to do is add some code to the viewDidLoad method in [...]]]></description>
			<content:encoded><![CDATA[<p>When developing navigation controller-based apps, it&#8217;s pretty common to want to customize the title of the back button that is displayed on the navigation bar. Usually the button title is set to the parent view controller&#8217;s title, but you can customize that.</p>
<p>All you need to do is add some code to the viewDidLoad method in the parent view controller:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
&nbsp;
    self.title <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Title goes here&quot;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Uncomment the following line to display an Edit button in the navigation bar for this view controller.</span>
    UIBarButtonItem <span style="color: #002200;">*</span>syncButton <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIBarButtonItem alloc<span style="color: #002200;">&#93;</span> initWithBarButtonSystemItem<span style="color: #002200;">:</span>UIBarButtonSystemItemRefresh target<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>refresh<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    self.navigationItem.rightBarButtonItem <span style="color: #002200;">=</span> syncButton;
    <span style="color: #002200;">&#91;</span>syncButton release<span style="color: #002200;">&#93;</span>;
&nbsp;
    UIBarButtonItem <span style="color: #002200;">*</span>backButton <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIBarButtonItem alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Back&quot;</span> style<span style="color: #002200;">:</span>UIBarButtonItemStyleBordered target<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> action<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    self.navigationItem.backBarButtonItem <span style="color: #002200;">=</span> backButton;
    <span style="color: #002200;">&#91;</span>backButton release<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>self refreshData<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Screenshots of the parent and child view controllers are available below:</p>

<a href='http://pessoal.org/blog/2009/02/03/iphone-sdk-customizing-back-button-title/child/' title='child'><img width="200" height="300" src="http://pessoal.org/blog/wp-content/uploads/2009/02/child-200x300.png" class="attachment-medium" alt="" title="child" /></a>
<a href='http://pessoal.org/blog/2009/02/03/iphone-sdk-customizing-back-button-title/parent/' title='parent'><img width="200" height="300" src="http://pessoal.org/blog/wp-content/uploads/2009/02/parent-200x300.png" class="attachment-medium" alt="" title="parent" /></a>

]]></content:encoded>
			<wfw:commentRss>http://pessoal.org/blog/2009/02/03/iphone-sdk-customizing-back-button-title/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>iPhone SDK: Parsing semi-complex JSON objects</title>
		<link>http://pessoal.org/blog/2009/01/15/iphone-sdk-parsing-complex-json-objects/</link>
		<comments>http://pessoal.org/blog/2009/01/15/iphone-sdk-parsing-complex-json-objects/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 04:01:18 +0000</pubDate>
		<dc:creator>jpm</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[iPhone SDK]]></category>

		<guid isPermaLink="false">http://pessoal.org/blog/?p=140</guid>
		<description><![CDATA[Since this seems to be quite common for newcomers to Objective-C / iPhone development, here&#8217;s another example on how to parse a semi-complex JSON object. This was asked as a comment on a previous post, but I think that deserves its own space.
The JSON object looks something like the following:

{&#34;start&#34;:0,
 &#34;stat&#34;:&#34;ok&#34;,
 &#34;locations&#34;:[{&#34;name&#34;:&#34;Pensacola, FL&#34;,
   [...]]]></description>
			<content:encoded><![CDATA[<p>Since this seems to be quite common for newcomers to Objective-C / iPhone development, here&#8217;s another example on how to parse a semi-complex JSON object. This was asked as a comment on a previous post, but I think that deserves its own space.</p>
<p>The JSON object looks something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="json" style="font-family:monospace;">{&quot;start&quot;:0,
 &quot;stat&quot;:&quot;ok&quot;,
 &quot;locations&quot;:[{&quot;name&quot;:&quot;Pensacola, FL&quot;,
               &quot;place_id&quot;:&quot;qQ7Vig2bBZsZCy82&quot;,
               &quot;woeid&quot;:2470377}],
 &quot;count&quot;:3,
 &quot;total&quot;:3,
 &quot;query&quot;:&quot;address=Pensacola&quot;}</pre></div></div>

<p>So you can quickly see that it&#8217;s an object, with a &#8220;locations&#8221; entry that contains an array of objects. This looks like an object that represents the search results for a location from some web service.</p>
<p>I will parse that complex object by creating separate variables to hold specific parts of the object, but there are simpler ways to do this. In this example, I want to get the value for the &#8220;name&#8221; key in the first item of the &#8220;locations&#8221; array.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// jsonString contains the actual JSON output from your web service</span>
SBJSON <span style="color: #002200;">*</span>json <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SBJSON alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
<span style="color: #11740a; font-style: italic;">// object containing full results</span>
<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>results <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>json objectWithString<span style="color: #002200;">:</span>jsonString error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span>;
<span style="color: #11740a; font-style: italic;">// array just for the &quot;location&quot; results</span>
<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>locations <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>results objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;locations&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #11740a; font-style: italic;">// first location in your array</span>
<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>firstLocation <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>locations objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
<span style="color: #11740a; font-style: italic;">// finally, the name key</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>name <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>firstLocation objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;name&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>json release<span style="color: #002200;">&#93;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://pessoal.org/blog/2009/01/15/iphone-sdk-parsing-complex-json-objects/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
