<?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>iPhone Programming Tutorials &#187; beginner</title>
	<atom:link href="/tag/beginner/feed/" rel="self" type="application/rss+xml" />
	<link>http://icodeblog.com</link>
	<description>iPhone Programming Tutorials</description>
	<lastBuildDate>Tue, 19 Nov 2013 19:34:46 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.9.1</generator>
	<item>
		<title>How to Add GPS to Your iOS App &#8211; Part 1</title>
		<link>http://icodeblog.com/2012/06/04/how-to-add-gps-to-your-ios-app-part-1/</link>
		<comments>http://icodeblog.com/2012/06/04/how-to-add-gps-to-your-ios-app-part-1/#comments</comments>
		<pubDate>Mon, 04 Jun 2012 21:33:11 +0000</pubDate>
		<dc:creator><![CDATA[Shawn Grimes]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[CoreLocation]]></category>
		<category><![CDATA[GPS]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iPhone Coding]]></category>
		<category><![CDATA[iphone dev]]></category>
		<category><![CDATA[iphone development]]></category>
		<category><![CDATA[iphone programming]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=3690</guid>
		<description><![CDATA[
In Part 1 of this series, I will introduce you to the very basics of CoreLocation services and getting the location of the device currently being used.
About CoreLocation
The CoreLocation framework provides your app with the ability to get a device&#8217;s current location, provided the user of the device has granted your app access to that information.
Location services are provided in two major ways using this framework:

Standard Location Services- This service provides the most accurate location information using a variety of  ...]]></description>
				<content:encoded><![CDATA[<p><center><img src="http://img.skitch.com/20120604-drip3tee432eccbfhsdkrpwhbr.jpg" alt="CoreLocation Graphic" /></center></p>
<blockquote><p>In Part 1 of this series, I will introduce you to the very basics of CoreLocation services and getting the location of the device currently being used.</p></blockquote>
<h2>About CoreLocation</h2>
<p>The CoreLocation framework provides your app with the ability to get a device&#8217;s current location, provided the user of the device has granted your app access to that information.</p>
<p>Location services are provided in two major ways using this framework:</p>
<ol>
<li><strong>Standard Location Services</strong>- This service provides the most accurate location information using a variety of methods including celluar, wifi, and GPS.
<ul>
<li>Most accurate.</li>
<li>Consumes the most power and can take a longer time to acquire.</li>
<li>Can only be used when your application is in the foreground.</li>
</ul>
</li>
<li><strong>Significant Change Location Services</strong>- This service uses cellular data to determine a general idea of where the device is located.
<ul>
<li>Fastest and uses the least amount of power to determine the location.</li>
<li>Can also be used to notify your app when a significant change in the location has occurred even if your app is not in focus.</li>
</ul>
</li>
</ol>
<h2>Project Files:</h2>
<p>You can download the project files at this github repository: <a href="http://github.com/shawngrimes/Location-and-Map-Sample">http://github.com/shawngrimes/Location-and-Map-Sample</a></p>
<h2>Adding Location Frameworks</h2>
<p>The first step to adding location services to your project is to add the CoreLocation framework.</p>
<div class="thumbnail" align="center"><a href="http://skitch.com/shawnegrimes/86jyp/link-binary-with-libraries"><img src="http://img.skitch.com/20120603-d7w4x5fyueprskt33g5xet7a1n.preview.jpg" alt="Link Binary With Libraries" /></a></div>
<ol>
<li>Select your target and go to the &#8220;Build Phases&#8221; tab.</li>
<li>Click the &#8220;+&#8221; button under &#8220;Link Binary With Libraries&#8221;</li>
<li>Select the CoreLocation framework and click &#8220;Add&#8221;</li>
</ol>
<div class="thumbnail" align="center"><a href="http://skitch.com/shawnegrimes/86je5/add-core-location-framework"><img src="http://img.skitch.com/20120603-gby2hyh3mtsfax86u5ht39491r.preview.jpg" alt="Add Core Location Framework" /></a></div>
<h2>Location Manager</h2>
<p>With location services, you create an instance of CLLocationManager. There is usually one instance of this per app so I feel a good place for this is in the App Delegate and then each view can access the current location from the App Delegate.</p>
<p>To start, open your App Delegate header file:</p>
<ol>
<li>Add <code>#import &lt;CoreLocation/CoreLocation.h&gt;</code> to the top</li>
<li>Specify that your app delegate complies with the Core Location Delegate protocol by changing your <code>@interface</code> line with <code>@interface CFAAppDelegate : UIResponder &lt;UIApplicationDelegate, CLLocationManagerDelegate&gt;</code></li>
<li>Specify a CLLocationManager property by adding <code>@property (strong, nonatomic) CLLocationManager *locationManager</code> right before the <code>@end</code> statement.</li>
</ol>
<p>Your App Delegate header file should now look similar to the following:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &amp;lt;UIKit/UIKit.h&amp;gt;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//Add Location Framework</span>
<span style="color: #6e371a;">#import &amp;lt;CoreLocation/CoreLocation.h&amp;gt;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">//Specify this app delegate complies with the location manager delegate</span>
<span style="color: #a61390;">@interface</span> CFAAppDelegate <span style="color: #002200;">:</span> UIResponder <span style="color: #002200;">&amp;</span>lt;UIApplicationDelegate, CLLocationManagerDelegate<span style="color: #002200;">&amp;</span>gt;
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>strong, nonatomic<span style="color: #002200;">&#41;</span> UIWindow <span style="color: #002200;">*</span>window;
&nbsp;
<span style="color: #11740a; font-style: italic;">//Add a location manager property to this app delegate</span>
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>strong, nonatomic<span style="color: #002200;">&#41;</span> CLLocationManager <span style="color: #002200;">*</span>locationManager;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<h2>Creating The Location Manager Object</h2>
<p>Switch over to the App Delegate implementation file (.m), and we are going to create our location manager object. The first thing we should do, since we created it as a property of our app delegate, is synthesize the property so add <code>@synthesize locationManager=_locationManager;</code> under the line that reads <code>@synthesize window = _window;</code>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;CFAAppDelegate.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> CFAAppDelegate
&nbsp;
<span style="color: #a61390;">@synthesize</span> window <span style="color: #002200;">=</span> _window;
<span style="color: #a61390;">@synthesize</span> locationManager<span style="color: #002200;">=</span>_locationManager;</pre></td></tr></table></div>

<p>Now that our property is synthesized, we can create the object. I usually create it in the method <code>application didFinishLaunchingWithOptions:</code>. A Location Manager object is created similar to any other object but there are three important properties you should set after you have alloc&#8217;d/init&#8217;d.</p>
<ol>
<li><strong>.purpose</strong> &#8211; The purpose property is displayed to the user of your app when they are prompted to allow your app to use their location. It gives you a chance to explain what your app is going to do with their location information.</li>
<li><strong>.desiredAccuracy</strong> &#8211; The desired accuracy property allows you to tell the device how accurate you would like the location information to be. This should be set based on your application&#8217;s needs. Don&#8217;t set this property to <code>kCLLocationAccuracyBest</code> if you only need to know what city they are in. <em>NOTE: This is the &#8220;desired&#8221; accuracy, it is not guaranteed. The device will determine the best available information and provide it to you but you are not, however, guaranteed any level of accuracy.</em></li>
<li><strong>.distanceFilter</strong> &#8211; The distance filter property tells the location manager how far a device needs to move (horizontally vs. an altitude change) before triggering a new location event. It is measured in meters. You can set the property to <code>kCLDistanceFilterNone</code> to be notified of all events (this is also the default value).</li>
</ol>
<p>The completed creation of our location manage looks like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>application<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application didFinishLaunchingWithOptions<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>launchOptions
<span style="color: #002200;">&#123;</span>
    self.window <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIWindow alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">// Override point for customization after application launch.</span>
    self.window.backgroundColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self.window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>self.locationManager<span style="color: #002200;">==</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
        _locationManager<span style="color: #002200;">=</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CLLocationManager alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
        <span style="color: #11740a; font-style: italic;">//I'm using ARC with this project so no need to release</span>
&nbsp;
        _locationManager.delegate<span style="color: #002200;">=</span>self;
        _locationManager.purpose <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;We will try to tell you where you are if you get lost&quot;</span>;
        _locationManager.desiredAccuracy<span style="color: #002200;">=</span>kCLLocationAccuracyBest;
        _locationManager.distanceFilter<span style="color: #002200;">=</span><span style="color: #2400d9;">500</span>;
        self.locationManager<span style="color: #002200;">=</span>_locationManager;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>If you run this now, you should not notice any change in the way your application runs, you also won&#8217;t start receiving location information just yet. That comes next&#8230;</p>
<h2>Starting Location Services</h2>
<p>Now that we have our location manager configured, we want to start getting the location. To start the standard location service, you should first check to make sure that location services are enabled. This is done with a simple call to <code>[CLLocationManager locationServicesEnabled]</code> so after we set up our location manager above and before the <code>return YES;</code>, add the following code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">    <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>CLLocationManager locationServicesEnabled<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>self.locationManager startUpdatingLocation<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<blockquote><p>NOTE: If location services are not enabled and you start updating location services, the user will be prompted to enable location services. This could be annoying to the end user if they have to answer &#8220;No&#8221; every time your app launches.</p></blockquote>
<p>If you run your app now, you will notice that the user is prompted to enable location services (and it will include the message from the <code>.prompt</code> property of our location manager object).</p>
<div class="thumbnail" align="center"><a href="http://skitch.com/shawnegrimes/86idb/enablelocationservicesprompt"><img src="http://img.skitch.com/20120605-ke3m4ipx1cj2p9pprstkcdcrdn.preview.jpg" alt="EnableLocationServicesPrompt" /></a></div>
<h2>Receiving Location Changes</h2>
<p>We&#8217;ve started location services, but now we need to start receiving updates to location changes. This is all done through the <code>CLLocationManagerDelegate</code> protocol. There are a number of available methods to implement for this delegate but the three most important are probably:</p>
<ol>
<li>– locationManager:didUpdateToLocation:fromLocation:</li>
<li>– locationManager:didFailWithError:</li>
<li>– locationManager:didChangeAuthorizationStatus:</li>
</ol>
<h2>– locationManager:didUpdateToLocation:fromLocation:</h2>
<p>Let&#8217;s start with receiving an update in location and how to handle that. In our App Delegate implementation file (.m), add the following method placeholder below the <code>@synthesize …</code> statements and above <code>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions</code> method:</p>

<div class="wp_syntax"><table><tr><td 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>locationManager<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocationManager <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>manager didUpdateToLocation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocation <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newLocation fromLocation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocation <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>oldLocation<span style="color: #002200;">&#123;</span>
&nbsp;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>One of the first things I like to do is check the timestamp of the newLocation detected. When you first start updating location services, the location manager will receive the last known location of the device, this could be hours or days old depending on the last time location services were used. In my apps, I like to make sure that I have a recent location. This following code will only use a location that has been found in the last 15.0 seconds:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">    <span style="color: #400080;">NSDate</span><span style="color: #002200;">*</span> eventDate <span style="color: #002200;">=</span> newLocation.timestamp;
    NSTimeInterval howRecent <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eventDate timeIntervalSinceNow<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">abs</span><span style="color: #002200;">&#40;</span>howRecent<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">15.0</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">//Location timestamp is within the last 15.0 seconds, let's use it!</span>
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The next check that I like to perform is to see how accurate the newLocation is. This can be done by looking at the <code>.horizontalAccuracy</code> property. This property tells you how accurate your location information is. As I stated above, you can request to have very accurate locations but you are not guaranteed it. In the illustration below, the newLocation.coordinate is the location the device thinks it is, but in truth the orange circle of the .horizontalAccuracy says that it could be anywhere in that circle.</p>
<div class="thumbnail" align="center"><a href="https://skitch.com/shawnegrimes/86kp3/horizontalaccuracy"><img src="https://img.skitch.com/20120603-enfpceaw8jgsqw1hieenx48jmj.preview.jpg" alt="horizontalAccuracy" /></a></div>
<p>Your application&#8217;s need will determine how accurate of a location you need. Again, if you just need the city the user is in, you won&#8217;t need it to be as accurate as if you were trying to find something within walking distance. In the code below, I have set my accuracy threshold to 35.0 meters. This is pretty broad but I have found that it works well indoors as well as out. Insert the following code right after the comment <code>//Location timestamp is within the last 15.0 seconds, let's use it!</code></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">        <span style="color: #11740a; font-style: italic;">//Location timestamp is within the last 15.0 seconds, let's use it!</span>
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>newLocation.horizontalAccuracy<span style="color: #002200;">&amp;</span>lt;<span style="color: #2400d9;">35.0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
            <span style="color: #11740a; font-style: italic;">//Location seems pretty accurate, let's use it!</span>
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;latitude %+.6f, longitude %+.6f<span style="color: #2400d9;">\n</span>&quot;</span>,
                  newLocation.coordinate.latitude,
                  newLocation.coordinate.longitude<span style="color: #002200;">&#41;</span>;
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Horizontal Accuracy:%f&quot;</span>, newLocation.horizontalAccuracy<span style="color: #002200;">&#41;</span>;
&nbsp;
            <span style="color: #11740a; font-style: italic;">//Optional: turn off location services once we've gotten a good location</span>
            <span style="color: #002200;">&#91;</span>manager stopUpdatingLocation<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The code above will print the new location to the console and then turn off location services. This is an optional step but if you don&#8217;t need location services anymore, it&#8217;s a smart thing to turn them off to conserve your user&#8217;s battery.</p>
<p>This is what our complete delegate method looks like:</p>

<div class="wp_syntax"><table><tr><td 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>locationManager<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocationManager <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>manager didUpdateToLocation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocation <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>newLocation fromLocation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CLLocation <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>oldLocation<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSDate</span><span style="color: #002200;">*</span> eventDate <span style="color: #002200;">=</span> newLocation.timestamp;
    NSTimeInterval howRecent <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eventDate timeIntervalSinceNow<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">abs</span><span style="color: #002200;">&#40;</span>howRecent<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">15.0</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">//Location timestamp is within the last 15.0 seconds, let's use it!</span>
        <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>newLocation.horizontalAccuracy<span style="color: #002200;">&amp;</span>lt;<span style="color: #2400d9;">35.0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
            <span style="color: #11740a; font-style: italic;">//Location seems pretty accurate, let's use it!</span>
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;latitude %+.6f, longitude %+.6f<span style="color: #2400d9;">\n</span>&quot;</span>,
                  newLocation.coordinate.latitude,
                  newLocation.coordinate.longitude<span style="color: #002200;">&#41;</span>;
            NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Horizontal Accuracy:%f&quot;</span>, newLocation.horizontalAccuracy<span style="color: #002200;">&#41;</span>;
&nbsp;
            <span style="color: #11740a; font-style: italic;">//Optional: turn off location services once we've gotten a good location</span>
            <span style="color: #002200;">&#91;</span>manager stopUpdatingLocation<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Now we can run the project again. If you didn&#8217;t authorize the app to use location services before, go ahead and do that now. We haven&#8217;t done anything with the location at this point except print it out to the console. If you don&#8217;t know how to see the console, make sure Xcode is the active window (NOT the simulator) and press ⌘⇧C</p>
<p>If you are running the project on the simulator and not a device, you may not see any results in the console window. This is because the simulator does not have location services turned on by default. You can simulate a location by selecting the location services simulation in the console window:</p>
<div class="thumbnail" align="center"><a href="http://skitch.com/shawnegrimes/86typ/simulate-location"><img src="http://img.skitch.com/20120604-b359j7crjba8wctd1gje5kk112.preview.jpg" alt="Simulate Location" /></a></div>
<p>You can also simulate a location in the Simulator by using the Debug menu.</p>
<div class="thumbnail" align="center"><a href="https://skitch.com/shawnegrimes/86tn4/simulator-location-services"><img src="https://img.skitch.com/20120604-qxs23ikjy6qufjpwpg72fuewk9.preview.jpg" alt="Simulator Location Services" /></a></div>
<p>Once a location has been selected, you may need to restart the app to get the new location to show in the console window:</p>
<div class="thumbnail" align="center"><a href="https://skitch.com/shawnegrimes/86trw/location-in-console"><img src="https://img.skitch.com/20120604-ddt5unstdmea3i58ddxsgfgih8.preview.jpg" alt="Location In Console" /></a></div>
<p>Because we built the location manager into the App Delegate, you can access the device&#8217;s current location anywhere in your code by including the AppDelegate header file (.h) in your view controller&#8217;s implementation file (.m):</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;CFAAppDelegate.h&quot;</span></pre></td></tr></table></div>

<p>Then whenever you need to access the location, you can use the following bit of code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//Make sure location services are enabled before requesting the location</span>
<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>CLLocationManager locationServicesEnabled<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#123;</span>
&nbsp;
    CFAAppDelegate <span style="color: #002200;">*</span>appDelegate<span style="color: #002200;">=</span><span style="color: #002200;">&#40;</span>CFAAppDelegate <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span>.delegate;
    CLLocation <span style="color: #002200;">*</span>currentLocation<span style="color: #002200;">=</span>appDelegate.locationManager.location;
&nbsp;
    <span style="color: #11740a; font-style: italic;">//Do what you want with the location...</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<blockquote><p><strong>About: Shawn Grimes</strong><br />
Shawn is a mobile app developer for <a href="http://elctech.com/">ELC Technologies</a>. He is a co-author of the book, <a href="http://www.amazon.com/gp/product/1430240059/ref=as_li_ss_tl?ie=UTF8&amp;tag=charmtrave-20&amp;linkCode=as2&amp;camp=1789&amp;creative=390957&amp;creativeASIN=1430240059">iOS 5 Recipes: A Problem-Solution Approach (Recipes Apress)</a>. You can follow him on twitter <a href="https://twitter.com/#!/shawng">@shawng</a>. Shawn also works with high school students to teach them app development skills through the <a href="http://www.appliedclub.org">APPlied Club</a> Program.</p></blockquote>
<p>UIKit/UIKit.hp</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2012/06/04/how-to-add-gps-to-your-ios-app-part-1/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Using XCode 4 Snippets</title>
		<link>http://icodeblog.com/2011/12/06/using-xcode-4-snippets/</link>
		<comments>http://icodeblog.com/2011/12/06/using-xcode-4-snippets/#comments</comments>
		<pubDate>Tue, 06 Dec 2011 23:02:06 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[xcode4]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=3658</guid>
		<description><![CDATA[Recently, I came across <a href="http://nearthespeedoflight.com/article/xcode_4_code_snippets">this post</a> and fell in love with the idea of using XCode 4 snippets.  Up until I read the post, I had heard they existed, but never really tried them.  After reading the post, I began using his snippets as well as started creating my own.  Now, I would say that my work-flow is faster than ever.  
Here is a quick demonstration of the power of snippets
Type:

ttt

It generates:

#pragma mark - UITableView  ...]]></description>
				<content:encoded><![CDATA[<p>Recently, I came across <a href="http://nearthespeedoflight.com/article/xcode_4_code_snippets">this post</a> and fell in love with the idea of using XCode 4 snippets.  Up until I read the post, I had heard they existed, but never really tried them.  After reading the post, I began using his snippets as well as started creating my own.  Now, I would say that my work-flow is faster than ever.  </p>
<p>Here is a quick demonstration of the power of snippets</p>
<p><b>Type:</b></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">ttt</pre></td></tr></table></div>

<p><b>It generates:</b></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#pragma mark - UITableView Datasource</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>numberOfSectionsInTableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">1</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<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 numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>section <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">10</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<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>;
&nbsp;
    <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>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithStyle<span style="color: #002200;">:</span>UITableViewCellStyleDefault reuseIdentifier<span style="color: #002200;">:</span>cellIdentifier<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    cell.textLabel.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;Cell %d&quot;</span>, indexPath.row<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #6e371a;">#pragma mark - UITableView Delegate methods</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>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 didSelectRowAtIndexPath<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>
&nbsp;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, with 3 keystrokes, I now have a fully working table view implementation with sample data.</p>
<p>This article will be another introduction to XCode 4 snippets but focusing on how to set them up as well as share them with your team.</p>
<h4>Adding A Snippet Inside Of XCode</h4>
<p>In my opinion, this is not very obvious.  The snippet library is fairly well hidden.  To bring it up, first ensure that the right side bar is visible in XCode by selecting the right most option of the &#8220;view&#8221; pane in the top right corner of XCode.</p>
<p><a href="/wp-content/uploads/2011/12/Screen-Shot-2011-12-06-at-3.16.01-PM.png"><img src="/wp-content/uploads/2011/12/Screen-Shot-2011-12-06-at-3.16.01-PM.png" alt="" title="Screen Shot 2011-12-06 at 3.16.01 PM" width="95" height="41" class="alignnone size-full wp-image-3661" /></a></p>
<div style="clear:both">&nbsp;</div>
<p>Now that the right bar is available, click on the &#8220;{}&#8221; option of the bottom most pane.  This will bring up the snippet library.  </p>
<p><a href="/wp-content/uploads/2011/12/Screen-Shot-2011-12-06-at-3.17.10-PM.png"><img src="/wp-content/uploads/2011/12/Screen-Shot-2011-12-06-at-3.17.10-PM.png" alt="" title="Screen Shot 2011-12-06 at 3.17.10 PM" width="260" height="273" class="alignnone size-full wp-image-3662" /></a></p>
<div style="clear:both">&nbsp;</div>
<p>Feel free to browse the snippets that XCode ships with, however most of them are not very useful.  So, now this is where I feel Apple engineers were a little <em>too</em> clever.  It took me a little while to figure out how to actually add a new snippet.  You would expect some sort of + button or something.  The way you add a new snippet in this case is to write out a chunk of code and drag it into the Code Snippet Library window.</p>
<p>Once you drag the code into the library, it creates a default snippet called &#8220;My Code Snippet&#8221;.  Which again, is just terribly non-obvious.  Now, double click on &#8220;My Code Snippet&#8221;, and then click the &#8220;edit&#8221; button to modify it.</p>
<p><a href="/wp-content/uploads/2011/12/Screen-Shot-2011-12-06-at-3.23.42-PM.png"><img src="/wp-content/uploads/2011/12/Screen-Shot-2011-12-06-at-3.23.42-PM.png" alt="" title="Screen Shot 2011-12-06 at 3.23.42 PM" width="447" height="298" class="alignnone size-full wp-image-3663" /></a></p>
<div style="clear:both">&nbsp;</div>
<p>Let&#8217;s talk about each of the important fields:</p>
<ol>
<li>Title &#8211; This is just the common name you will use to refer to the snippet.  It will display during autocompletion</li>
<li>Completion Shortcut &#8211; this is the command you will use to invoke the snippet.  For example, I have ttt to automatically create all of the UITableView delegate and datasource methods with sample data.</li>
<li>Completion Scopes &#8211; This is pretty cool.  You can have the same shortcut to represent differnet snippets based on the scope of the document.  For example, you won&#8217;t want to invoke your @property snippets inside of your Class implementation.  So, they get ignored&#8230;</li>
</ol>
<p><strong>Adding dynamic fields</strong></p>
<p>If you notice, many of Apple&#8217;s auto generated classes or snippets (like UIAlertView), have these &#8220;code bubbles&#8221; hinting at the type of data you should put into them.  They are also nice because they allow you to tab between them enabling you to implement the code quicker.  To add your own simply insert this into your code:</p>
<p><strong>&lt;#Text#></strong></p>
<p>Where &#8220;Text&#8221; is whatever you want the code bubble to say.  </p>
<p>That&#8217;s it, when you are finished click done and you should now be able to use your snippets.</p>
<h4>Sharing Snippets</h4>
<p>While XCode doesn&#8217;t have an export button for snippets, they are in a fairly predictable location.  They are located at:</p>
<p><strong>~/Library/Developer/Xcode/UserData/CodeSnippets/</strong></p>
<p>That being said, you could manually share them between developers (zip them up and email) OR you could version control this folder with something like git.  I really like the latter approach.  Any time a developer adds a new snippet to the library, the others just have to pull, restart XCode, and voila the snippet is there.</p>
<h4>Conclusion</h4>
<p>The only issue now is remembering to actually use your snippets in practice.  It takes some getting used to, but I&#8217;m sure you will get it.  Below, you can download a zip file containing many of the snippets that I use.  Simply unzip it into the folder that I mentioned above.  </p>
<p><a href='/wp-content/uploads/2011/12/XCode-4-snippets.zip'>Download My Snippets</a></p>
<p>Happy iCoding!</p>
<p><a href="http://twitter.com/brandontreb">Follow me on Twitter</a></p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2011/12/06/using-xcode-4-snippets/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Making UITableViews look not so plain</title>
		<link>http://icodeblog.com/2011/11/19/making-uitableviews-look-not-so-plain/</link>
		<comments>http://icodeblog.com/2011/11/19/making-uitableviews-look-not-so-plain/#comments</comments>
		<pubDate>Sat, 19 Nov 2011 00:57:33 +0000</pubDate>
		<dc:creator><![CDATA[James Van Metre]]></dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginner]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=3622</guid>
		<description><![CDATA[As most of you probably know, UITableView&#8217;s are incredibly useful and versatile views to be using in your applications.
If you have ever tried to customize a UITableView though, you know that as soon as you start adding lots of UIViews, UILabels, and UImageViews to a cells ContentView, that these tableviews start to scroll slower and slower, and become choppier and choppier.
What we are going to explore today is how to remedy that situation.
To download the entire XCode project, you can  ...]]></description>
				<content:encoded><![CDATA[<p>As most of you probably know, UITableView&#8217;s are incredibly useful and versatile views to be using in your applications.<br />
If you have ever tried to customize a UITableView though, you know that as soon as you start adding lots of UIViews, UILabels, and UImageViews to a cells ContentView, that these tableviews start to scroll slower and slower, and become choppier and choppier.</p>
<p>What we are going to explore today is how to remedy that situation.<br />
To download the entire XCode project, you can find it at:  <a href="http://github.com/elc/ICB_PrettyTableView">http://github.com/elc/ICB_PrettyTableView</a></p>
<p>We are going to build a simple contact viewer, that will display the phones contacts. For each contact, if they have a first name, last name, email and phone number, they will be displayed within one cell, with different colors. The reason this is useful is because it provides the basics for customizing UITableViewCells that can really start to make your application look nice, and still scroll well.<br />
If you don&#8217;t want to have simulated data inside the simulator, check out this post for copying data from your device to the simulator: <a href="/2011/11/09/how-to-import-contacts-into-the-iphone-simulator/">How to import contacts into the iphone simulator</a></p>
<p>In this example, we have a standard UITableViewController. We are going to have a couple class variables defined in the header</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span>
<span style="color: #6e371a;">#import &lt;AddressBook/AddressBook.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> ICBTableViewController <span style="color: #002200;">:</span> UITableViewController
<span style="color: #002200;">&#123;</span>
    ABAddressBookRef _addressBook;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>contacts;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p> ABAddressBookRef _addressBook is defined in our header, so that we don&#8217;t have to release it until we dealloc. And the contacts is so that we can hold on to the data for our tableView.<br />
In the main table view controller file we are going to override the &#8211; (void)viewDidLoad to provide some initial configuration of the tableView, as well as loading or generating our data. (We will generate fake data for devices or the simulator that don&#8217;t have address book data)</p>

<div class="wp_syntax"><table><tr><td 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>;
    self.tableView.backgroundColor <span style="color: #002200;">=</span> UIColor.blackColor;
    self.tableView.separatorStyle <span style="color: #002200;">=</span> UITableViewCellSeparatorStyleNone;
&nbsp;
    ABAddressBookRef addressBook <span style="color: #002200;">=</span> ABAddressBookCreate<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>tempArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>ABAddressBookCopyArrayOfAllPeople<span style="color: #002200;">&#40;</span>addressBook<span style="color: #002200;">&#41;</span>;
    tempArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tempArray sortedArrayUsingComparator<span style="color: #002200;">:^</span>NSComparisonResult<span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span> obj1, <span style="color: #a61390;">id</span> obj2<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>name1 <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>ABRecordCopyCompositeName<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>ABRecordRef<span style="color: #002200;">&#41;</span>obj1<span style="color: #002200;">&#41;</span>;
        <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>name2 <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>ABRecordCopyCompositeName<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>ABRecordRef<span style="color: #002200;">&#41;</span>obj2<span style="color: #002200;">&#41;</span>;
        <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>name1 compare<span style="color: #002200;">:</span>name2<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>tempArray count<span style="color: #002200;">&#93;</span> &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        self.contacts <span style="color: #002200;">=</span> tempArray;
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>tempMutableArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> arrayWithCapacity<span style="color: #002200;">:</span><span style="color: #2400d9;">100</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> i <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; i &lt; <span style="color: #2400d9;">100</span>; <span style="color: #002200;">++</span>i<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            <span style="color: #400080;">NSMutableDictionary</span> <span style="color: #002200;">*</span>dict <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableDictionary</span> dictionary<span style="color: #002200;">&#93;</span>;
            <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>i <span style="color: #002200;">%</span> <span style="color: #2400d9;">9</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">!=</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
                <span style="color: #002200;">&#91;</span>dict setObject<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;FirstName%d&quot;</span>, i<span style="color: #002200;">&#93;</span> forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;firstName&quot;</span><span style="color: #002200;">&#93;</span>;
            <span style="color: #002200;">&#125;</span>
            <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>i <span style="color: #002200;">%</span> <span style="color: #2400d9;">3</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
                <span style="color: #002200;">&#91;</span>dict setObject<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;LastName%d&quot;</span>, i<span style="color: #002200;">&#93;</span> forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;lastName&quot;</span><span style="color: #002200;">&#93;</span>;
            <span style="color: #002200;">&#125;</span>
            <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>i <span style="color: #002200;">%</span> <span style="color: #2400d9;">3</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span> <span style="color: #002200;">&amp;&amp;</span> <span style="color: #002200;">&#40;</span>i <span style="color: #002200;">%</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
                <span style="color: #002200;">&#91;</span>dict setObject<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;emailTest%d@test%d.com&quot;</span>, i, i<span style="color: #002200;">&#93;</span> forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;email&quot;</span><span style="color: #002200;">&#93;</span>;
            <span style="color: #002200;">&#125;</span>
            <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>i <span style="color: #002200;">%</span> <span style="color: #2400d9;">7</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
                <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #a61390;">string</span> <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;%d&quot;</span>, i<span style="color: #002200;">&#93;</span>;
                <span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #a61390;">string</span> length<span style="color: #002200;">&#93;</span> &lt; <span style="color: #2400d9;">10</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
                    <span style="color: #a61390;">string</span> <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #a61390;">string</span> stringByAppendingFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>, <span style="color: #a61390;">string</span><span style="color: #002200;">&#93;</span>;
                <span style="color: #002200;">&#125;</span>
                <span style="color: #002200;">&#91;</span>dict setObject<span style="color: #002200;">:</span><span style="color: #a61390;">string</span> forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;phone&quot;</span><span style="color: #002200;">&#93;</span>;
            <span style="color: #002200;">&#125;</span>
            <span style="color: #002200;">&#91;</span>tempMutableArray addObject<span style="color: #002200;">:</span>dict<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#125;</span>
        self.contacts <span style="color: #002200;">=</span> tempMutableArray;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>As you can see here we have a couple of self.tableView methods we have called to setup the background color, and also the cell separator style.<br />
If you are running this application on a device, or simulator that has contacts, this method will also make a copy of the address book as the data to display. If there is no data in the address book, we create some fake test data just for displaying.<br />
Also don&#8217;t forget to include our &#8211; (void)dealloc method for releasing our _addressBook variable.</p>

<div class="wp_syntax"><table><tr><td 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>dealloc
<span style="color: #002200;">&#123;</span>
    CFRelease<span style="color: #002200;">&#40;</span>_addressBook<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span>_contacts release<span style="color: #002200;">&#93;</span>;
    _contacts <span style="color: #002200;">=</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>We have to supply the tableView with our number of rows</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<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 numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>section
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self.contacts count<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The next portion we have to override is the &#8211; (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath so that we can supply the tableView with our cells.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Customize the appearance of table view cells.</span>
<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>   
&nbsp;
    <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;ICBTableViewCellIdentifier&quot;</span>;
&nbsp;
    ICBTableViewCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>ICBTableViewCell<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</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>ICBTableViewCell alloc<span style="color: #002200;">&#93;</span> initWithStyle<span style="color: #002200;">:</span>UITableViewCellStyleDefault reuseIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
        cell.textLabel.textColor <span style="color: #002200;">=</span> UIColor.whiteColor;
    <span style="color: #002200;">&#125;</span>
    cell.tag <span style="color: #002200;">=</span> indexPath.row;
&nbsp;
    <span style="color: #400080;">NSObject</span> <span style="color: #002200;">*</span>object <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.contacts objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>object isKindOfClass<span style="color: #002200;">:</span><span style="color: #400080;">NSDictionary</span>.class<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>cell setDictionary<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>object<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>cell setRecord<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ABRecordRef<span style="color: #002200;">&#41;</span>object<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>We are checking each object coming out of our array so that we can determine if we need to call the setDictionary, or setRecord method calls.</p>
<p>Now the meat of this tutorial, extending a UITableViewCell.</p>
<p>In our header we are going to define a bunch of strings that we want to display</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span>
<span style="color: #6e371a;">#import &lt;AddressBook/AddressBook.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> ICBTableViewCell <span style="color: #002200;">:</span> UITableViewCell
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>firstName;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>lastName;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>email;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>phone;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>address;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setRecord<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ABRecordRef<span style="color: #002200;">&#41;</span>record;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>setDictionary<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>dict;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>And then our two set methods:</p>

<div class="wp_syntax"><table><tr><td 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>setRecord<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ABRecordRef<span style="color: #002200;">&#41;</span>record
<span style="color: #002200;">&#123;</span>
    self.firstName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>ABRecordCopyValue<span style="color: #002200;">&#40;</span>record, kABPersonFirstNameProperty<span style="color: #002200;">&#41;</span> autorelease<span style="color: #002200;">&#93;</span>;
    self.lastName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>ABRecordCopyValue<span style="color: #002200;">&#40;</span>record, kABPersonLastNameProperty<span style="color: #002200;">&#41;</span> autorelease<span style="color: #002200;">&#93;</span>;
    self.email <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self getFirstEmail<span style="color: #002200;">:</span>record<span style="color: #002200;">&#93;</span>;
    self.phone <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self getFirstPhone<span style="color: #002200;">:</span>record<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self setNeedsDisplay<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>setDictionary<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>dict
<span style="color: #002200;">&#123;</span>
    self.firstName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dict objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;firstName&quot;</span><span style="color: #002200;">&#93;</span>;
    self.lastName <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dict objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;lastName&quot;</span><span style="color: #002200;">&#93;</span>;
    self.email <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dict objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;email&quot;</span><span style="color: #002200;">&#93;</span>;
    self.phone <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dict objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;phone&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>self setNeedsDisplay<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Some tutorials will have you put this next part into a separate UIView subclass, and add that class as the contentView of this UITableViewCell, but I prefer to override the drawRect of the UITableViewCell, and do all my drawing there.</p>
<p>The first thing I am doing is getting the current graphics context so that we can draw to the screen, clipping to the rect that is passed in drawRect:(CGRect)rect, and then depending on whether this cell is even, I am filling the entire rect with an almost black color, or slightly lighter.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>drawRect<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>rect
<span style="color: #002200;">&#123;</span>
    CGContextRef ctx <span style="color: #002200;">=</span> UIGraphicsGetCurrentContext<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    CGContextClipToRect<span style="color: #002200;">&#40;</span>ctx, rect<span style="color: #002200;">&#41;</span>;
    <span style="color: #11740a; font-style: italic;">//If even</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>self.tag <span style="color: #002200;">%</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        CGContextSetFillColorWithColor<span style="color: #002200;">&#40;</span>ctx, <span style="color: #002200;">&#91;</span>UIColor colorWithWhite<span style="color: #002200;">:</span>0.1f alpha<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span>.f<span style="color: #002200;">&#93;</span>.CGColor<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
        CGContextSetFillColorWithColor<span style="color: #002200;">&#40;</span>ctx, <span style="color: #002200;">&#91;</span>UIColor colorWithWhite<span style="color: #002200;">:</span>0.15f alpha<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span>.f<span style="color: #002200;">&#93;</span>.CGColor<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    CGContextFillRect<span style="color: #002200;">&#40;</span>ctx, rect<span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p>The next thing I am going to figure out is whether I want this text to be centered in the cell, and I am determining this based off whether there is an email field or not.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">    <span style="color: #11740a; font-style: italic;">//Vertically center our text, if no email</span>
    <span style="color: #a61390;">BOOL</span> isCentered <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>self.email <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span>;</pre></td></tr></table></div>

<p>And now for the meat of the drawRect method. We calculate the size of firstName, draw it offset from the left by 5, and then draw lastName right after it. We also change the color we are drawing between those two. </p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">    CGRect tempRect;
    CGFloat midY <span style="color: #002200;">=</span> CGRectGetMidY<span style="color: #002200;">&#40;</span>rect<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
    UIFont <span style="color: #002200;">*</span>defaultFont <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIFont systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">16</span><span style="color: #002200;">&#93;</span>;
    CGSize size <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.firstName sizeWithFont<span style="color: #002200;">:</span>defaultFont<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>isCentered <span style="color: #002200;">==</span> <span style="color: #a61390;">NO</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        tempRect <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">5</span>, <span style="color: #2400d9;">0</span>, size.width, size.height<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
        tempRect <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">5</span>, midY <span style="color: #002200;">-</span> size.height<span style="color: #002200;">/</span><span style="color: #2400d9;">2</span>, size.width, size.height<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#91;</span>self.firstName drawInRect<span style="color: #002200;">:</span>tempRect withFont<span style="color: #002200;">:</span>defaultFont<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIColor lightGrayColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
    size <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.lastName sizeWithFont<span style="color: #002200;">:</span>defaultFont<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>isCentered <span style="color: #002200;">==</span> <span style="color: #a61390;">NO</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        tempRect <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span>CGRectGetMaxX<span style="color: #002200;">&#40;</span>tempRect<span style="color: #002200;">&#41;</span><span style="color: #002200;">+</span><span style="color: #2400d9;">5</span>, <span style="color: #2400d9;">0</span>, size.width, size.height<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
        tempRect <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span>CGRectGetMaxX<span style="color: #002200;">&#40;</span>tempRect<span style="color: #002200;">&#41;</span><span style="color: #002200;">+</span><span style="color: #2400d9;">5</span>, midY <span style="color: #002200;">-</span> size.height<span style="color: #002200;">/</span><span style="color: #2400d9;">2</span>, size.width, size.height<span style="color: #002200;">&#41;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#91;</span>self.lastName drawInRect<span style="color: #002200;">:</span>tempRect withFont<span style="color: #002200;">:</span>defaultFont<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Next we find out if phone actually exists, and if so set the color to red, and draw it to the right of lastName. We also have to make sure we aren&#8217;t drawing this outside our boundaries, so we check to see where the end is, and if it is outside, we crop it to 5 pixels from the end.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self.phone <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIColor redColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
        size <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.phone sizeWithFont<span style="color: #002200;">:</span>defaultFont<span style="color: #002200;">&#93;</span>;
        CGFloat end <span style="color: #002200;">=</span> CGRectGetMaxX<span style="color: #002200;">&#40;</span>tempRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">+</span> size.width;
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>end &gt; rect.size.width<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            size.width <span style="color: #002200;">=</span> CGRectGetMaxX<span style="color: #002200;">&#40;</span>rect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">-</span> CGRectGetMaxX<span style="color: #002200;">&#40;</span>tempRect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">-</span> <span style="color: #2400d9;">10</span>; <span style="color: #11740a; font-style: italic;">//-10 so that we get 5 from the end of last name, and 5 from the end of rect</span>
        <span style="color: #002200;">&#125;</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>isCentered <span style="color: #002200;">==</span> <span style="color: #a61390;">NO</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
            tempRect <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span>CGRectGetMaxX<span style="color: #002200;">&#40;</span>rect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">-</span> size.width <span style="color: #002200;">-</span> <span style="color: #2400d9;">5</span>, <span style="color: #2400d9;">0</span>, size.width, size.height<span style="color: #002200;">&#41;</span>;
        <span style="color: #002200;">&#125;</span> <span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
            tempRect <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span>CGRectGetMaxX<span style="color: #002200;">&#40;</span>rect<span style="color: #002200;">&#41;</span> <span style="color: #002200;">-</span> size.width <span style="color: #002200;">-</span> <span style="color: #2400d9;">5</span>, midY <span style="color: #002200;">-</span> size.height<span style="color: #002200;">/</span><span style="color: #2400d9;">2</span>, size.width, size.height<span style="color: #002200;">&#41;</span>;
        <span style="color: #002200;">&#125;</span>
        <span style="color: #002200;">&#91;</span>self.phone drawInRect<span style="color: #002200;">:</span>tempRect withFont<span style="color: #002200;">:</span>defaultFont lineBreakMode<span style="color: #002200;">:</span>UILineBreakModeTailTruncation<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>And finally if our email actually exists draw it on the bottom left.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self.email <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIColor blueColor<span style="color: #002200;">&#93;</span> set<span style="color: #002200;">&#93;</span>;
        size <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.email sizeWithFont<span style="color: #002200;">:</span>defaultFont<span style="color: #002200;">&#93;</span>;
        tempRect <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">5</span>, midY, size.width, size.height<span style="color: #002200;">&#41;</span>;
        <span style="color: #002200;">&#91;</span>self.email drawInRect<span style="color: #002200;">:</span>tempRect withFont<span style="color: #002200;">:</span>defaultFont<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>I hope this helps you in configuring UITableViewCells for your own project, and hopefully will let you start to think about the possibilities.</p>
<p>To download the entire XCode project, you can find it at:  <a href="http://github.com/elc/ICB_PrettyTableView">http://github.com/elc/ICB_PrettyTableView</a></p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2011/11/19/making-uitableviews-look-not-so-plain/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Back To Basics &#8211; An Introduction To View Controllers</title>
		<link>http://icodeblog.com/2011/10/11/back-to-basics-an-introduction-to-view-controllers/</link>
		<comments>http://icodeblog.com/2011/10/11/back-to-basics-an-introduction-to-view-controllers/#comments</comments>
		<pubDate>Tue, 11 Oct 2011 18:57:34 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[uiviewcontroller]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=3462</guid>
		<description><![CDATA[Introduction
In today&#8217;s post, we are going to be discussing view controllers.  Let&#8217;s start by talking about what a view controller is and it&#8217;s role in your iOS projects.
What is a View Controller?
View Controllers are at the core of every application.  They act as sort of the glue between your models and your views.  View controllers are both responsible for fetching/initializing your models as well as loading up your views to display the information within them.
Basic View Controllers
Every  ...]]></description>
				<content:encoded><![CDATA[<h4>Introduction</h4>
<p>In today&#8217;s post, we are going to be discussing <strong>view controllers</strong>.  Let&#8217;s start by talking about what a view controller is and it&#8217;s role in your iOS projects.</p>
<h4>What is a View Controller?</h4>
<p>View Controllers are at the core of every application.  They act as sort of the glue between your models and your views.  View controllers are both responsible for fetching/initializing your models as well as loading up your views to display the information within them.</p>
<h4>Basic View Controllers</h4>
<p>Every view controller that Apple provides extends from the base <strong>UIViewController</strong> subclass.  This gives them a few basic properties that you will find handy. The most important of course is the <strong>view</strong> property.  This is a reference to the view that the controller is managing.</p>
<p>Along with some default properties, you also get some powerful methods that allow each UIViewController subclass to easily interact with all of the common view controllers.</p>
<p>Here are some of the basic view controllers to be discussed in this post:</p>
<ul>
<li>UINavigationController</li>
<li>UITableViewController</li>
<li>UITabBarController</li>
</ul>
<p>I picked these 3 controllers since they are the most common and are automatically created in the main project templates when creating a new XCode project.</p>
<h4>UINavigationController</h4>
<div id="attachment_3464" style="width: 610px" class="wp-caption alignnone"><a href="/2011/10/11/back-to-basics-an-introduction-to-view-controllers/navigation_interface/" rel="attachment wp-att-3464"><img src="/wp-content/uploads/2011/10/navigation_interface-600x270.jpg" alt="" title="navigation_interface" width="600" class="size-large wp-image-3464" /></a><p class="wp-caption-text">Basic Navigation Interface</p></div>
<p>Generally we don&#8217;t talk about UINavigationControllers outside of the context of a <strong>UITableViewController</strong>, however we need to discuss them prior in order to see what&#8217;s going on under the hood.  </p>
<p>UINavigationController are used when you want to have some sort of hierarchal representation of your data (ie drill down).  They work using a stack of UIViewController subclasses. Every time you &#8220;drill down&#8221;, you simply add another view controller to the stack.  Then, the &#8220;back&#8221; logic is simply a matter of popping view controllers off of a stack.</p>
<p>At the bottom of the stack, there is the rootViewController which get set during initialization and can never be popped off.  At the top of the stack, is the current view controller who&#8217;s view is being displayed to the user.</p>
<p>Here is a code sample demonstrating how to push a view controller on to the navigation stack.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Initialize MyViewController object</span>
MyViewController <span style="color: #002200;">*</span>controller <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyViewController alloc<span style="color: #002200;">&#93;</span> initWithNibName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;MyViewController&quot;</span> 
                                                                   bundle<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #11740a; font-style: italic;">// Pushes MyViewController object on the navigation stack</span>
<span style="color: #002200;">&#91;</span>self.navigationController pushViewController<span style="color: #002200;">:</span>controller animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>controller release<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>In this example, we instantiate a new UIViewController subclass called MyViewController and push it on to the view controller stack.  One very important detail to point out is, you must do this from inside of a view controller that is contained inside of a navigation controller, otherwise the &#8220;self.navigationController&#8221; property will be nil.  navigationController is a property of every UIViewController subclass that gets set whenever it has been inserted into a UINavigationController.</p>
<p>Another thing to note is, we release the controller after pushing it on to the stack (prior to iOS5 with ARC). This is because the navigation controller will retain your view controller therefore preventing it from being released from memory.</p>
<p>The following code will pop the view controller off of the view stack with an animation.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span>self.navigationController popViewControllerAnimated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Now we are going to see how UINavigationControllers are used in conjunction with UITableViewControllers.</p>
<h4>UITableViewController</h4>
<div id="attachment_3465" style="width: 169px" class="wp-caption alignnone"><a href="/2011/10/11/back-to-basics-an-introduction-to-view-controllers/screen-shot-2011-10-11-at-11-35-44-am/" rel="attachment wp-att-3465"><img src="/wp-content/uploads/2011/10/Screen-Shot-2011-10-11-at-11.35.44-AM-159x300.png" alt="" title="Screen Shot 2011-10-11 at 11.35.44 AM" width="159" height="300" class="size-medium wp-image-3465" /></a><p class="wp-caption-text">UITableViewController</p></div>
<p>UITableViewControllers are quite possibly the most popular/common view controllers used in iOS.  They are perfect for displaying large amounts of data or any sort of form entry.</p>
<p>By default, a UITableViewController has a UITableView as it&#8217;s main view and serves as both the delegate and data source for this view.  What this means is, it is responsible for populating the table with data, configuring how the data is displayed, and determining what to do when the user interacts with the table (swipes, touches, etc&#8230;).</p>
<p>Let&#8217;s take a look at a simple UITableViewController.  Here is an init method initializing the data array to be displayed in the table view as well as the primary 3 data source methods.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithNibName<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>nibNameOrNil bundle<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSBundle</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>nibBundleOrNil <span style="color: #002200;">&#123;</span>
    self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super initWithNibName<span style="color: #002200;">:</span>nibNameOrNil bundle<span style="color: #002200;">:</span>nibBundleOrNil<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        self.title <span style="color: #002200;">=</span> NSLocalizedString<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sites&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sites&quot;</span><span style="color: #002200;">&#41;</span>;
        <span style="color: #11740a; font-style: italic;">// #1</span>
        _dataArray <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> alloc<span style="color: #002200;">&#93;</span> initWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Reddit&quot;</span>,
                                                        <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;XKCD&quot;</span>,
                                                        <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;The Oatmeal&quot;</span>,
                                                        <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hacker News&quot;</span>,
                                                        <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;iCodeBlog&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>numberOfSectionsInTableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// #2</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">1</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<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 numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>section <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// #3</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>_dataArray count<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<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> initWithStyle<span style="color: #002200;">:</span>UITableViewCellStyleDefault reuseIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
        cell.accessoryType <span style="color: #002200;">=</span> UITableViewCellAccessoryDisclosureIndicator;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// #4</span>
    cell.textLabel.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>_dataArray objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>As mentioned before, we are displaying a simple array of NSString objects in the table view.  This array gets initialized in #1 before the tableview is even displayed.  The next method in #2 returns the number of sections for the table.  In our case, we only need one section so we hard code the number 1 as the return value.</p>
<p>The code in #3 returns the number of cells in a given section.  Generally, this will mean the number of objects in your array.  So, we just return the count property of our dataArray.</p>
<p>Finally, the method in #4 builds the cell for a given row in a section, configures that cell, and returns it.  In our case, the only configuration we need to do is set the text property of the cell&#8217;s UITextLabel to the string in our array at the corresponding index.</p>
<p>These 3 methods are the absolute minimum required to use a UITableViewController and display its table.  There are quite a few more methods that you can implement related to the cell height, titles for sections, and more.  The last thing I want to show you is how to support basic user interaction.</p>
<p>There is only one method you need to implement to support taps on the cells.  Here is the sample code from above inside of didSelectRowAtIndexPath.</p>

<div class="wp_syntax"><table><tr><td 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>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 didSelectRowAtIndexPath<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: #11740a; font-style: italic;">// Initialize MyViewController object</span>
    MyViewController <span style="color: #002200;">*</span>controller <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MyViewController alloc<span style="color: #002200;">&#93;</span> initWithNibName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;MyViewController&quot;</span> 
                                                                       bundle<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">// Forward some data</span>
    controller.site <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>_dataArray objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">// Pushes MyViewController object on the navigation stack</span>
    <span style="color: #002200;">&#91;</span>self.navigationController pushViewController<span style="color: #002200;">:</span>controller animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>controller release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Now when the user taps a row, it will loading up a MyViewController object, pass along the information about the cell tapped and push it on to the view stack.  One thing to note here is, this code assumes that the UITableViewController class presented above is inside of a UINavigationController. </p>
<h4>UITabBarController</h4>
<div id="attachment_3466" style="width: 169px" class="wp-caption alignnone"><a href="/2011/10/11/back-to-basics-an-introduction-to-view-controllers/screen-shot-2011-10-11-at-11-36-03-am/" rel="attachment wp-att-3466"><img src="/wp-content/uploads/2011/10/Screen-Shot-2011-10-11-at-11.36.03-AM-159x300.png" alt="" title="Screen Shot 2011-10-11 at 11.36.03 AM" width="159" height="300" class="size-medium wp-image-3466" /></a><p class="wp-caption-text">UITabBarController</p></div>
<p>The last controller we are going to discuss in this post is the UITabBarController.  As you may have seen in many applications, a tab bar separates out view logic into multiple &#8220;tabs&#8221; and switches between them when the user taps on each one.  Each tab represents a different view controller that could be any of the types above.  As a newer developer, TabBarControllers are a little tricky to implement manually, so I&#8217;d suggest starting your project using &#8220;Apple&#8217;s Tab Bar Application&#8221; template file. This will provide you with a simple tab bar application with two sample tabs.</p>
<p>We are going to walk through the code generated from the iOS 5 template that details how a tab bar is constructed.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>application<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application didFinishLaunchingWithOptions<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>launchOptions <span style="color: #002200;">&#123;</span>
    self.window <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIWindow alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">// #1</span>
    UIViewController <span style="color: #002200;">*</span>viewController1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>FirstViewController alloc<span style="color: #002200;">&#93;</span> initWithNibName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;FirstViewController&quot;</span> bundle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    UIViewController <span style="color: #002200;">*</span>viewController2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>SecondViewController alloc<span style="color: #002200;">&#93;</span> initWithNibName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;SecondViewController&quot;</span> bundle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">// #2</span>
    self.tabBarController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITabBarController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">// #3</span>
    self.tabBarController.viewControllers <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span>viewController1, viewController2, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #11740a; font-style: italic;">// #4</span>
    self.window.rootViewController <span style="color: #002200;">=</span> self.tabBarController;
    <span style="color: #002200;">&#91;</span>self.window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The code here is actually pretty straight forward.  #1 initializes both of the view controllers we want to display in 2 separate tabs.  Don&#8217;t worry about setting their title and tab bar icon now, I will get to that in a moment.</p>
<p>Now, in #2 we initialize the tab bar controller.  Unlike the UITableViewController, we don&#8217;t need to make our own subclass.  It is fine to just let the app delegate manage an instance of the tab bar controller.  </p>
<p>The code in #3 is where we set the view controllers of the tab bar controller.  It should be clear now how easy it is to add more &#8220;tabs&#8221; to you your application. </p>
<p>Finally, #4 adds the tab bar controller to the window displaying it to the user.</p>
<p>Let&#8217;s take a quick look at the code in the initWithNibName method of FirstViewController in order to see how the title and image are set for that controller.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithNibName<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>nibNameOrNil bundle<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSBundle</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>nibBundleOrNil
<span style="color: #002200;">&#123;</span>
    self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super initWithNibName<span style="color: #002200;">:</span>nibNameOrNil bundle<span style="color: #002200;">:</span>nibBundleOrNil<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        self.title <span style="color: #002200;">=</span> NSLocalizedString<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;First&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;First&quot;</span><span style="color: #002200;">&#41;</span>;
        self.tabBarItem.image <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;first&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Pretty simple right?  Every UIViewController subclass has a title and tabBarItem property.  That way, it knows what to display if you place it inside of a UITabBarController.</p>
<h4>Conclusion</h4>
<p>This wraps up our very surface level introduction to UIViewControllers.  I have zipped both of the sample projects used here and you can download them <a href='/2011/10/11/back-to-basics-an-introduction-to-view-controllers/viewcontrollers/' rel='attachment wp-att-3479'>here</a>.</p>
<p>If you have any questions or comments, please feel free to leave them here or write them to me on <a href="http://twitter.com/brandontreb">Twitter</a>.</p>
<p>Happy iCoding!</p>
<p><span style="font-family: 'Lucida Grande';"><strong><span style="font-weight: normal;"><span style="font-family: arial, verdana, tahoma, sans-serif; font-size: 13px; line-height: 20px;"><em>This post is part of an iOS development series called Back To Basics.  You can keep up with this series through the <a style="text-decoration: none; color: #004199; padding: 0px; margin: 0px;" href="/2011/07/12/iphone-development-back-to-basics/">table of contents</a>, <a style="text-decoration: none; color: #004199; padding: 0px; margin: 0px;" href="/tag/b2b/feed/">RSS feed</a>, or <a style="text-decoration: none; color: #004199; padding: 0px; margin: 0px;" href="https://twitter.com/#!/brandontreb">Twitter</a>.</em></span></span></strong></span></p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2011/10/11/back-to-basics-an-introduction-to-view-controllers/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Back To Basics: Hello iPhone</title>
		<link>http://icodeblog.com/2011/08/17/back-to-basics-hello-iphone/</link>
		<comments>http://icodeblog.com/2011/08/17/back-to-basics-hello-iphone/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 21:01:43 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iphone]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=3302</guid>
		<description><![CDATA[Today I&#8217;m going to show you how to make a simple &#8220;Hello World&#8221; project on the iPhone. There are quite a few ways that this can be accomplished, however I am going to show you the way that I feel will be the most beneficial.
While I know many of iCodeBlog&#8217;s readers are veteran developers, this tutorial (and many in this series) are going to be geared towards the more novice developer. This group is often overlooked when it comes to  ...]]></description>
				<content:encoded><![CDATA[<p>Today I&#8217;m going to show you how to make a simple &#8220;Hello World&#8221; project on the iPhone. There are quite a few ways that this can be accomplished, however I am going to show you the way that I feel will be the most beneficial.</p>
<p>While I know many of iCodeBlog&#8217;s readers are veteran developers, this tutorial (and many in this series) are going to be geared towards the more novice developer. This group is often overlooked when it comes to tutorials, and my hope is to help lay a solid foundation for them (in addition to honing the skills of the veteran developers). So, let&#8217;s dig in.</p>
<h4>Introduction</h4>
<p>Generally, when you make a &#8220;Hello World&#8221; project, the goal is to get the text &#8220;Hello World&#8221; to display on the screen or the console. On the iPhone/iPad, there are quite a few ways to display text on the screen (labels, text areas, text views, core text, etc&#8230;). Today we are just going to focus on drawing the text using a <strong>UILabel</strong>.</p>
<p>There are two ways to get a UILabel to display on the screen. The first is to use Apple&#8217;s <strong>Interface Builder</strong> tool. Interface Builder is an extremely handy tool for creating iOS interfaces visually. It supports drag and drop and allows you to configure all of the properties of each of the elements. While it is a great tool, I want to show you a bit what&#8217;s happening under the hood in code. It is important not to lean on Interface Builder as a crutch when creating interfaces as there are times when you will have to add UI elements manually. That being said, I will have an intro to Interface Builder tutorial up later in this series.</p>
<h4>Creating The Project</h4>
<p>Before we begin make sure you have read and understand the following tutorial:</p>
<p><a href="/2011/07/18/back-to-basics-getting-set-up-for-ios-development/">Back To Basics &#8211; Getting Set Up For iOS Development</a></p>
<p>This tutorial will ensure that you have the proper environment set up for iOS development.</p>
<p>When you first launch XCode, you are prompted with a screen similar to this one:</p>
<p><a href="/wp-content/uploads/2011/08/Screen-shot-2011-08-17-at-12.51.31-PM.png"><img class="size-full wp-image-3325 alignnone" title="Screen shot 2011-08-17 at 12.51.31 PM" src="/wp-content/uploads/2011/08/Screen-shot-2011-08-17-at-12.51.31-PM.png" alt="" width="600" height="393" /></a></p>
<p>To create a new project, simply click on <strong>Create a new XCode project</strong>. This will bring up a wizard guiding you through the initial project set up.</p>
<p>Built into XCode are several different project templates to be used as a base for the most common projects that you might want to create. Each one comes with some specific &#8220;boiler plate&#8221; code to quickly get you up and running.</p>
<p><a href="/wp-content/uploads/2011/08/Screen-shot-2011-08-17-at-12.56.43-PM.png"><img class="size-full wp-image-3326 alignnone" title="Screen shot 2011-08-17 at 12.56.43 PM" src="/wp-content/uploads/2011/08/Screen-shot-2011-08-17-at-12.56.43-PM.png" alt="" width="600" height="408" /></a></p>
<p>We are going to start by clicking on <strong>View-based Application</strong>. This will give us enough code to have a simple view on the screen. A UIView is the simplest of display objects. Every other UI element that you draw on the screen will inherit from UIView. In the case of a view-based application, we are given a full screen blank view to start with. We can then add display objects as subviews.</p>
<p>After selecting View-based Application, click next. This screen will show some simple project options such as name, company identifier, and Device family.</p>
<p><a href="/wp-content/uploads/2011/08/Screen-shot-2011-08-17-at-1.01.18-PM.png"><img class="size-full wp-image-3327 alignnone" title="Screen shot 2011-08-17 at 1.01.18 PM" src="/wp-content/uploads/2011/08/Screen-shot-2011-08-17-at-1.01.18-PM.png" alt="" width="600" height="408" /></a></p>
<p>Here&#8217;s a bit about each:</p>
<ul>
<li><strong>Name:</strong> This is simply the name of the application. Don&#8217;t worry you are not bound to this and you can easily change the display name.</li>
<li><strong>Company Identifier</strong> This is unique id used to identify your application. It will be prepended to your project name to form the unique id that the app store uses. Identifiers are usually of the form com.companyName.projectName.</li>
<li><strong>Device Family:</strong> This will be either iPhone or iPad. You can always upgrade to universal later (tutorial to come).</li>
</ul>
<h4>The Files</h4>
<p>Once your project has been created, you will have 6 files of interest (for this tutorial&#8217;s sake):</p>
<p><strong>HelloiPhoneAppDelegate.h/m</strong></p>
<p>The app delegate is the point of entry for your application. It&#8217;s a singleton class (meaning there is only one), and it is responsible for displaying the main window/view of the application. You generally want to keep this class pretty lean and offload the work somewhere else. It&#8217;s often a n00b mistake to want to work on your UI/logic in the app delegate. For this project, we are not going to touch it at all. Just note that it is what&#8217;s launching your main view.</p>
<p><strong>MainWindow.xib</strong></p>
<p>Xib files are to be used with Interface Builders. They are basically xml files that Interface Builder uses to represent a user interface. In this case the MainWindow.xib (pronounced nib), is responsible for loading up our Window and Main ViewController (HelloiPhoneViewController). The View Controller could have been loaded programmatically in the App Delegate, however Apple has chosen to load in using a nib in Interface Builder for this template. I&#8217;m not going to go into detail about that in this tutorial, but you can see for yourself by opening up MainWindow.xib and look under <strong>objects</strong>.</p>
<p><strong>HelloiPhoneViewController</strong></p>
<p>iOS development tends to really enforce <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller">Model-View-Controller</a> design patterns. This is basically the idea of separating the view code from the model code (using the controller code as the glue). Generally, in iOS when you have a view controller, it has a corresponding nib file(the view) to go along with it. HelloiPhoneViewcontroller.xib is where you would drag and drop UI elements to customize the view using the Interface Builder tool. As I mentioned in the beginning, we are going to ignore this file and add our text manually.</p>
<h4>The Code</h4>
<p>Well, this has been quite a bit of talk just to get us to the simple code below. Open up the file HelloiPhoneViewController.m and find the method called <strong>- (void) viewDidLoad </strong>. This method fires automatically when the view is first loaded. Make sure to uncomment it if it&#8217;s commented out. Inside of this method, add the following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td 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;
    CGRect frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">10</span>, <span style="color: #2400d9;">10</span>, <span style="color: #2400d9;">300</span>, <span style="color: #2400d9;">25</span><span style="color: #002200;">&#41;</span>;
    UILabel <span style="color: #002200;">*</span>helloLabel <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>frame<span style="color: #002200;">&#93;</span>;
    helloLabel.text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hello iPhone!&quot;</span>;
    helloLabel.textColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor blueColor<span style="color: #002200;">&#93;</span>;
    helloLabel.textAlignment <span style="color: #002200;">=</span> UITextAlignmentCenter;
    <span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>helloLabel<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>helloLabel release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Let&#8217;s break down this code:</p>
<p><strong>Line 4:</strong> This creates a frame. Think of a frame as a box. The first two parameters are the starting x and y locations of the box, and the second two are the width and height of the box. Every display object you will create has a frame. It tells the device how big and where to draw a view.</p>
<p><strong>Line 5:</strong> On this line we create a new label object. The alloc says &#8220;give me the memory large enough for a UIlabel&#8221; and the init initializes all of the view properties. You will never really see these two methods called apart from each other. We call one of the copy constructors of UILabel called initWithFrame. This allows us to pass in the view&#8217;s frame on initialization.</p>
<p><strong>Lines 6-8:</strong> In these 3 lines, we configure various properties of a UILabel. The first is the actual text to be displayed, the second is the color of the text, and the third tells the text to center inside of it&#8217;s frame. There are quite a few options you can set with regards to any UI element and they can be found in the documentation for that particular object.</p>
<p><strong>Line 9:</strong> This is where we actually get the label to display. All views are subviews of some other view (even your main view is a subview of the window). We are basically attaching our label to the main view to be drawn. Without this line, your label would never be seen.</p>
<p><strong>Line 10:</strong> I don&#8217;t want to go <em>too</em> much into this right now, but The iOS platform (as of iOS versions &lt; 5) doesn&#8217;t have garbage collection. What this means is, we need to handle our memory management ourselves. That being said, as a general rule, whenever you do an &#8220;alloc init&#8221; on an object, you must have a corresponding release call to give that memory back to the system. Otherwise, you have a memory leak. More to come on this.</p>
<h4>Running The Application</h4>
<p>OK, now that we have the code in place it&#8217;s time to run it in the simulator. To do this, select iPhone X.X Simulator in the dropdown at the very top of XCode (currently mine says 4.3). Then click the <strong>Run</strong> button. Your code will be compiled and the simulator will start. You should now see your _beautiful Interface_.</p>
<p><a href="/wp-content/uploads/2011/08/Screen-shot-2011-08-17-at-2.22.21-PM.png"><img class="size-full wp-image-3328" title="Screen shot 2011-08-17 at 2.22.21 PM" src="/wp-content/uploads/2011/08/Screen-shot-2011-08-17-at-2.22.21-PM.png" alt="" width="387" height="735" /></a></p>
<div style="clear:both">&nbsp;</div>
<p>Well, it&#8217;s not much to look at, but try reading the documentation and adding some other interface elements to the view. Or, read the documentation for UILabel and change some of the properties.</p>
<p>If you have any questions or comments, feel free to leave them in the comments section of this post or send them to me on <a href="http://twitter.com/brandontreb">Twitter</a>.</p>
<p>You can download the source code for this tutorial <a href="/wp-content/uploads/2011/08/HelloiPhone.zip">here</a>.</p>
<p>Happy iCoding!</p>
<p><span style="font-family: 'Lucida Grande';"><strong><span style="font-weight: normal;"><span style="font-family: arial, verdana, tahoma, sans-serif; font-size: 13px; line-height: 20px;"><em>This post is part of an iOS development series called Back To Basics.  You can keep up with this series through the <a style="text-decoration: none; color: #004199; padding: 0px; margin: 0px;" href="/2011/07/12/iphone-development-back-to-basics/">table of contents</a>, <a style="text-decoration: none; color: #004199; padding: 0px; margin: 0px;" href="/tag/b2b/feed/">RSS feed</a>, or <a style="text-decoration: none; color: #004199; padding: 0px; margin: 0px;" href="https://twitter.com/#!/brandontreb">Twitter</a>.</em></span></span></strong></span></p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2011/08/17/back-to-basics-hello-iphone/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Back To Basics &#8211; Getting Set Up For iOS Development</title>
		<link>http://icodeblog.com/2011/07/18/back-to-basics-getting-set-up-for-ios-development/</link>
		<comments>http://icodeblog.com/2011/07/18/back-to-basics-getting-set-up-for-ios-development/#comments</comments>
		<pubDate>Mon, 18 Jul 2011 18:44:01 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[b2b]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[getting started]]></category>
		<category><![CDATA[iOS]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=3240</guid>
		<description><![CDATA[For this tutorial, we are going to discuss the basics of getting set up for developing on the iOS platform.   While there are many 3rd party tools that allow you to develop a number of ways, I am only going to discuss the officially supported ones from Apple.  That being said, let&#8217;s get started.
The Hardware

The first thing you are going to need is an Intel Based Apple Computer running OSX 10.6.6 or better.  Hackintosh computers have  ...]]></description>
				<content:encoded><![CDATA[<p>For this tutorial, we are going to discuss the basics of getting set up for developing on the iOS platform.   While there are many 3rd party tools that allow you to develop a number of ways, I am only going to discuss the officially supported ones from Apple.  That being said, let&#8217;s get started.</p>
<h4>The Hardware</h4>
<p><img src="/wp-content/uploads/2011/07/overview_display20110426.png" alt="" width="350" /></p>
<p>The first thing you are going to need is an Intel Based Apple Computer running OSX 10.6.6 or better.  Hackintosh computers have been know to allow you to develop on them however, they are not recommended as they require quite a bit of configuration.  So, your best best is to develop on the real thing.</p>
<p>Besides that, who wants to use a Windowz box anyway?</p>
<h4>The Account</h4>
<p><img src="http://f.cl.ly/items/11000Q2N0Q242s0y1O1I/Screen%20shot%202011-07-18%20at%2011.57.16%20AM.png" alt="" width="350" /></p>
<p>To become a bonafide iOS developer, you must sign up for <a href="http://developer.apple.com/devcenter/ios/index.action">Apple&#8217;s iOS Developer program</a>.  It&#8217;s free until you are ready to submit your applications to the App Store.  So, if you are just tinkering with iOS development and want to give it a shot, there is no risk.</p>
<p>Once you sign up, you will have access to the latest SDK, developer articles, sample code, documentation, XCode, and more!  What are you waiting for? <a href="http://developer.apple.com/devcenter/ios/index.action">Sign Up Now!</a></p>
<p>Once you are ready to deploy your first application, you will need to upgrade your account for the low low price of $99.  Also, you must pay this fee when you are ready to test on your own device.  The free account limits you to using only the simulator for testing.</p>
<p>One cool thing this gets you (besides the ability to submit applications to the App Store) is you get to download all of the early iOS releases.  As I write this post, I&#8217;m running iOS 5 beta on my device which isn&#8217;t slated to come out for another couple months.</p>
<h4>The Software</h4>
<p><img src="http://f.cl.ly/items/2m362U3E2Y280A331u28/Screen%20shot%202011-07-18%20at%2012.20.14%20PM.png" alt="" width="650" /></p>
<p>In order to develop for the iOS platform, you <strong>must</strong> have the iOS SDK installed.   Apple has nicely bundled their SDK up along with some other great tools such as XCode in order to make it super easy for you to install and get coding.</p>
<p>With the download your get quite a bit (from Apple&#8217;s site)</p>
<blockquote><p><em>&#8220;This is the complete Xcode developer toolset for Mac, iPhone, and iPad. It includes the Xcode IDE, iOS Simulator, and all required tools and frameworks for building Mac OS X and iOS apps.&#8221;</em></p></blockquote>
<p>So even if you don&#8217;t have an iOS device (I didn&#8217;t when I first started developing AND BLOGGING on iCodeBlog), you can still develop and test your applications.  One note on this however is, <strong>always</strong> test on a <em>real</em> device before submitting an application to the store.  The simulator is a great tool to use while testing but is very misleading in terms of performance abd behavior.</p>
<h4>Conclusion</h4>
<p>Well, I hope that this post has found you well and you are now on your way to creating your first iPhone application.  In the next tutorial in this series, I will be discussing the basics of XCode and setting up your iOS projects.</p>
<p>If you have any comments, questions, or things to add, feel free to post them in the comments section of this post.</p>
<p>Happy iCoding!</p>
<p><span style="font-family: 'Lucida Grande';"><strong><span style="font-weight: normal;"><span style="font-family: arial, verdana, tahoma, sans-serif; font-size: 13px; line-height: 20px;"><i>This post is part of an iOS development series called Back To Basics.  You can keep up with this series through the <a style="text-decoration: none; color: #004199; padding: 0px; margin: 0px;" href="/2011/07/12/iphone-development-back-to-basics/">table of contents</a>, <a style="text-decoration: none; color: #004199; padding: 0px; margin: 0px;" href="/tag/b2b/feed/">RSS feed</a>, or <a style="text-decoration: none; color: #004199; padding: 0px; margin: 0px;" href="https://twitter.com/#!/brandontreb">Twitter</a>.</i></span></span></strong></span></p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2011/07/18/back-to-basics-getting-set-up-for-ios-development/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>iPhone Development &#8211; Back To Basics</title>
		<link>http://icodeblog.com/2011/07/12/iphone-development-back-to-basics/</link>
		<comments>http://icodeblog.com/2011/07/12/iphone-development-back-to-basics/#comments</comments>
		<pubDate>Tue, 12 Jul 2011 22:36:19 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[b2b]]></category>
		<category><![CDATA[beginner]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=3212</guid>
		<description><![CDATA[Wow, can you believe that it has been almost 3 years since I started iCodeblog!?  It seems like just yesterday, That I wrote my <a href="/2008/07/24/iphone-programming-tutorial-1-getting-set-up/">very first tutorial</a> in July of 2008.  Now, iCodeBlog has multiple authors, hundreds of tutorials, and over 5,000 readers per day!
As you know, a few things (to say the least) have changed since we first launched iCodeBlog in 2008 with the way we develop for the iOS platform.  With that being said,  ...]]></description>
				<content:encoded><![CDATA[<p>Wow, can you believe that it has been almost 3 years since I started iCodeblog!?  It seems like just yesterday, That I wrote my <a href="/2008/07/24/iphone-programming-tutorial-1-getting-set-up/">very first tutorial</a> in July of 2008.  Now, iCodeBlog has multiple authors, hundreds of tutorials, and over 5,000 readers per day!</p>
<p>As you know, a few things (to say the least) have changed since we first launched iCodeBlog in 2008 with the way we develop for the iOS platform.  With that being said, many of my early tutorials are now quite dated with old screenshots, old (deprecated) code, and no knowledge of memory management whatsoever. I have also evolved both in development as well as writing.  Since beginning, I have published many tutorials, written over 30 iOS applications, and even <a href="http://manning.com/trebitowski">published a book</a>.</p>
<h4>A New iOS Development Series</h4>
<p>All that being said, I am proud to announce my next tutorial series called <strong>Back To Basics</strong>.  This will be an ongoing series where I will revisit some of the core iOS development concepts for n00bs and experienced developers alike.  I will focus heavily on conventions, clean and clear coding, and really try to aid the reader in developing a solid foundation in iOS development.</p>
<p>Here is a rough roadmap of where we are going (obviously incomplete).  <strong>This will serve as the table of contents as the articles are written.</strong></p>
<ul>
<li><a href="/2011/07/18/back-to-basics-getting-set-up-for-ios-development/">Getting Set Up For iOS Development</a></li>
<li>Basics of Objective-C</li>
<li><a href="/2011/08/17/back-to-basics-hello-iphone/">Hello iOS</a></li>
<li><a href="/2011/10/11/back-to-basics-an-introduction-to-view-controllers/">An Introduction To View Controllers</A></li>
<li> Survey of Native Interface Components
<ul>
<li>Views</li>
<li>Table Views</li>
<li>Labels</li>
<li>Image Views</li>
<li>Pickers</li>
<li>Sliders</li>
<li>etc&#8230;</li>
</ul>
</li>
<li> Then we start the good stuff&#8230;). iOS Frameworks</li>
<li>Image and Video</li>
<li>Audio Recording and Playback</li>
<li>Location</li>
<li>Maps</li>
<li>GameKit</li>
<li>Game Center</li>
<li>etc&#8230;</li>
</ul>
<p>* Note that this list by no means is sorted or complete,  it&#8217;s just to give you an idea of what I plan on working on going forward. At some point, I will have a formal method for submitting suggestions for tutorials to be considered.</p>
<h4>But iCodeBlog Seems To Lack In The Post Frequency Department Lately</h4>
<p>This is true, and I&#8217;m very sorry about that.  Over the years iCodeBlog has gone through many transitions from being acquired by <a href="http://elctech.com">ELC Technologies</a> to partnering with <a href="http://velummedia.com/">Velum Media</a>.  This has caused a lot of confusion and lack of motivation to create new content.  We now have quite a few dedicated authors with some real incentives to write new content.</p>
<p>So make sure you <a href="http://feeds.feedburner.com/icodeblog">subscribe to the RSS feed</a> and stay tuned, because iCodeBlog is about to enter its best season yet.</p>
<p>Happy iCoding!</p>
<p><span style="font-family: 'Lucida Grande';"><strong><span style="font-weight: normal;"><span style="font-family: arial, verdana, tahoma, sans-serif; font-size: 13px; line-height: 20px;"><em>This post is part of an iOS development series called Back To Basics.  You can keep up with this series through the <a style="text-decoration: none; color: #004199; padding: 0px; margin: 0px;" href="/2011/07/12/iphone-development-back-to-basics/">table of contents</a>, <a style="text-decoration: none; color: #004199; padding: 0px; margin: 0px;" href="/tag/b2b/feed/">RSS feed</a>, or <a style="text-decoration: none; color: #004199; padding: 0px; margin: 0px;" href="https://twitter.com/#!/brandontreb">Twitter</a>.</em></span></span></strong></span></p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2011/07/12/iphone-development-back-to-basics/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>ELCTextFieldCell &#8211; A Useful TableViewCell for Forms</title>
		<link>http://icodeblog.com/2011/01/04/elctextfieldcell-a-useful-tableviewcell-for-forms/</link>
		<comments>http://icodeblog.com/2011/01/04/elctextfieldcell-a-useful-tableviewcell-for-forms/#comments</comments>
		<pubDate>Tue, 04 Jan 2011 13:46:52 +0000</pubDate>
		<dc:creator><![CDATA[Collin]]></dc:creator>
				<category><![CDATA[Featured]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[beginner]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=2849</guid>
		<description><![CDATA[Hello iCoders, today I am going to be open sourcing a UITableViewCell we have created over at ELC that we have found to be very useful. When developing apps we have found that many times a form of some type is required. This is common in Registration forms, contact forms, feedback forms, etc. The problem was writing the same basic code over and over to have an elegant fast form experience for the user. To quicken the development time of these elements we created the ELCTextFieldCell class which facilitates the creation and flow of a larger form. In this post I will be walking you through the usage of the class.]]></description>
				<content:encoded><![CDATA[<p>Hello iCoders, today I am going to be open sourcing a UITableViewCell we have created over at ELC that we have found to be very useful. When developing apps we have found that many times a form of some type is required. This is common in Registration forms, contact forms, feedback forms, etc. The problem was writing the same basic code over and over to have an elegant fast form experience for the user. To quicken the development time of these elements we created the ELCTextFieldCell class which facilitates the creation and flow of a larger form. In this post I will be walking you through the usage of the class.</p>
<h2>GIT Hub</h2>
<p>You can find a demo project with this <a href="https://github.com/elc/ELCTextFieldCell">code hosted on GitHub</a>. Follow us if you find it useful, we try to open source as much as we can.</p>
<h2>Screencast</h2>
<p>Here is a fast video demo of a simple form that can be made using these cells.</p>
<p><iframe src="http://player.vimeo.com/video/18436192?portrait=0&amp;color=ff9933" width="600" height="375" frameborder="0"></iframe>
<p><a href="http://vimeo.com/18436192">ELCTextFieldCell Demo</a> from <a href="http://vimeo.com/user2008025">Collin Ruffenach</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<h2>How to use</h2>
<p>This class is best used by defining to arrays in the header of the table view controller that is going to use it. I like to call mine labels and placeholders. In these arrays store the values that are going to be the left labels for your forms cells and the placeholders for the cells. I create these in the viewDidLoad method most commonly. The reason I like to do this is because it allows for less code to be written through many other method in the table view controller. From here we need to define 6 more methods, most of which are pre defined by Apple in your table view controller class.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">&nbsp;</pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">&nbsp;</pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>numberOfSectionsInTableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">1</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<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 numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>section <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>labels count<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td 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>
&nbsp;
    <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;
    ELCTextfieldCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>ELCTextfieldCell<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</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>ELCTextfieldCell alloc<span style="color: #002200;">&#93;</span> initWithStyle<span style="color: #002200;">:</span>UITableViewCellStyleDefault 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>
&nbsp;
	<span style="color: #002200;">&#91;</span>self configureCell<span style="color: #002200;">:</span>cell atIndexPath<span style="color: #002200;">:</span>indexPath<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Here I like to have a configureCell:AtIndexPath method to keep my delegate UITableViewCellDelegate methods a bit cleaner. In this case my 4th cell (row 3) is for a phone number, you can obviously change this depending on your needs.</p>

<div class="wp_syntax"><table><tr><td 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>configureCell<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>ELCTextfieldCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>cell atIndexPath<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>
&nbsp;
	cell.leftLabel.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.labels objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span>;
	cell.rightTextField.placeholder <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.placeholders objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span>;
	cell.indexPath <span style="color: #002200;">=</span> indexPath;
	cell.delegate <span style="color: #002200;">=</span> self;
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>indexPath.row <span style="color: #002200;">==</span> <span style="color: #2400d9;">3</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		<span style="color: #002200;">&#91;</span>cell.rightTextField setKeyboardType<span style="color: #002200;">:</span>UIKeyboardTypeNumberPad<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>All that is left to do is implement the ELCTextFieldDelegate methods. The first we will implement is the method that will be called whenever text is changed within the text field of the cell. In this case I just print out all the data going through this method but in most cases you will be applying these changes to your model.</p>

<div class="wp_syntax"><table><tr><td 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>updateTextLabelAtIndexPath<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: #a61390;">string</span><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><span style="color: #a61390;">string</span> <span style="color: #002200;">&#123;</span>
&nbsp;
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;See input: %@ from section: %d row: %d, should update models appropriately&quot;</span>, <span style="color: #a61390;">string</span>, indexPath.section, indexPath.row<span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The final method that you implement will be the method called when return is hit by the user when typing in the rightTextField of the cell. Here you will first check if it is anything but the bottom most cell. If it is, you will increment the IndexPath row and then request that the rightTextField of this next cell becomes the new first responder, which will make the field active. Then you tell the tableview to make sure that this new index path cell is visible. With long forms the tableview will scroll with the users navigation through each field which is very nice. If it is indeed returning from the last cell all we do is tell the textFieldCell to resign first responder so the keyboard disappears.</p>

<div class="wp_syntax"><table><tr><td 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>textFieldDidReturnWithIndexPath<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>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>indexPath.row &lt; <span style="color: #002200;">&#91;</span>labels count<span style="color: #002200;">&#93;</span><span style="color: #002200;">-</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span>path <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSIndexPath</span> indexPathForRow<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">+</span><span style="color: #2400d9;">1</span> inSection<span style="color: #002200;">:</span>indexPath.section<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span>ELCTextfieldCell<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>self.tableView cellForRowAtIndexPath<span style="color: #002200;">:</span>path<span style="color: #002200;">&#93;</span> rightTextField<span style="color: #002200;">&#93;</span> becomeFirstResponder<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>self.tableView scrollToRowAtIndexPath<span style="color: #002200;">:</span>path atScrollPosition<span style="color: #002200;">:</span>UITableViewScrollPositionTop animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span>ELCTextfieldCell<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#91;</span>self.tableView cellForRowAtIndexPath<span style="color: #002200;">:</span>indexPath<span style="color: #002200;">&#93;</span> rightTextField<span style="color: #002200;">&#93;</span> resignFirstResponder<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>That&#8217;s all and you&#8217;re done! This should be a super fast way to make forms in your apps. Let me know any questions and happy iCoding.</p>
<p>Follow me on twitter <a href="http://www.twitter.com/cruffenach">@cruffenach</a></p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2011/01/04/elctextfieldcell-a-useful-tableviewcell-for-forms/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>iPad Programming Tutorial &#8211; Hello World++</title>
		<link>http://icodeblog.com/2010/04/05/ipad-programming-tutorial-hello-world/</link>
		<comments>http://icodeblog.com/2010/04/05/ipad-programming-tutorial-hello-world/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 16:00:23 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[ipad]]></category>
		<category><![CDATA[ipad coding]]></category>
		<category><![CDATA[ipad programming]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=1735</guid>
		<description><![CDATA[Now, that the iPad has been released, I'm sure you are all scrambling for ideas on how to snag a piece of the maket in the imminent gold rush.  iCodeBlog is going to help you on your journey with a series of iPad tutorials to come.

Since the iPad uses the same SDK as the iPhone, all of the code under the hood is almost identical.  Actually, when looking at the new and changed API classes, you will realize that most of them are user interface related.  This is good news for us since we have already been coding iPhone.

While this tutorial is called "Hello World", it is really much more than that.  I assume you already have working knowledge of iPhone/Objective-C programming.]]></description>
				<content:encoded><![CDATA[<h4>Introduction</h4>
<p>Now, that the iPad has been released, I&#8217;m sure you are all scrambling for ideas on how to snag a piece of the maket in the imminent gold rush.  iCodeBlog is going to help you on your journey with a series of iPad tutorials to come.</p>
<p>Since the iPad uses the same SDK as the iPhone, all of the code under the hood is almost identical.  Actually, when looking at the <a href="http://developer.apple.com/iphone/prerelease/library/releasenotes/General/WhatsNewIniPhoneOS/Articles/iPhoneOS3_2.html#//apple_ref/doc/uid/TP40009337-SW1">new and changed API classes</a>, you will realize that most of them are user interface related.  This is good news for us since we have already been coding iPhone.</p>
<p>While this tutorial is called &#8220;Hello World&#8221;, it is really much more than that.  I assume you already have working knowledge of iPhone/Objective-C programming.</p>
<h4>What We Will Be Creating</h4>
<p>In today&#8217;s tutorial, I will be showing you how to create an iPad project that uses the UISplitViewController to display content in 2 separate panes.  We will also be touching on some of the new design/UI patterns and giving an overall introduction to iPad programming.</p>
<p>The project will be based on one of my <a href="/2008/08/08/iphone-programming-tutorial-populating-uitableview-with-an-nsarray/" target="_blank">earliest tutorials that displayed a list of fruit</a> in a UITableView and drilled down when they were selected.  We will be expanding on that example and creating something that will look like this.</p>
<p><a href="/wp-content/uploads/2010/02/watermelon-ss1.png"><img class="alignnone size-full wp-image-1737" title="watermelon-ss" src="/wp-content/uploads/2010/02/watermelon-ss1.png" alt="" width="500" height="387" /></a></p>
<p>It uses a UISplitViewController to display a UITableView on the left and a UIView with a UIImageView subview on the right.  This project is actually quite simple to create as the template code provides much of the code we need to get started.</p>
<h4>Getting Started</h4>
<p>1. Make sure you have downloaded the 3.2 SDK form <a href="http://developer.apple.com/iphone/">http://developer.apple.com/iphone/</a>.  The iPad simulator will come with this download.</p>
<p>2. Download the resources needed for this project and unzip them <a href="/wp-content/uploads/2010/02/iPadHelloWorldResources1.zip">iPadHelloWorldResources.zip</a> . (contains image files and a plist we will be using to load the images)</p>
<h4>Creating The Project</h4>
<p>Starting a project for the iPad is no different than starting one for the iPhone.  When you open XCode and select File-&gt;New Project, you should notice the addition of a Split View-Based Application.  Select this and name it iPadHelloWorld.</p>
<p><a href="/wp-content/uploads/2010/02/Screen-shot-2010-02-01-at-12.37.12-PM1.png"><img class="alignnone size-full wp-image-1739" title="Screen shot 2010-02-01 at 12.37.12 PM" src="/wp-content/uploads/2010/02/Screen-shot-2010-02-01-at-12.37.12-PM1.png" alt="" width="544" height="418" /></a></p>
<p>This will create a basic application with a UITableView on the left and a UIView on the right.  It will even populate the table with some sample elements.  It will add the following files to your project.</p>
<p><a href="/wp-content/uploads/2010/02/Screen-shot-2010-02-02-at-11.02.17-AM1.png"><img class="alignnone size-full wp-image-1740" title="Screen shot 2010-02-02 at 11.02.17 AM" src="/wp-content/uploads/2010/02/Screen-shot-2010-02-02-at-11.02.17-AM1.png" alt="" width="247" height="349" /></a></p>
<p>Here is a brief description of each of these files:</p>
<ul>
<li>iPadHelloWorldAppDelegate &#8211; This is similar to every app delegate.  If you look in the application:didFinishLaunchingWithOptions method, you will see that the UISplitViewController is being allocated with the MasterViewController and DetailViewControllers.</li>
<li>MasterViewController &#8211; A UITableViewController, nothing fancy.  It will be handling the view on the left hand side.</li>
<li>DetailViewController &#8211; This handles the content view that you see on the right hand side.  We will be updating this as the user selects different rows in the table to the left.  This simply houses a single view.</li>
</ul>
<p>Go ahead and press Build and Run to check out the application.  If you haven&#8217;t already done so, play around with the iPad contacts and settings apps as well.</p>
<p><strong>Note: When you launch the application, you will only see the main view since the simulator runs in vertical mode.  To see the views side-by-side, rotate the simulator by clicking &#8220;Hardware -&gt; Rotate Left/Right&#8221;.  You can also press CMD-&gt;Arrow Left/Right on the keyboard.</strong></p>
<h4>Importing The Project Images</h4>
<p>Once you have had some time to play with the new iPad project, you will now need to import the images needed for this project.  After downloading and unzipping the files in from this tutorial, drag them into the project folder called &#8220;Resources-iPad&#8221;.</p>
<p>XCode will prompt you to copy the files, check yes and click OK.</p>
<p><a href="/wp-content/uploads/2010/02/Screen-shot-2010-02-02-at-11.30.31-AM1.png"><img class="alignnone size-full wp-image-1741" title="Screen shot 2010-02-02 at 11.30.31 AM" src="/wp-content/uploads/2010/02/Screen-shot-2010-02-02-at-11.30.31-AM1.png" alt="" width="450" height="413" /></a></p>
<p>Make sure you include all 4 images files as well as the file named fruits.plist.</p>
<h4>Displaying The List Of Fruits</h4>
<p>Displaying our fruits list is no different than displaying data in any other UITableView.  Let&#8217;s begin by opening MasterViewController.h and adding a declaration for our fruits array.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import</span>
&nbsp;
<span style="color: #a61390;">@class</span> DetailViewController;
&nbsp;
<span style="color: #a61390;">@interface</span> MasterViewController <span style="color: #002200;">:</span> UITableViewController <span style="color: #002200;">&#123;</span>
    DetailViewController <span style="color: #002200;">*</span>detailViewController;
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span> fruits;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet DetailViewController <span style="color: #002200;">*</span>detailViewController;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>fruits;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>As you can see, there is nothing new here.  We simply declare our fruits array and create a property for it.</p>
<p>We will be loading the fruits from the plist file that you imported into your project in the last step.  Loading content from a plist file is a very quick and easy solution when you don&#8217;t require a database.</p>
<p>Open up MasterViewController.m and add the following line of code to your viewDidLoad method.</p>

<div class="wp_syntax"><table><tr><td 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>;
    self.fruits <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithContentsOfFile<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;fruits&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;plist&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> retain<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The file fruits.plist is essentially an array that has been written out to a file.  If you open it up, it looks very similar to XML.  Now that our fruits array has been populated, let&#8217;s implement each of the UITableView delegate and datasource methods to populate the table.</p>
<p><strong>UITableView datasource methods</strong></p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<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>aTableView numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>section <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Return the number of rows in the section.</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>fruits count<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<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>
&nbsp;
    <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;CellIdentifier&quot;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Dequeue or create a cell of the appropriate type.</span>
	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> initWithStyle<span style="color: #002200;">:</span>UITableViewCellStyleDefault reuseIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
        cell.accessoryType <span style="color: #002200;">=</span> UITableViewCellAccessoryNone;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Get the object to display and set the value in the cell.</span>
    cell.textLabel.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.fruits objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Nothing special&#8230; We first tell the tableview that we want fruits.count (4 in this case) number of rows.</p>
<p>Next, we display the name of the fruit in each tableview cell.  If you want to learn more on UITableViews, read <a href="/2008/08/08/iphone-programming-tutorial-populating-uitableview-with-an-nsarray/">this tutorial</a>.</p>
<p><strong>UITableView delegate methods</strong></p>

<div class="wp_syntax"><table><tr><td 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>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>aTableView didSelectRowAtIndexPath<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>
&nbsp;
    <span style="color: #11740a; font-style: italic;">/*
     When a row is selected, set the detail view controller's detail item to the item associated with the selected row.
     */</span>
    detailViewController.detailItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.fruits objectAtIndex<span style="color: #002200;">:</span> indexPath.row<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Here, we are simply setting the detailItem property of the detailViewController to the selected fruit.  We will discuss this property later in this section, but for now all you need to know is that its type is <strong>id.</strong></p>
<p>At this point, go ahead and press Build and Run to see your code in action.  You should see something that looks like this:</p>
<p><a href="/wp-content/uploads/2010/02/tableview1.png"><img class="alignnone size-full wp-image-1744" title="tableview" src="/wp-content/uploads/2010/02/tableview1.png" alt="" width="500" height="387" /></a></p>
<p>It displays the list of fruits, but nothing happens when you select a cell (well the title of the detailView may change).</p>
<p>Now that we have our list of fruits displayed, we now need to implement the code to display their corresponding image.</p>
<h4>Displaying The Fruits</h4>
<p>Displaying the selected fruit is actually quite simple.  The first thing we need to do is add a UIImageView to our detailView.</p>
<p>Start by adding the IBOutlet for the image view.  Open up DetailViewController.h and add the following code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> DetailViewController <span style="color: #002200;">:</span> UIViewController  <span style="color: #002200;">&#123;</span>
&nbsp;
    UIPopoverController <span style="color: #002200;">*</span>popoverController;
    UINavigationBar <span style="color: #002200;">*</span>navigationBar;
&nbsp;
    <span style="color: #a61390;">id</span> detailItem;
&nbsp;
    IBOutlet UIImageView <span style="color: #002200;">*</span> fruitImageView;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> UIPopoverController <span style="color: #002200;">*</span>popoverController;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet UINavigationBar <span style="color: #002200;">*</span>navigationBar;
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">id</span> detailItem;
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet UIImageView <span style="color: #002200;">*</span> fruitImageView;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>All of the code here comes with the template except the code to add the IBOutlet UIImageView.  Once you have added this open up DetailView.xib in interface builder.</p>
<p>Add a UIImageView on to the view and size it to 500&#215;500.</p>
<p><a href="/wp-content/uploads/2010/02/notitle1.png"><img class="alignnone size-full wp-image-1746" title="notitle" src="/wp-content/uploads/2010/02/notitle1.png" alt="" width="500" height="672" /></a></p>
<p>Now, click on the File&#8217;s Owner object and open the connection inspector (Tools -&gt; connection inspector).</p>
<p>Drag from your imageView IBOutlet to the UIImageView and release.  The UIImageView is now connected to your outlet.</p>
<p><strong>Note: If you want the images to not skew, set the content mode of the image view (in the attributes inspector) to Aspect Fit.</strong></p>
<p>Now that the imageview has been connected, it&#8217;s time to write the code that updates it.  Close Interface Builder and open the file DetailViewController.m and add the following lines to the setDetailItem method:</p>

<div class="wp_syntax"><table><tr><td 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>setDetailItem<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>newDetailItem <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>detailItem <span style="color: #002200;">!=</span> newDetailItem<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>detailItem release<span style="color: #002200;">&#93;</span>;
        detailItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>newDetailItem retain<span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #11740a; font-style: italic;">// Update the view.</span>
        navigationBar.topItem.title <span style="color: #002200;">=</span> detailItem;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span> imageName <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;%@.png&quot;</span>,detailItem<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self.fruitImageView setImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span>imageName<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>popoverController <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>popoverController dismissPopoverAnimated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Most of this code has been added by the template and I won&#8217;t discuss it too much in this tutorial.  But for now, the important additions are the lines that load the image based on  the name of the fruit and the one below it that sets the image property of the image view.</p>
<p>There you have it! Build and go and the application should function as mentioned.  Here is another screenshot of the final product.</p>
<p><a href="/wp-content/uploads/2010/02/cherry1.png"><img class="alignnone size-full wp-image-1747" title="cherry" src="/wp-content/uploads/2010/02/cherry1.png" alt="" width="500" height="387" /></a></p>
<h4>Another Cool Feature Of SplitViewController</h4>
<p>When in vertical mode, the SplitViewController gives you another new UI element called the UIPopOverView.  Collin will have a tutorial up soon on this view, but the figure below shows you what I&#8217;m talking about.</p>
<p><a href="/wp-content/uploads/2010/02/popover1.png"><img class="alignnone size-full wp-image-1748" title="popover" src="/wp-content/uploads/2010/02/popover1.png" alt="" width="500" height="645" /></a></p>
<p>When the device is vertical, it will automatically rotate your view and provide a UIPopoverView when the &#8220;Master List&#8221; button is pretty. (BTW this button is also customizable).</p>
<p>You may download the source for this tutorial here <a href="/wp-content/uploads/2010/02/iPadHelloWorld1.zip">iPadHelloWorld.zip</a>.</p>
<p>If you have questions, post them here or<a href="http://twitter.com/brandontreb"> ask me on Twitter</a>.</p>
<p>Happy iCoding!</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/04/05/ipad-programming-tutorial-hello-world/feed/</wfw:commentRss>
		<slash:comments>78</slash:comments>
		</item>
		<item>
		<title>Pick Your Own Adventure iPhone Edition</title>
		<link>http://icodeblog.com/2010/03/24/choose-your-own-adventure-iphone-edition/</link>
		<comments>http://icodeblog.com/2010/03/24/choose-your-own-adventure-iphone-edition/#comments</comments>
		<pubDate>Wed, 24 Mar 2010 17:33:54 +0000</pubDate>
		<dc:creator><![CDATA[Collin]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[Featured]]></category>
		<category><![CDATA[UIAlertView]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=1832</guid>
		<description><![CDATA[
A few days ago I came across this cool Pick Your Own Adventure game on <a href="http://twitter.com/peretti/status/10731245477" target="_blank">Twitter</a> from user <a href="http://twitter.com/peretti" target="_blank">Peretti</a>. I decided to make a quick renewable application that used UIAlertViews and UIActionSheets to tell the story. We are going to implement the UIAlertView and UIActionSheet protocol methods in order to get the functionality we are looking for. This will be a short project but will show the important methods associated with these views.
Step 1
Start a new  ...]]></description>
				<content:encoded><![CDATA[<p style="text-align: center;"><img class="aligncenter" src="http://jonverzosa.files.wordpress.com/2009/12/choose-your-own-adventure.jpg" alt="" width="245" height="400" /></p>
<p>A few days ago I came across this cool Pick Your Own Adventure game on <a href="http://twitter.com/peretti/status/10731245477" target="_blank">Twitter</a> from user <a href="http://twitter.com/peretti" target="_blank">Peretti</a>. I decided to make a quick renewable application that used UIAlertViews and UIActionSheets to tell the story. We are going to implement the UIAlertView and UIActionSheet protocol methods in order to get the functionality we are looking for. This will be a short project but will show the important methods associated with these views.</p>
<h3><span style="color: #ff6600;">Step 1</span></h3>
<p>Start a new view based project called CYOA. If you want to add a background image you can add a UIImageView to your CYOAViewController.xib. You can get the one I used <a title="App Background" href="/wp-content/uploads/2010/03/AppBackground1.png" target="_blank">here</a>. But that is not necessary.</p>
<h3><span style="color: #ff6600;">Step 2</span></h3>
<p>In your project we are going to create a new object to hold the pieces that compose and given &#8220;frame&#8221; of a pick your own adventure. A frame of a pick your own adventure game is composed of 6 things as far as I am concerned.</p>
<ol>
<li>Prompt/Title</li>
<li>Choice 1 Text</li>
<li>Choice 2 Text</li>
<li>Choice 1 Tile</li>
<li>Choice 2 Tile</li>
<li>isEnd</li>
</ol>
<p>Through the use of these 6 objects we will be able to create a network of what I have called &#8220;AdventurePieces&#8221;. Create a new file. Make a subclass of NSObject and call it AdventurePiece.</p>
<p>Open AdventurePiece.h and input the following.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import</span>
&nbsp;
<span style="color: #a61390;">@interface</span> AdventurePiece <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>message;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>choice1;
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>choice2;
&nbsp;
	AdventurePiece <span style="color: #002200;">*</span>choice1Piece;
	AdventurePiece <span style="color: #002200;">*</span>choice2Piece;
&nbsp;
	<span style="color: #a61390;">BOOL</span> isEnd;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>message;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>choice1;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>choice2;
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> AdventurePiece <span style="color: #002200;">*</span>choice1Piece;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> AdventurePiece <span style="color: #002200;">*</span>choice2Piece;
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>assign<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">BOOL</span> isEnd;
&nbsp;
<span style="color: #002200;">-</span>initWithMessage<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
	 firstChoice<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>_choice1
	secondChoice<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>_choice2
	  firstPiece<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>AdventurePiece<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>_choice1Piece
	 secondPiece<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>AdventurePiece<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>_choice2Piece
		   isEnd<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>_isEnd;
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<h3><span style="color: #ff6600;">Step 3</span></h3>
<p>We are now going to implement the initialization method that we defined in our header. Open up AdventurePiece.m and input the following.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;AdventurePiece.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> AdventurePiece
&nbsp;
<span style="color: #a61390;">@synthesize</span> message;
<span style="color: #a61390;">@synthesize</span> choice1;
<span style="color: #a61390;">@synthesize</span> choice2;
&nbsp;
<span style="color: #a61390;">@synthesize</span> choice1Piece;
<span style="color: #a61390;">@synthesize</span> choice2Piece;
&nbsp;
<span style="color: #a61390;">@synthesize</span> isEnd;
&nbsp;
<span style="color: #002200;">-</span>initWithMessage<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
	 firstChoice<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>_choice1
	secondChoice<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>_choice2
	  firstPiece<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>AdventurePiece<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>_choice1Piece
	 secondPiece<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>AdventurePiece<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>_choice2Piece
		   isEnd<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>_isEnd
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		message <span style="color: #002200;">=</span> _message;
		choice1 <span style="color: #002200;">=</span> _choice1;
		choice2 <span style="color: #002200;">=</span> _choice2;
		choice1Piece <span style="color: #002200;">=</span> _choice1Piece;
		choice2Piece <span style="color: #002200;">=</span> _choice2Piece;
		isEnd <span style="color: #002200;">=</span> _isEnd;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p>All we need to do in this initialization method is fill in our object parameters appropriately.</p>
<h3><span style="color: #ff6600;">Step 4</span></h3>
<p>Now that we have made the object that will facilitate our story we need to go about making the logic to automate bringing up the adventure pieces in the right order. First we are going to define one object in our view controller header. Go to CYOAViewController.h and input the following.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import</span>
<span style="color: #6e371a;">#import &quot;AdventurePiece.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> MakeMyOwnAdventureViewController <span style="color: #002200;">:</span> UIViewController <span style="color: #002200;">&amp;</span>lt;UIActionSheetDelegate, UIAlertViewDelegate<span style="color: #002200;">&amp;</span>gt;  <span style="color: #002200;">&#123;</span>
&nbsp;
	AdventurePiece <span style="color: #002200;">*</span>currentPiece;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<h3><span style="color: #ff6600;">Step 5</span></h3>
<p>With that done we need to do the following final steps.</p>
<ol>
<li>When the app launches a method should be called that creates a bunch of connected adventure pieces and sets the currentPiece instance variable to the one we desire to start on.</li>
<li>Tell the app to show the currentPiece adventure tile.</li>
<li>Respond to users pushing buttons on the adventure tile by resetting the current piece instance variable.</li>
<p>Pull up a different view when the end of the chain is reached.</ol>
<p>For our purposes we will be have UIAlertViews show the parts of the story before the end. And the final piece of our story will be shown in a UIActionSheet. Lets look at each of the code pieces required to finish off our last four steps.</p>
<p>1. When the app launches a method should be called that creates a bunch of connected adventure pieces and sets the currentPiece instance variable to the one we desire to start on.</p>

<div class="wp_syntax"><table><tr><td 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>makeTheStory <span style="color: #002200;">&#123;</span>
&nbsp;
	AdventurePiece <span style="color: #002200;">*</span>successPiece <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>AdventurePiece alloc<span style="color: #002200;">&#93;</span> initWithMessage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Sparks fly from the red wire but no explosion! Kim Jong Il's evil plot is foiled!&quot;</span> firstChoice<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;YAY&quot;</span> secondChoice<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> firstPiece<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> secondPiece<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> isEnd<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
	AdventurePiece <span style="color: #002200;">*</span>failPiece <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>AdventurePiece alloc<span style="color: #002200;">&#93;</span> initWithMessage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Cutting the blue wire begins a chain reaction - omg that is bad. Like really bad..Kaboom! What went wrong!?!&quot;</span> firstChoice<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Too Bad&quot;</span> secondChoice<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> firstPiece<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> secondPiece<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> isEnd<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
	AdventurePiece <span style="color: #002200;">*</span>failPiece1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>AdventurePiece alloc<span style="color: #002200;">&#93;</span> initWithMessage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Bad Choice. You Die&quot;</span> firstChoice<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Too Bad&quot;</span> secondChoice<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> firstPiece<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> secondPiece<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> isEnd<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
	AdventurePiece <span style="color: #002200;">*</span>middlePieceA <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>AdventurePiece alloc<span style="color: #002200;">&#93;</span> initWithMessage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;You parachute to North Korea, sneak past guards to a live nuclear bomb. Do you&quot;</span> firstChoice<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Cut Red&quot;</span> secondChoice<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Cut Blue&quot;</span> firstPiece<span style="color: #002200;">:</span>successPiece secondPiece<span style="color: #002200;">:</span>failPiece isEnd<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
	AdventurePiece <span style="color: #002200;">*</span>middlePieceB <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>AdventurePiece alloc<span style="color: #002200;">&#93;</span> initWithMessage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;As you are leaving you notice trained assassins behind you&quot;</span> firstChoice<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Run&quot;</span> secondChoice<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Fight&quot;</span> firstPiece<span style="color: #002200;">:</span>failPiece1 secondPiece<span style="color: #002200;">:</span>failPiece1	isEnd<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
	currentPiece <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>AdventurePiece alloc<span style="color: #002200;">&#93;</span> initWithMessage<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;You are assigned to a dangerous mission.&quot;</span> firstChoice<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Accept&quot;</span> secondChoice<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Vacation&quot;</span> firstPiece<span style="color: #002200;">:</span>middlePieceA secondPiece<span style="color: #002200;">:</span>middlePieceB isEnd<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>In this method we make 5 total adventure pieces. This specific pick your own adventure is quite short. It as 3 prompts, 2 choices each with a total of 4 end points. You can see what I mean in this diagram here. The white square are the prompts, the green squares are the choices and the red squares are the final results. We fill in the current piece instance variable as the top most piece and we are done here.</p>
<p><a href="/wp-content/uploads/2010/03/CYOAFlow1.png"><img class="aligncenter size-full wp-image-1862" title="CYOAFlow" src="/wp-content/uploads/2010/03/CYOAFlow1.png" alt="" width="558" height="618" /></a></p>
<p>2. Tell the app to show the currentPiece adventure tile.</p>

<div class="wp_syntax"><table><tr><td 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>
&nbsp;
    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self makeTheStory<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self showStoryForCurrentPiece<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td 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>showStoryForCurrentPiece <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>currentPiece isEnd<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		UIActionSheet <span style="color: #002200;">*</span>actionSheet <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIActionSheet alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>currentPiece message<span style="color: #002200;">&#93;</span> delegate<span style="color: #002200;">:</span>self cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> destructiveButtonTitle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> otherButtonTitles<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>currentPiece choice1<span style="color: #002200;">&#93;</span>,<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Retry&quot;</span>,<span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>actionSheet showInView<span style="color: #002200;">:</span>self.view<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
&nbsp;
		UIAlertView <span style="color: #002200;">*</span>alert <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><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Your Message&quot;</span> message<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>currentPiece message<span style="color: #002200;">&#93;</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: #002200;">&#91;</span>currentPiece choice1<span style="color: #002200;">&#93;</span>,<span style="color: #002200;">&#91;</span>currentPiece choice2<span style="color: #002200;">&#93;</span>,<span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>alert show<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>This method should be called every time a new current piece is set. If checks to see if the adventure piece is an ending piece, if it is then an action sheet it created. If it is not an alert view is created. The button titles, and prompts are filled in appropriately.</p>
<p>Respond to users pushing buttons on the adventure tile by resetting the current piece instance variable.</p>

<div class="wp_syntax"><table><tr><td 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>alertView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIAlertView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>alertView willDismissWithButtonIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>buttonIndex <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>buttonIndex <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		currentPiece <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>currentPiece choice1Piece<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>self showStoryForCurrentPiece<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #a61390;">else</span>
	<span style="color: #002200;">&#123;</span>
		currentPiece <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>currentPiece choice2Piece<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>self showStoryForCurrentPiece<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>When an alert view button is pressed this method will be called. An adventure piece that brings up an alert view should always have connected adventure pieces for both of the possible answers it as. This resets the currentPiece adventure piece and show the current piece again.</p>
<p>Pull up a different view when the end of the chain is reached</p>

<div class="wp_syntax"><table><tr><td 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>actionSheet<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIActionSheet <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>actionSheet clickedButtonAtIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>buttonIndex <span style="color: #002200;">&#123;</span>
&nbsp;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>buttonIndex <span style="color: #002200;">==</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
		<span style="color: #002200;">&#91;</span>self makeTheStory<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#91;</span>self showStoryForCurrentPiece<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>If this method is called that means that a action piece that was at the end of a chain was brought up. If the users hits the second button (button id == 1) that means they want to reset the story. This can be performed by building our story again showing the current piece again.</p>
<p>You can change the makeTheStory method to  be whatever type of story you want. You can download the project <a href="/wp-content/uploads/2010/03/MakeMyOwnAdventure11.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2010/03/24/choose-your-own-adventure-iphone-edition/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
