<?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; tutorial</title>
	<atom:link href="/tag/tutorial/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=4.1.1</generator>
	<item>
		<title>Disable the iPhone&#8217;s Front Camera</title>
		<link>http://icodeblog.com/2011/08/23/disable-the-iphones-front-camera/</link>
		<comments>http://icodeblog.com/2011/08/23/disable-the-iphones-front-camera/#comments</comments>
		<pubDate>Tue, 23 Aug 2011 20:44:00 +0000</pubDate>
		<dc:creator><![CDATA[Marc Charbonneau]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Camera]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[iPhone Coding]]></category>
		<category><![CDATA[iphone programming]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=3334</guid>
		<description><![CDATA[The iPhone 4&#8217;s front camera is limited to 640&#215;480 resolution. Although handy for video conferencing, for some apps that&#8217;s to small to yield a usable photo. Unfortunately the UIImagePickerController class does not have an option to restrict the user from using the front camera. Although you can check the size of the photo after the user is finished, it&#8217;s not great user experience to reject it after they go through the entire process of taking a photo.
One option is to  ...]]></description>
				<content:encoded><![CDATA[<p>The iPhone 4&#8217;s front camera is limited to 640&#215;480 resolution. Although handy for video conferencing, for some apps that&#8217;s to small to yield a usable photo. Unfortunately the UIImagePickerController class does not have an option to restrict the user from using the front camera. Although you can check the size of the photo after the user is finished, it&#8217;s not great user experience to reject it after they go through the entire process of taking a photo.</p>
<p>One option is to replace the standard camera controls with a custom interface, but that&#8217;s a whole lot of work if you just want to prevent the user from taking a photo with the front camera. Fortunately there&#8217;s another option: put a transparent button over the &#8220;switch camera&#8221; button, which will intercept touch events and show an alert dialog. It sounds simple, but as you&#8217;ll see there are a few tricks to actually getting this to work.</p>
<p>We&#8217;re going to use UIImagePickerController&#8217;s <strong>cameraOverlayView</strong> property to add our button to the view hierarchy. We can&#8217;t simply provide a UIButton object though. In the latest iOS SDK, cameraOverlayView is automatically resized to fill the entire screen, while we only want to cover a small corner of it. Instead, we&#8217;re going to put the button inside a UIView subclass that will be used for layout and a few other tasks. Go ahead and create this UIView subclass in your project, call it <strong>ELCCameraOverlayView</strong>, and delete any methods the Xcode template includes by default.</p>
<p>We&#8217;ll need a button, so let&#8217;s start by giving our new subclass a UIButton instance variable named <strong>_button</strong> with a corresponding <strong>button</strong> property. Declare this as you&#8217;d declare any synthesized property, but let&#8217;s override the default setter method to also add it to the view hierarchy.</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>setButton<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIButton <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>button;
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> _button <span style="color: #002200;">!=</span> button <span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #002200;">&#91;</span>_button removeFromSuperview<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>_button release<span style="color: #002200;">&#93;</span>;
        _button <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>button retain<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> button <span style="color: #002200;">!=</span> <span style="color: #a61390;">nil</span> <span style="color: #002200;">&#41;</span>
        	<span style="color: #002200;">&#91;</span>self addSubview<span style="color: #002200;">:</span>button<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>You might create the button in another class (such as a view controller) and assign it to our custom class, but you can also create a standard button in your initWithFrame: method. Note that we still have to call <strong>addSubview:</strong> here as we&#8217;re not using the property accessor method (which is discouraged in init 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>initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>frame;
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span> self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super initWithFrame<span style="color: #002200;">:</span>frame<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        _button <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIButton alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span> 240.0f, 0.0f, 80.0f, 80.0f <span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>self addSubview<span style="color: #002200;">:</span>_button<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>With the button in place, let&#8217;s move on to intercepting touch events which would normally go to the &#8220;switch cameras&#8221; button. We can do this by overriding the UIView methods which are called to determine if an event occurred within a view&#8217;s frame or not. Just check to see if the event took place within the button&#8217;s frame rectangle.</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>UIView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>hitTest<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGPoint<span style="color: #002200;">&#41;</span>point withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event;
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#91;</span>super hitTest<span style="color: #002200;">:</span>point withEvent<span style="color: #002200;">:</span>event<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> self.button <span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span> self.button;
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">nil</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>pointInside<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGPoint<span style="color: #002200;">&#41;</span>point withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event;
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> CGRectContainsPoint<span style="color: #002200;">&#40;</span> self.button.frame, point <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
        <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">NO</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>It may seem like we&#8217;re almost done at this point. If you set the camera overlay view to an instance of this class you&#8217;ll see the &#8220;switch camera&#8221; button should no longer work, and if you give the button an action method you can pop up an alert message telling users not to use the front camera. However, if you rotate the phone to landscape mode, you&#8217;ll notice that while the &#8220;switch camera&#8221; button is re-positioned, our custom button is not! To see what I mean, try setting a custom background color on our button so you can see it on screen.</p>
<p>This is a tricky problem. Unlike UIViewController, our UIView subclass does not have any way of telling when the interface orientation is changed. This doesn&#8217;t even matter though, since UIImagePickerController doesn&#8217;t actually change its interface orientation, it simply re-arranges the camera buttons while remaining in UIInterfaceOrientationPortrait! </p>
<p>The way I&#8217;ve solved this problem is to use the accelerometer to determine the device orientation. It might sound complicated, but it&#8217;s actually not a lot of work. Start by adding a new instance variable <strong>_interfaceOrientation</strong> and property <strong>interfaceOrientation</strong> to your class, of type <strong>UIInterfaceOrientation</strong>. We&#8217;ll also override its setter method to call <strong>setNeedsLayout</strong> whenever its changed.</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>setInterfaceOrientation<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIInterfaceOrientation<span style="color: #002200;">&#41;</span>interfaceOrientation;
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> _interfaceOrientation <span style="color: #002200;">!=</span> interfaceOrientation <span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        _interfaceOrientation <span style="color: #002200;">=</span> interfaceOrientation;
        <span style="color: #002200;">&#91;</span>self setNeedsLayout<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Now let&#8217;s move the button whenever the orientation changes, and implement the required <strong>UIAccelerometerDelegate</strong> 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>layoutSubviews;
<span style="color: #002200;">&#123;</span>
    CGFloat width <span style="color: #002200;">=</span> CGRectGetWidth<span style="color: #002200;">&#40;</span> self.button.frame <span style="color: #002200;">&#41;</span>;
    CGFloat height <span style="color: #002200;">=</span> CGRectGetHeight<span style="color: #002200;">&#40;</span> self.button.frame <span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #a61390;">switch</span> <span style="color: #002200;">&#40;</span> self.interfaceOrientation <span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        <span style="color: #a61390;">case</span> UIInterfaceOrientationPortrait<span style="color: #002200;">:</span>
        <span style="color: #a61390;">case</span> UIInterfaceOrientationPortraitUpsideDown<span style="color: #002200;">:</span>
            self.button.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span> CGRectGetMaxX<span style="color: #002200;">&#40;</span> self.bounds <span style="color: #002200;">&#41;</span> <span style="color: #002200;">-</span> width, 0.0f, width, height <span style="color: #002200;">&#41;</span>;
            <span style="color: #a61390;">break</span>;
        <span style="color: #a61390;">case</span> UIInterfaceOrientationLandscapeRight<span style="color: #002200;">:</span>
            self.button.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span> CGRectGetMaxX<span style="color: #002200;">&#40;</span> self.bounds <span style="color: #002200;">&#41;</span> <span style="color: #002200;">-</span> width, CGRectGetMaxY<span style="color: #002200;">&#40;</span> self.bounds <span style="color: #002200;">&#41;</span> <span style="color: #002200;">-</span> height <span style="color: #002200;">-</span> 50.0f, width, height <span style="color: #002200;">&#41;</span>;
            <span style="color: #a61390;">break</span>;
        <span style="color: #a61390;">case</span> UIInterfaceOrientationLandscapeLeft<span style="color: #002200;">:</span>
            self.button.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span> 0.0f, 0.0f, width, height <span style="color: #002200;">&#41;</span>;
            <span style="color: #a61390;">break</span>;
    <span style="color: #002200;">&#125;</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>accelerometer<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIAccelerometer <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>accelerometer didAccelerate<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIAcceleration <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>acceleration;
<span style="color: #002200;">&#123;</span> 
	CGFloat x <span style="color: #002200;">=</span> <span style="color: #002200;">-</span><span style="color: #002200;">&#91;</span>acceleration x<span style="color: #002200;">&#93;</span>; 
	CGFloat y <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>acceleration y<span style="color: #002200;">&#93;</span>; 
	CGFloat angle <span style="color: #002200;">=</span> <span style="color: #a61390;">atan2</span><span style="color: #002200;">&#40;</span>y, x<span style="color: #002200;">&#41;</span>; 
&nbsp;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> angle &gt;<span style="color: #002200;">=</span> <span style="color: #002200;">-</span>2.25f <span style="color: #002200;">&amp;&amp;</span> angle &lt;<span style="color: #002200;">=</span> <span style="color: #002200;">-</span>0.25f <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
        self.interfaceOrientation <span style="color: #002200;">=</span> UIInterfaceOrientationPortrait;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> angle &gt;<span style="color: #002200;">=</span> <span style="color: #002200;">-</span>1.75f <span style="color: #002200;">&amp;&amp;</span> angle &lt;<span style="color: #002200;">=</span> 0.75f <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
        self.interfaceOrientation <span style="color: #002200;">=</span> UIInterfaceOrientationLandscapeRight;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> angle &gt;<span style="color: #002200;">=</span> 0.75f <span style="color: #002200;">&amp;&amp;</span> angle &lt;<span style="color: #002200;">=</span> 2.25f <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
        self.interfaceOrientation <span style="color: #002200;">=</span> UIInterfaceOrientationPortraitUpsideDown;
	<span style="color: #002200;">&#125;</span>
	<span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> angle &lt;<span style="color: #002200;">=</span> <span style="color: #002200;">-</span>2.25f || angle &gt;<span style="color: #002200;">=</span> 2.25f <span style="color: #002200;">&#41;</span>
	<span style="color: #002200;">&#123;</span>
        self.interfaceOrientation <span style="color: #002200;">=</span> UIInterfaceOrientationLandscapeLeft;
	<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>We&#8217;re almost done! Just make sure to start and stop the accelerometer in your init and dealloc 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>initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGRect<span style="color: #002200;">&#41;</span>frame;
<span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span> self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super initWithFrame<span style="color: #002200;">:</span>frame<span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
        _interfaceOrientation <span style="color: #002200;">=</span> UIInterfaceOrientationPortrait;
        _button <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIButton alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span> 240.0f, 0.0f, 80.0f, 80.0f <span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAccelerometer sharedAccelerometer<span style="color: #002200;">&#93;</span> setDelegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>self addSubview<span style="color: #002200;">:</span>_button<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <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><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc;
<span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAccelerometer sharedAccelerometer<span style="color: #002200;">&#93;</span> setDelegate<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>_button release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Finished! If you haven&#8217;t tested your overlay yet, you can add it to your UIImagePickerController as so.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;">UIImagePickerController <span style="color: #002200;">*</span>controller <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImagePickerController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
ELCCameraOverlayView <span style="color: #002200;">*</span>view <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ELCCameraOverlayView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>controller.view.frame<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>view.button addTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>selectFrontCamera<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> forControlEvents<span style="color: #002200;">:</span>UIControlEventTouchUpInside<span style="color: #002200;">&#93;</span>;
&nbsp;
controller.sourceType <span style="color: #002200;">=</span> UIImagePickerControllerSourceTypeCamera;
controller.cameraCaptureMode <span style="color: #002200;">=</span> UIImagePickerControllerCameraCaptureModePhoto;
controller.cameraDevice <span style="color: #002200;">=</span> UIImagePickerControllerCameraDeviceRear;
controller.delegate <span style="color: #002200;">=</span> self;
controller.cameraOverlayView <span style="color: #002200;">=</span> view;
&nbsp;
<span style="color: #002200;">&#91;</span>self.navigationController presentModalViewController<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;">&#91;</span>view release<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Don&#8217;t forget to provide the button&#8217;s action method. You could show an alert, for instance.</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>selectFrontCamera<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>sender;
<span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>title <span style="color: #002200;">=</span> NSLocalizedString<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Front Camera Disabled&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span> <span style="color: #002200;">&#41;</span>;
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>message <span style="color: #002200;">=</span> NSLocalizedString<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;The front camera does not have sufficient resolution.&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span> <span style="color: #002200;">&#41;</span>;
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>button <span style="color: #002200;">=</span> NSLocalizedString<span style="color: #002200;">&#40;</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Okay&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&quot;</span> <span style="color: #002200;">&#41;</span>;
    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>title message<span style="color: #002200;">:</span>message delegate<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> cancelButtonTitle<span style="color: #002200;">:</span>button otherButtonTitles<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>alert show<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>alert release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>I&#8217;ve uploaded the <a href="https://gist.github.com/1166388">complete ELCCameraOverlayView class files here</a>. Leave a comment if you have any questions!</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2011/08/23/disable-the-iphones-front-camera/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>NSTimer: The Poor Man&#8217;s Threading &#8211; Code Snapshot</title>
		<link>http://icodeblog.com/2009/07/23/nstimer-the-poor-mans-threading-code-snapshot/</link>
		<comments>http://icodeblog.com/2009/07/23/nstimer-the-poor-mans-threading-code-snapshot/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 18:28:26 +0000</pubDate>
		<dc:creator><![CDATA[Collin]]></dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[Games]]></category>
		<category><![CDATA[NSTimer]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=1152</guid>
		<description><![CDATA[Introduction
Hey guys. So usually the posts we put up here involve screencasts and presentations, but we are going to start also posting small less time consuming pieces for the site. Today I bring to you a small project involving NSTimers. Today we are going to build an app that represents a horse race. We will create a view with 6 small UIView squares with a blue background at the bottom of the screen, we will use a timer to move  ...]]></description>
				<content:encoded><![CDATA[<h1><span style="color: #ff6600;">Introduction</span></h1>
<p>Hey guys. So usually the posts we put up here involve screencasts and presentations, but we are going to start also posting small less time consuming pieces for the site. Today I bring to you a small project involving NSTimers. Today we are going to build an app that represents a horse race. We will create a view with 6 small UIView squares with a blue background at the bottom of the screen, we will use a timer to move a random one of them forward a random amount of distance. Let&#8217;s get started!</p>
<p><a href="/wp-content/uploads/2009/07/Picture-162.png"><img class="aligncenter size-full wp-image-1164" title="Picture 16" src="/wp-content/uploads/2009/07/Picture-162.png" alt="Picture 16" width="351" height="693" /></a></p>
<h1><span style="color: #ff6600;">Source</span></h1>
<p>You can get the source here: <a href="/wp-content/uploads/2009/07/NSTimerDemo2.zip">NSTimerDemo</a></p>
<h1><span style="color: #ff6600;">Steps</span></h1>
<h3><span style="color: #ff6600;">Step 1<br />
</span></h3>
<p>Create a view based application in xCode. Call it whatever.</p>
<h3><span style="color: #ff6600;">Step 2<br />
</span></h3>
<p>In your view controller class header file add:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSTimer</span> <span style="color: #002200;">*</span>myTimer;</pre></td></tr></table></div>

<h3><span style="color: #ff6600;">Step 3<br />
</span></h3>
<p>In your view controller class, uncomment out the viewDidLoad method and fill in the following code:</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>;
&nbsp;
      CGRect workingFrame;
      workingFrame.origin.x <span style="color: #002200;">=</span> <span style="color: #2400d9;">15</span>;
      workingFrame.origin.y <span style="color: #002200;">=</span> <span style="color: #2400d9;">400</span>;
      workingFrame.size.width <span style="color: #002200;">=</span> <span style="color: #2400d9;">40</span>;
      workingFrame.size.height <span style="color: #002200;">=</span> <span style="color: #2400d9;">40</span>;
&nbsp;
      <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 <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">6</span>; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span>
      <span style="color: #002200;">&#123;</span>
           UIView <span style="color: #002200;">*</span>myView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>workingFrame<span style="color: #002200;">&#93;</span>;
           <span style="color: #002200;">&#91;</span>myView setTag<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span>;
           <span style="color: #002200;">&#91;</span>myView setBackgroundColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor blueColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
           workingFrame.origin.x <span style="color: #002200;">=</span> workingFrame.origin.x <span style="color: #002200;">+</span> workingFrame.size.width <span style="color: #002200;">+</span> <span style="color: #2400d9;">10</span>;
           <span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>myView<span style="color: #002200;">&#93;</span>;
      <span style="color: #002200;">&#125;</span>
&nbsp;
      myTimer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSTimer</span> scheduledTimerWithTimeInterval<span style="color: #002200;">:</span>.1 target<span style="color: #002200;">:</span>self selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>moveACar<span style="color: #002200;">&#41;</span> userInfo<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> repeats<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<h3><span style="color: #ff6600;">Step 4<br />
</span></h3>
<h3><span style="color: #ff6600;"> </span></h3>
<p>In your view controller class, add the following 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>moveACar
<span style="color: #002200;">&#123;</span>
      <span style="color: #a61390;">int</span> r <span style="color: #002200;">=</span> <span style="color: #a61390;">rand</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">%</span> <span style="color: #2400d9;">6</span>;
      NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;My number is %d&quot;</span>, r<span style="color: #002200;">&#41;</span>;
&nbsp;
      <span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span>UIView <span style="color: #002200;">*</span>aView <span style="color: #a61390;">in</span> <span style="color: #002200;">&#91;</span>self.view subviews<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
      <span style="color: #002200;">&#123;</span>
           <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>aView tag<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> r<span style="color: #002200;">&#41;</span>
           <span style="color: #002200;">&#123;</span>
                <span style="color: #a61390;">int</span> movement <span style="color: #002200;">=</span> <span style="color: #a61390;">rand</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">%</span> <span style="color: #2400d9;">100</span>;
                CGRect workingFrame <span style="color: #002200;">=</span> aView.frame;
                workingFrame.origin.y <span style="color: #002200;">=</span> workingFrame.origin.y <span style="color: #002200;">-</span> movement;
&nbsp;
                <span style="color: #002200;">&#91;</span>UIView beginAnimations<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> context<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span><span style="color: #002200;">&#93;</span>;
                <span style="color: #002200;">&#91;</span>UIView setAnimationDuration<span style="color: #002200;">:</span>.2<span style="color: #002200;">&#93;</span>;
                <span style="color: #002200;">&#91;</span>aView setFrame<span style="color: #002200;">:</span>workingFrame<span style="color: #002200;">&#93;</span>;
                <span style="color: #002200;">&#91;</span>UIView commitAnimations<span style="color: #002200;">&#93;</span>;
&nbsp;
                <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>workingFrame.origin.y <span style="color: #002200;">&amp;</span>lt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
                <span style="color: #002200;">&#123;</span>
                    <span style="color: #002200;">&#91;</span>myTimer invalidate<span style="color: #002200;">&#93;</span>;
                <span style="color: #002200;">&#125;</span>
           <span style="color: #002200;">&#125;</span>
      <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<h1><span style="color: #ff6600;">Conclusion</span></h1>
<p>So that is it. Timers are really cool and come in handy for all sorts of small problems in a project. Happy coding.</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2009/07/23/nstimer-the-poor-mans-threading-code-snapshot/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 4</title>
		<link>http://icodeblog.com/2008/09/22/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-4/</link>
		<comments>http://icodeblog.com/2008/09/22/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-4/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 23:47:39 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[iphone programming]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[sqlite3]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=392</guid>
		<description><![CDATA[This is the final installment of our 4 part series of creating a Todo list for the iPhone.  In this tutorial, I will detail how to add and delete new todo objects from the SQLite database.  Make sure that you have completed the following tutorials before you begin this one:

<a title="Permanent Link to iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 1" rel="bookmark" href="/2008/08/19/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-1/">iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 1</a>
<a  ...]]></description>
				<content:encoded><![CDATA[<p style="text-align: left;">This is the final installment of our 4 part series of creating a Todo list for the iPhone.  In this tutorial, I will detail how to add and delete new todo objects from the SQLite database.  Make sure that you have completed the following tutorials before you begin this one:</p>
<ul style="text-align: left;">
<li><a title="Permanent Link to iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 1" rel="bookmark" href="/2008/08/19/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-1/">iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 1</a></li>
<li><a title="Permanent Link to iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 1" rel="bookmark" href="/2008/09/02/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-2/">iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 2</a></li>
<li><a href="/2008/09/10/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-3/">iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 3</a></li>
</ul>
<p style="text-align: left;">When you have completed this tutorial, you should have a main screen that looks something like this:</p>
<p style="text-align: left;"><a href="/wp-content/uploads/2008/09/mainscreen1.png"><img class="size-full wp-image-394 aligncenter" title="screenshot" src="/wp-content/uploads/2008/09/mainscreen1.png" alt="" width="386" height="742" /></a></p>
<p style="text-align: left;">Let&#8217;s get started&#8230;</p>
<p style="text-align: left;">The first thing we need to do is add the UIBarButtonItem items to the NavigationBar so that we get the &#8220;Edit&#8221; and &#8220;Add&#8221; button.  Open up <strong>RootViewController.m</strong> and add the following code to the <strong>viewDidLoad</strong> method.</p>
<p style="text-align: left;"><a href="/wp-content/uploads/2008/09/viewdidload1.png"><img class="size-full wp-image-396 aligncenter" title="viewdidload" src="/wp-content/uploads/2008/09/viewdidload1.png" alt="" width="500" height="107" /></a></p>
<p style="text-align: left;">The first thing we see is the line that sets the leftBarButtonItem to self.editButtonItem.  This automatically adds the &#8220;Edit&#8221; button to the NavigationController.  Also, it sets up the functionality that allows the &#8220;delete&#8221; buttons to be displayed when the button is pressed.  You can see this functionality if you do a &#8220;Build and Go&#8221; at this step.  Next, I have manually created a UIBarButtonItem and added it to the navigationbar.  This can be done in Interface Builder, but I wanted to show you how to do it manually and assign an action to it (I&#8217;m sure you will require this functionality in a future program).  Here is a break down of the parameters:</p>
<ul style="text-align: left;">
<li>initWithTitle &#8211; The text to be displayed on the button</li>
<li>style &#8211; How the button will look</li>
<li>target &#8211; The class object that handles the messages sent from this button</li>
<li>action &#8211; The method to be called when the button is passed.  We can use @selector and give it the name of the function to call.</li>
</ul>
<p style="text-align: left;">Finally, we assign this button to the rightBarButtonItem.  If you do a Build and Go, it should error since we haven&#8217;t created the addTodo method. We will do that in a bit.  Now, let&#8217;s create a method inside of our Todo object that will add new Todos to the database.</p>
<p style="text-align: left;">Open up <strong>Todo.h</strong> and add the following code:</p>
<p style="text-align: left;"><a href="/wp-content/uploads/2008/09/todo1.png"><img class="alignnone size-full wp-image-398" title="todo" src="/wp-content/uploads/2008/09/todo1.png" alt="" width="363" height="33" /></a></p>
<p style="text-align: left;">So in addition to the <strong>insertNewTodoIntoDatabase</strong> method, we also see the deleteFromDatabase method signature.  I have just added this so I don&#8217;t have to come back to it later.  We will be implementing this when I show you how to delete todos from the database.  One thing to note about the <strong>insertNewTodoIntoDatabase </strong>method is it has a &#8220;+&#8221; rather than a &#8220;-&#8221; sign.  This means that it is a static method.  Static methods are associated with the class not the instance meaning we can call this method without instanciating this class.  So we can do stuff like Todo.insertNewTodoIntoDatabase.  Now we will implement this method.</p>
<p style="text-align: left;">Before we can do this, we must declare a few more static sqlite3_statement&#8217;s.  Add the following statements to the top of <strong>Todo.m</strong></p>
<p style="text-align: left;"><a href="/wp-content/uploads/2008/09/todomstatic1.png"><img class="size-full wp-image-400 aligncenter" title="todomstatic" src="/wp-content/uploads/2008/09/todomstatic1.png" alt="" width="270" height="30" /></a></p>
<p style="text-align: left;">Nothing new here&#8230;Now implement the following method:</p>
<p style="text-align: left;"><a href="/wp-content/uploads/2008/09/todom31.png"><img class="size-full wp-image-399 aligncenter" title="todom3" src="/wp-content/uploads/2008/09/todom31.png" alt="" width="499" height="197" /></a></p>
<p style="text-align: left;">This is similar to our update method.  Notice that we are inserting default values into the database.  This is so we don&#8217;t run into any problems with null or nil values.  The most important part of this method is the fact that it returns the primary key of the newly created todo object.  This will be used later so we can immediately transition to the todo when the &#8220;Add&#8221; button is pressed.  The last thing we need to do to the todo object is update the dehydrate method so that the todoText gets saved if it gets changed.  Update the dehydrate method to look like this:</p>
<p style="text-align: left;"><a href="/wp-content/uploads/2008/09/dehydrate1.png"><img class="size-full wp-image-401 aligncenter" title="dehydrate" src="/wp-content/uploads/2008/09/dehydrate1.png" alt="" width="500" height="257" /></a></p>
<p style="text-align: left;">There are only a few minor changes here.  First we see the &#8220;text = ?&#8221; part added to the sql statement.  This is simply so we can update the text of the todo.  The other change is we bound the self.text property to the 1st question mark in the sql statement.  One thing to notice is we call [self.text UTF8String].  This is because sqlite3_bind_text takes a (char *).  This will convert an NSString to an acceptable format.</p>
<p style="text-align: left;">Now we need to add a method inside of our RootViewController to add a todo.  This is the method that will be called when the user presses the &#8220;Add&#8221; button.  Inside of RootViewController.m add the following code:</p>
<p><a href="/wp-content/uploads/2008/09/1-rootviewcontroller1.png"><img class="size-full wp-image-422 aligncenter" title="1-rootviewcontroller" src="/wp-content/uploads/2008/09/1-rootviewcontroller1.png" alt="" width="500" height="179" /></a></p>
<p style="text-align: left;">First, we get a reference to the appDelegate object.  This is because we need to call its addTodo method.  Next, we instantiate the TodoViewController if it has not already been instantiated.  We need this around because we will push it on to the view stack and transition to it after we create our new todo object.  After this is done, we call the addTodo method of the appDelegate.  It will return the newly created todo object and the view will be transitioned to its detail screen in order to update its details.  Now we need to implement the method addTodo inside of our appDelegate.  Open up todoAppDelegate.h and add the following code to create the method signature.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/2-appdelegateh1.png"><img class="size-full wp-image-423 aligncenter" title="2-appdelegateh" src="/wp-content/uploads/2008/09/2-appdelegateh1.png" alt="" width="107" height="18" /></a></p>
<p style="text-align: left;">
<p style="text-align: left;">Now, let&#8217;s implement this method.  Open up todoAppDelegate.m and add the following code:</p>
<p style="text-align: left;">
<p style="text-align: left;"><a href="/wp-content/uploads/2008/09/3-appdelegatem1.png"><img class="size-full wp-image-424 aligncenter" title="3-appdelegatem" src="/wp-content/uploads/2008/09/3-appdelegatem1.png" alt="" width="500" height="101" /></a></p>
<p style="text-align: left;">
<p>First, we are calling the insertNewTodoIntoDatabase method of the Todo object.  Notice that we are simply calling the method without first building an instance of a todo object.  As I said in tutorial 3, this is because that method is static and gets called without building an instance of the class.  Next, we insatiate the todo object that was just created by calling its initWithPrimaryKey method.  This will give us reference to the new todo object.  Finally, we append this todo to the end of our todos array.  Since our UITableView is updated with this array, it will automatically include the new todo object.  The last line just returns this todo object.</p>
<p>Remember is the last tutorial we made it so the users could update the status and the priority of a todo?  Well, now we also need to give them the ability to update the text of the todo.  So open up TodoViewController.h and add the following code:</p>
<p style="text-align: center;">
<p><a href="/wp-content/uploads/2008/09/5-todoviewcontrollerh1.png"><img class="alignnone size-full wp-image-425" title="5-todoviewcontrollerh" src="/wp-content/uploads/2008/09/5-todoviewcontrollerh1.png" alt="" width="430" height="271" /></a><a href="/wp-content/uploads/2008/09/5-todoviewcontrollerh1.png"><br />
</a></p>
<p style="text-align: left;">
<p>Ok, so I&#8217;m guessing you are wondering why the UITextView for the todoText object has been changed to a UITextField.  Well, I will tell you.  UITextView doesn&#8217;t have the methods that we need to save the text with our current design.  We will also be changing this on our Interface inside of Interface Builder.  So for now, just believe me and anywhere it says UITextView, change it to UITextField.  The only additional code we added here is the method signature for the updateText method.  It&#8217;s an IBAction that will get called when the user presses the &#8220;Done&#8221; button on the keyboard after setting the text for the todo.  Next, we need to implement this method.  Open up TodoViewController.m and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/5-todoviewcontrollerm1.png"><img class="size-full wp-image-426 aligncenter" title="5-todoviewcontrollerm" src="/wp-content/uploads/2008/09/5-todoviewcontrollerm1.png" alt="" width="245" height="53" /></a></p>
<p style="text-align: left;">All this does is update the text of the todo to the text that the user entered inside of the UITextField.  The last thing we need to do in order to add a todo is to replace the UITextView with a UITextField and connect it to our updateText method.  Double click on your TodoViewController.xib file to open it in Interface Builder.</p>
<p>Now click on the UITextView on your interface and press the delete key on your keyboard to delete it.  Now, drag a UITextField from the library and drop it onto your interface.  Resize it to fit.  When you have completed that, your interface should look something like this:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/6-interfacebuilder1.png"><img class="size-full wp-image-427 aligncenter" title="6-interfacebuilder" src="/wp-content/uploads/2008/09/6-interfacebuilder1.png" alt="" width="320" height="502" /></a></p>
<p style="text-align: left;">Now we need to connect this component.  Make sure it is selected and click Tools -&gt; Connections Inspector to open up the connections inspector.  Drag from the circle next to the method &#8220;Did End On Exit&#8221; to the &#8220;File&#8217;s Owner&#8221; object.  The words udpateText should pop up.  Click on them to make the connection.  Next, click in the circle next to &#8220;New Referencing Outlet&#8221; and drag it to the &#8220;File&#8217;s Owner&#8221; object.  Select todoText  when it pops up.  The Connections Inspector should look like this:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/12-ib1.png"><img class="size-full wp-image-434 aligncenter" title="12-ib" src="/wp-content/uploads/2008/09/12-ib1.png" alt="" width="287" height="708" /></a></p>
<p>Now we are done with Interface Builder.  Go ahead and close it.  We are now able to add todos.  The last thing we need to do is give the ability to delete todos from the list as well as our database.  This is all done in code, and we won&#8217;t need interface builder for this.</p>
<p>Let&#8217;s start by adding the methods to the appDelegate to handle the deletion of todos.  Open up todoAppDelegate.h and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/7-appdelegateh1.png"><img class="alignnone size-full wp-image-428" title="7-appdelegateh" src="/wp-content/uploads/2008/09/7-appdelegateh1.png" alt="" width="194" height="16" /></a></p>
<p style="text-align: left;">All we see here is a signature for the removeTodo method.  Also, be sure to add a #import &#8220;Todo.h&#8221; statement to the top of this file so that we can interface with the todo objects. Now let&#8217;s implement the removeTodo method.  Open up todoAppDelegate.m and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/8-appdelegatem1.png"><img class="size-full wp-image-429 aligncenter" title="8-appdelegatem" src="/wp-content/uploads/2008/09/8-appdelegatem1.png" alt="" width="308" height="119" /></a></p>
<p style="text-align: left;">The first line looks up the todo in the todos NSArray.  It returns the index in the array of the todo to be deleted.  Then, we call the deleteFromDatabase method on the todo object and then remove it from the todos array.  Since the UITableView is updated via this array, it will automatically remove the todo without any additional code on our part.</p>
<p>Now, let&#8217;s create the removeTodo method for the todo object. We have already written the method signature in Todo.h in a previous step, so open up Todo.m and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/10-todom1.png"><img class="size-full wp-image-431 aligncenter" title="10-todom" src="/wp-content/uploads/2008/09/10-todom1.png" alt="" width="500" height="182" /></a></p>
<p style="text-align: left;">Remember the delete_statement variable is a static sqlite3_stmt that we declared in a previous step.  First, we check to see if it is nil.  If it is we compile the statement using the sqlite3_prepare statement.  Next, we bind the primary key of the current todo to the &#8220;?&#8221; in the sqlite3 statement.  Next, we just step the statement to execute it and reset it.  The last thing we need to do to delete todos from the database is to specify what happens when the user presses the &#8220;delete&#8221; button.  Open up RootViewController.m and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/11-rootviewcontroller11.png"><img class="size-full wp-image-432 aligncenter" title="11-rootviewcontroller1" src="/wp-content/uploads/2008/09/11-rootviewcontroller11.png" alt="" width="499" height="143" /></a></p>
<p style="text-align: left;">
<p style="text-align: left;">The first step (like the first step of many functions) is to get a reference to the appDelegate.  Next, we check to see if we are currently editing.  If so, call the removeTodo method on appDelegate.  The next line, removes the row from the UITableView at the given indexPath.</p>
<p>Now click Build and Go!  You should now be able to add and delete todo items from the database.  This concludes our four part series of creating a todo list for the iPhone.  As always, if you have any questions feel free to leave them in the comments section.  If you get lost at any time you can download the sample code <a href="/wp-content/uploads/2008/09/todo-part-41.zip">here</a>.</p>
<p>Happy iCoding!</p>
<p style="text-align: left;">
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2008/09/22/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-4/feed/</wfw:commentRss>
		<slash:comments>115</slash:comments>
		</item>
		<item>
		<title>iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 3</title>
		<link>http://icodeblog.com/2008/09/10/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-3/</link>
		<comments>http://icodeblog.com/2008/09/10/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-3/#comments</comments>
		<pubDate>Thu, 11 Sep 2008 01:39:29 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[iphone programming]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[todo]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[uitableview]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=339</guid>
		<description><![CDATA[This is part 3 in our multipart series of creating a todo list for the iPhone.  For this, you must have completed the following tutorials.

<a title="Permanent Link to iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 1" rel="bookmark" href="/2008/08/19/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-1/">iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 1</a>
<a title="Permanent Link to iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 1" rel="bookmark" href="/2008/09/02/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-2/">iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite  ...]]></description>
				<content:encoded><![CDATA[<p style="text-align: left;">This is part 3 in our multipart series of creating a todo list for the iPhone.  For this, you must have completed the following tutorials.</p>
<ul>
<li><a title="Permanent Link to iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 1" rel="bookmark" href="/2008/08/19/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-1/">iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 1</a></li>
<li><a title="Permanent Link to iPhone Programming Tutorial - Creating a ToDo List Using SQLite Part 1" rel="bookmark" href="/2008/09/02/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-2/">iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 2</a></li>
</ul>
<p>The focus of this tutorial will mainly be on viewing the todo items when selected.  I will also show you how to update the todo status.  This will require us to use interface builder.  When you are completed, you will be able to edit todos through an interface similar to this:</p>
<p style="text-align: center; "><a href="/wp-content/uploads/2008/09/screenshot11.png"><img class="size-full wp-image-390 aligncenter" title="screenshot1" src="/wp-content/uploads/2008/09/screenshot11.png" alt="" width="386" height="742" /></a></p>
<h2>Bringing Your Code Up To Speed</h2>
<p>For this tutorial, we will need to get the last variable from the todo database.  This is of course being the status variable.  If you recall, it&#8217;s a boolean value (1 for complete, 0 for in progress).  We need to get it from the database and associate it with the todo object.  First let&#8217;s create a property to hold this value.  Open todo.h and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todoh11.png"><img class="alignnone size-full wp-image-370" title="todoh1" src="/wp-content/uploads/2008/09/todoh11.png" alt="" width="393" height="329" /></a></p>
<p style="text-align: left;">So a few changes here&#8230;First there is the added NSInteger status.  This will be the property we associate with the status (complete or not) of the todo.  We also create a property from it.  Next, there is a BOOL property called &#8220;dirty&#8221;.  We will use this object to signify when a todo has been altered.  You will see how this comes into play when we implement the dehydrate method. Also, I have added 3 method signatures.  updateStatus will be the method called when we want to update our status variable.  Similarly, the updatePriority method will be called to update the priority.  Finally, we have added a dehydrate method.  This method should be familiar (or confusing) if you have messed with Apple&#8217;s books example.  Basically, it will be used to save the state of the todo to the database.  We will be calling this method on each todo item when the program exits.  I will show you how to do this in a little bit.</p>
<p style="text-align: left;">Be sure to add the status variable to the synthesize line.  Also, as we did before, we need to create a static sqlite3_stmt to hold the compiled dehydration statement.  Add the following code to Todo.m:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/synth11.png"><img class="alignnone size-full wp-image-382" title="synth1" src="/wp-content/uploads/2008/09/synth11.png" alt="" width="280" height="74" /></a></p>
<p style="text-align: left;">Now let&#8217;s implement the methods.  Add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todom21.png"><img class="size-full wp-image-371 aligncenter" title="todom2" src="/wp-content/uploads/2008/09/todom21.png" alt="" width="500" height="379" /></a></p>
<p style="text-align: left;">The first two methods (udpateStatus and updatePriority) are pretty strait forward.  They update the status and the priority of the todo and then set the &#8220;dirty&#8221; property to 1.  This signifies that the todo has been altered and will need to be saved to the database.</p>
<p style="text-align: left;">Finally, there is the dehydrate method&#8230; We will call this method on each todo upon termination of the program.  If the todo is &#8220;dirty&#8221; meaning the dirty property was set to YES, we will need to save the new data to the database.  The database code should look pretty similar to code in previous tutorials.  First, we check to see if the dehydrate_statement is equal to nil.  If you recall, this will only happen the first time this method gets called.  Next we create the update statement and then bind our variables to each of the &#8220;?&#8221;&#8216;s.  Notice the ordering.  The numbers represent the question marks from left to right (1 being the first, 2 being the second, 3 being the third).  It took me quite some time to figure this out.  Finally, we execute the sqlite statement by calling sqlite3_step and then reset the statement.</p>
<p style="text-align: left;">The last thing we need to do to Todo.m is change the SELECT statement in the initWithPrimaryKey method to grab the &#8216;complete&#8217; field.  Update the code to look like the screenshot below:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/00-todom1.png"><img class="size-full wp-image-357 aligncenter" title="00-todom" src="/wp-content/uploads/2008/09/00-todom1.png" alt="" width="500" height="322" /></a></p>
<p style="text-align: left;">There are not really many changes.  The first change is the added status to the synthesize line.  Next, the sql statement was updated to read</p>
<p style="text-align: left;">SELECT text,priority,complete FROM todo WHERE pk=?</p>
<p style="text-align: left;">This allows us to get the &#8220;complete&#8221; field from the database.  Finally, there is the line &#8220;self.status = sqlite3_column_in(init_statement,2);&#8221;.  This is assigning the status property to the data at index 2 in the sql data array.  We can now use this field.</p>
<p style="text-align: left;">One thing we need to do for the navigation to function properly is add a title to our main view.  Open up rootViewController.m and add the following code to the viewDidLoad method:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/title1.png"><img class="alignnone size-full wp-image-381" title="title" src="/wp-content/uploads/2008/09/title1.png" alt="" width="175" height="16" /></a></p>
<h2>Create the Todo Detail View</h2>
<p>Now we are going to create the view that will display when the user selects the todo in the UITableView.  Go ahead and open up Interface Builder by selecting one of you existing nib (.xib) files.  Once it&#8217;s open add a new View by clicking <strong>File -&gt; New</strong> and select <strong>View.</strong> Drag the following controls.</p>
<ul>
<li>UITextView</li>
<li>UISegmentedControl &#8211; For this you will need to set the number of segments to 3.  You will also see a dropdown menu below this option.  Select each segment and fill in one of the priorities for the title.  Here is a screenshot. You should give a title to each (Low , Medium, High).
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/segmented-control1.png"><img class="alignnone size-full wp-image-340" title="segmented-control" src="/wp-content/uploads/2008/09/segmented-control1.png" alt="" width="287" height="708" /></a></p>
</li>
<li>UILabel &#8211; This will be used to display the status</li>
<li>UIButton &#8211; Users will click this button to update the status (Mark as complete)</li>
</ul>
<p>When you are done, your interface should look something like this (but probably better):</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/ibscreenshot1.png"><img class="alignnone size-full wp-image-369" title="ibscreenshot" src="/wp-content/uploads/2008/09/ibscreenshot1.png" alt="" width="320" height="502" /></a></p>
<p style="text-align: left;">I know that my interface doesn&#8217;t look the coolest.  I&#8217;m a programmer not a graphic designer&#8230; Ok save this view by pressing Command-S.  Make sure you are in your current projects directory.  Name it <strong>TodoViewController</strong> and press Save<strong>. </strong></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/2-save1.png"><img class="size-full wp-image-343 aligncenter" title="2-save" src="/wp-content/uploads/2008/09/2-save1.png" alt="" width="500" height="392" /></a></p>
<p style="text-align: left;">It will then ask you if you want to add it to your project. Check the box next to the word todo and click Add.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/3-save1.png"><img class="alignnone size-full wp-image-344" title="3-save" src="/wp-content/uploads/2008/09/3-save1.png" alt="" width="431" height="290" /></a></p>
<p style="text-align: left;">Now close Interface Builder.  Next, we are going to add the viewController class and set up variables to interface with this view.</p>
<h2 style="text-align: left;">Create TodoViewController Class Files</h2>
<p>Click <strong>File -&gt; New File&#8230;</strong> Select <strong>UIViewController Subclass</strong> and click Next.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/14addviewcontrollerclass1.png"><img class="size-full wp-image-212 aligncenter" title="14addviewcontrollerclass" src="/wp-content/uploads/2008/08/14addviewcontrollerclass1.png" alt="" width="500" height="368" /></a></p>
<p style="text-align: left;">Name the file <strong>TodoViewController</strong> and make sure that the box that says &#8220;Also create TodoViewController.h&#8221; is checked and click Finish.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/4-viewcontroller1.png"><img class="size-full wp-image-345 aligncenter" title="4-viewcontroller" src="/wp-content/uploads/2008/09/4-viewcontroller1.png" alt="" width="500" height="368" /></a></p>
<p style="text-align: left;">Open up TodoViewController.h and add the following code.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/04-todoviewcontroller1.png"><img class="alignnone size-full wp-image-359" title="04-todoviewcontroller" src="/wp-content/uploads/2008/09/04-todoviewcontroller1.png" alt="" width="430" height="300" /></a></p>
<p style="text-align: left;">Basically, we are setting up Interface Builder Outlets for each of the UI components to be connected to.  Notice, the UIButton has an IBOutlet.  This is because we will need to update the text on the button depending on whether or not the todo is completed.  Also, I have an IBAction called updateStatus.  We will be connecting this to the button we created.  It will toggle the status (pending/complete) of a todo item.  Finally, we see the updatePriority method.  This method will be called when the user selects one of the priority segments in the UISegmentedControl. Next, open up TodoViewController.m and add the following synthesize code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/synthtodoviewcontroller11.png"><img class="alignnone size-full wp-image-364" title="synthtodoviewcontroller1" src="/wp-content/uploads/2008/09/synthtodoviewcontroller11.png" alt="" width="374" height="22" /></a></p>
<p style="text-align: left;">This will allow us to get and set these variables.</p>
<p style="text-align: left;">Before we connect this code to the Interface, we need to implement the methods that will be called when the user presses the button to mark a todo as complete as well as when the user presses a segment in the UISegmentedControl.  Inside of TodoViewController add the following methods.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todoviewcontrollerm1.png"><img class="size-full wp-image-368 aligncenter" title="todoviewcontrollerm" src="/wp-content/uploads/2008/09/todoviewcontrollerm1.png" alt="" width="500" height="229" /></a></p>
<p style="text-align: left;">Let&#8217;s go through this.  First we see the updateStatus method.  This gets called when a user presses the button to alter the status.  We basically check the current status of the todo (whether or not it&#8217;s completed) and depending on that, change the text to be displayed on the UIButton.  So, if the todo is not complete (in progress) and this button is pressed, the text will be changed from &#8220;Mark As Complete&#8221; to &#8220;Mark As In Progress&#8221;.  Finally, we call the updateStatus of the todo and pass the new value (1 or 0) to it.</p>
<p style="text-align: left;">Next we see the updatePriority method.  It simply reads the value of the UISegmentedControl by calling the selectedSegmentIndex method on it.  The next part looks a little messy.   There are 2 reasons that reason we can&#8217;t just pass the value of the UISegmentedControl directly to the method.  The first is, the UISegmentedControl is ordered in acending order (1, 2, 3&#8230;), but our priorities are in descending order (3 = low, 2 = medium, 1 = high).  This is where the &#8220;2 &#8211; priority&#8221; comes from.  Next, UISegmented controls are &#8220;0 indexed&#8221; meaning the indices start at 0 and increment from there.  So we need to add a &#8220;+1&#8243; to the index as our todo priorities start at 1.</p>
<p style="text-align: left;">Now we need to connect the UI Components in Interface Builder to this code.  Double click on TodoViewController.xib to open it in Interface Builder.</p>
<h2 style="text-align: left;">Connecting UI Components To Code</h2>
<p>We first need to associate this view with the class we just created.  In the Interface Builder, click on the File&#8217;s Owner object.  Next click <strong>Tools -&gt; Identity Inspector</strong>.  You should see a drop-down next to class.  Select TodoViewController from this list and you will see the variables we just created appear in the boxes below.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/07-files-owner1.png"><img class="size-full wp-image-349 aligncenter" title="07-files-owner" src="/wp-content/uploads/2008/09/07-files-owner1.png" alt="" width="305" height="287" /></a></p>
<p style="text-align: left;">This is what the Identity window should look like after you have selected TodoViewController.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todoviewcontrollerfilesowner1.png"><img class="alignnone size-full wp-image-380" title="todoviewcontrollerfilesowner" src="/wp-content/uploads/2008/09/todoviewcontrollerfilesowner1.png" alt="" width="287" height="708" /></a></p>
<p style="text-align: left;">Now that the class is associated, we can begin connecting the components.  We will start by connecting the view.  Click on the top of your view window to select the view itself (make sure you haven&#8217;t selected any of the UI components).  Click <strong>Tools -&gt; Connections Inspector</strong>.  Next to where is says &#8220;New Referencing Outlet&#8221; click in the circle and drag it to the &#8220;File&#8217;s Owner&#8221; object and release it.  The word &#8220;view&#8221; should pop up.  Click on the word view.  It should now look like this.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/08-view-connect1.png"><img class="size-full wp-image-350 aligncenter" title="08-view-connect" src="/wp-content/uploads/2008/09/08-view-connect1.png" alt="" width="287" height="708" /></a></p>
<p style="text-align: left;">Now repeat these steps for each of the components (UITextView, UISegmentedControl, UILabel, UIButton) connecting each to the &#8220;File&#8217;s Owner Object&#8221;.   Instead of the word &#8220;view&#8221; popping up, you should see the variable name for the corresponding variable that you want to connect the component to.   So for the UITextView, you should see the word &#8220;todoText&#8221; appear when you drag it to the File&#8217;s Owner object.</p>
<p style="text-align: left;">We need to connect the UIButton to the updateStatus method we created.  To do this click inside the &#8220;Touch up inside&#8221; circle and drag it to the &#8220;File&#8217;s Owner&#8221; object.  You should see the text &#8220;updateStatus&#8221; appear.  Click on it.  If all goes well it should look like this.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/uibutton11.png"><img class="alignnone size-full wp-image-362" title="uibutton1" src="/wp-content/uploads/2008/09/uibutton11.png" alt="" width="287" height="708" /></a></p>
<p style="text-align: left;">The last thing we need to do inside of Interface Builder is connect the UISegmentedControl.  Click on it in your view and then click <strong>Tools -&gt; Connections Inspector&#8230;</strong> Click on the circle next to the &#8220;Value Changed&#8221; method and drag it to the &#8220;File&#8217;s Owner&#8221; object.  You will see the method updatePriority popup.  Go ahead and click on it.  Your window for the UISegmentedControl should now look like this:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/uisegementedcontrol11.png"><img class="alignnone size-full wp-image-366" title="uisegementedcontrol1" src="/wp-content/uploads/2008/09/uisegementedcontrol11.png" alt="" width="287" height="708" /></a></p>
<p style="text-align: left;">Now, let&#8217;s display this view when a row is selected.  Close Interface Builder and open up RootViewController.h and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/11-rootviewcontroller2.png"><img class="size-full wp-image-354 aligncenter" title="11-rootviewcontroller" src="/wp-content/uploads/2008/09/11-rootviewcontroller2.png" alt="" width="352" height="149" /></a></p>
<p style="text-align: left;">We need a variable to associate with the TodoViewController that we will be transitioning to.  Next, open up RootViewController.m and add the following code to synthesize this property.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/12-synth1.png"><img class="size-full wp-image-355 aligncenter" title="12-synth" src="/wp-content/uploads/2008/09/12-synth1.png" alt="" width="133" height="21" /></a></p>
<h2 style="text-align: left;">Keeping the UITableView Up To Date</h2>
<p style="text-align: left;">Whenever a todo item is altered (status or priority) the UITableView needs to be updated with the new changes.  Add the following code to the viewWillAppear.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/rootviewcontrollermwillapprea1.png"><img class="size-full wp-image-372 aligncenter" title="rootviewcontrollermwillapprea" src="/wp-content/uploads/2008/09/rootviewcontrollermwillapprea1.png" alt="" width="246" height="65" /></a></p>
<p style="text-align: left;">The line [self.tableView reloadData] reloads the table data every time the view appears (or reappears).  This will ensure that our table is always up to date.</p>
<p style="text-align: left;">Now add the following code to the didSelectRowAtIndex method:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/didselectrowatindex1.png"><img class="size-full wp-image-373 aligncenter" title="didselectrowatindex" src="/wp-content/uploads/2008/09/didselectrowatindex1.png" alt="" width="500" height="341" /></a></p>
<p style="text-align: left;">This is quite a bulky method with a lot of familiar code.  First, we get a handle to the appDelegate and the todo object that was selected.  Next, we push the todoView (the view you created in interface builder) on to the viewController stack to transition to it.  After that, we are setting some of the properties of the view.  The title is set to the text of the todo (it will get truncated if it is too long) and the UITextView is also set to the todo text.  Next, we are translating our priority to an index for the UISegmentedView.  I explained why this was necessary above.  Then the index of the UISegmentedControl is set by using the setSelectedSegmentIndex method.  Finally, we set the text of the button and label based on the status of the todo.</p>
<p style="text-align: left;">The very last thing we need to do is tell the application to save itself when it closes.  Open up todoAppDelegate.m and add the following code to the applicationWillTerminate method:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/applicationwillterminate1.png"><img class="size-full wp-image-374 aligncenter" title="applicationwillterminate" src="/wp-content/uploads/2008/09/applicationwillterminate1.png" alt="" width="388" height="64" /></a></p>
<p style="text-align: left;">If you ask me, this is some freakin sweet functionality.  The method &#8220;makeObjectsPerformSelector&#8221; is a built in method on an NSArray.  It basically loops over every object in the array, calling the method you pass in to it on each one.  It&#8217;s like doing a for loop and calling the todo[x].dehydrate method for each todo.  Only this is much cleaner.  So, to reiterate, this method will call the dehydrate method on each todo.  If the todo is &#8220;dirty&#8221; meaning it was altered, it will be saved to the database, otherwise the dehydrate method will do nothing.</p>
<p style="text-align: left;">* One thing to note.  The applicationWillTerminate method will not be called if you quit the simulator while the application is running.  To make sure it gets called (and the todo data gets saved) make sure you press the home button on the simulator after you make alterations to the todos.  If you simply press Apple-q and quit the simulator while inside of the todo program, no data will be saved and you will post angry comments on my site telling me that my tutorial is wrong.</p>
<p style="text-align: left;">Click Build and Go and just sit back and enjoy the magic of rock! I mean XCode&#8230;</p>
<p style="text-align: left;">When you select a todo item, your screen should look something like this:</p>
<p style="text-align: center; "><a href="/wp-content/uploads/2008/09/screenshot11.png"><img class="size-full wp-image-390 aligncenter" title="screenshot1" src="/wp-content/uploads/2008/09/screenshot11.png" alt="" width="386" height="742" /></a></p>
<p style="text-align: left;">Well, that concludes part 3 of our series.  Join me next time, when I will show you how to add and delete todos from the SQLite database. If you have any comments or questions, feel free to leave them in the comments section.  I would love to hear them.  Please <a href="http://feeds.feedburner.com/icodeblog">subscribe to the RSS feed</a> if you want to be automatically notified of new tutorials.  If you get lost at any point, you can download the sample code for this tutorial <a href="/wp-content/uploads/2008/09/todo-part-31.zip">here</a>.</p>
<p style="text-align: left;">Happy iCoding!</p>
<p style="text-align: left;">
<p style="text-align: left;">
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2008/09/10/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-3/feed/</wfw:commentRss>
		<slash:comments>68</slash:comments>
		</item>
		<item>
		<title>iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 2</title>
		<link>http://icodeblog.com/2008/09/02/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-2/</link>
		<comments>http://icodeblog.com/2008/09/02/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-2/#comments</comments>
		<pubDate>Tue, 02 Sep 2008 17:35:46 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[iphone programming]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[sqlite3]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=293</guid>
		<description><![CDATA[
This tutorial is part 2 in our series of creating a to-do list.  I will assume that you have completed the following tutorial and its prequisites.

<a href="/2008/08/19/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-1/">iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 1</a>

I will be using the code produced from that tutorial as a base for this one.  When you are finished with this tutorial, your application will look something like this:
<a href="/wp-content/uploads/2008/09/screenshot2.png"></a>
In this section, I will not only teach you how to  ...]]></description>
				<content:encoded><![CDATA[<p><script type="text/javascript"><!--
digg_url = 'http://digg.com/programming/iPhone_Programming_Tutorial_Creating_a_ToDo_List_Using_SQL';
// --></script><script src="http://digg.com/api/diggthis.js"></script></p>
<p>This tutorial is part 2 in our series of creating a to-do list.  I will assume that you have completed the following tutorial and its prequisites.</p>
<ul>
<li><a href="/2008/08/19/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-1/">iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 1</a></li>
</ul>
<p>I will be using the code produced from that tutorial as a base for this one.  When you are finished with this tutorial, your application will look something like this:</p>
<p style="text-align: center; "><a href="/wp-content/uploads/2008/09/screenshot2.png"><img class="alignnone size-full wp-image-327" title="screenshot" src="/wp-content/uploads/2008/09/screenshot2.png" alt="" width="386" height="742" /></a></p>
<p>In this section, I will not only teach you how to display the SQL data in UITablewView, but I will be detailing how to display it in <strong>multiple columns</strong> with images and text.  For this tutorial, you will need to download the following images.</p>
<ul>
<li><a href="/wp-content/uploads/2008/09/red1.png"><img class="alignnone size-full wp-image-304" title="red1" src="/wp-content/uploads/2008/09/red1.png" alt="" /></a></li>
<li><a href="/wp-content/uploads/2008/09/green1.png"><img class="alignnone size-full wp-image-305" title="green1" src="/wp-content/uploads/2008/09/green1.png" alt="" /></a></li>
<li><a href="/wp-content/uploads/2008/09/yellow1.png"><img class="alignnone size-full wp-image-306" title="yellow" src="/wp-content/uploads/2008/09/yellow1.png" alt="" /></a></li>
</ul>
<p>We will be using these images to denote the priority (Green = low, Yellow = medium, Red = high).</p>
<h2>Bring Your Code Up To Speed</h2>
<p>Before we begin, we need to add some code to the Todo.h and Todo.m class to support the priority field in the database.  Open up Todo.h and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todoh2.png"><img class="size-full wp-image-308 aligncenter" title="todoh" src="/wp-content/uploads/2008/09/todoh2.png" alt="" width="378" height="146" /></a></p>
<p style="text-align: left;">All that is new here is the added NSInteger priority property.  We will be using this to get and set the priority for a given todo object.  Next, open Todo.m and add the following code.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todom12.png"><img class="size-full wp-image-310 aligncenter" title="todom1" src="/wp-content/uploads/2008/09/todom12.png" alt="" width="499" height="326" /></a></p>
<p style="text-align: left;">The first line that has changed is the synthesize line.  We added our priority property to allow XCode to create the getter and setter methods for it.  Next, you will notice that the sql statement has changed slightly.  We are now getting the priority in addition to the text from the todo table.  Finally,  we set self.priority property to the selected priority value from the todo table.  This is done by using the sqlite3_column_int method.  We pass the init_statement and the number 1.  1 being the index of the sql array for which the priority data is contained.</p>
<h2>Add Images to Your Project</h2>
<p>Download the images above and save them to your project directory.  Inside of your project, right click (control-click) on the <strong>Resources</strong> folder and click <strong>Add -&gt; Existing Files&#8230;</strong> Browser for the images, select all of them and click <strong>Add</strong>. Check the box that sais &#8220;Copy items into destination group&#8217;s folder (if needed)&#8221;.  Click <strong>Add. </strong>The image files should now appear inside of your <strong>Resources </strong>folder.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/add-images1.png"><img class="size-full wp-image-300 aligncenter" title="add-images" src="/wp-content/uploads/2008/09/add-images1.png" alt="" width="400" height="374" /></a></p>
<h2>Create a UITableViewCell Subclass</h2>
<p>To display data in columns within a UITableView, we have to create our own cell class that defines the type of data we want to display.  By default, Apple provides us with a simple cell object that can only display one column of text.  Normally, this is fine as it will work for a wide variety of applications.  Since we require 3 columns for this tutorial, we need to wrap our own cell object.</p>
<p>Click <strong>File -&gt; New File&#8230;</strong> and select <strong>UITableViewCell</strong>. Click <strong>Next.</strong></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/uitableviewcell1.png"><img class="size-full wp-image-295 aligncenter" title="uitableviewcell" src="/wp-content/uploads/2008/09/uitableviewcell1.png" alt="" width="500" height="368" /></a></p>
<p>Name this file <strong>TodoCell</strong> and make sure this that the box that sais &#8220;Also create TodoCell.h&#8221; is checked.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todocell1.png"><img class="size-full wp-image-296 aligncenter" title="todocell" src="/wp-content/uploads/2008/09/todocell1.png" alt="" width="500" height="368" /></a></p>
<p style="text-align: left;">This will create a &#8220;barebones&#8221; UITableViewCell object with some basic methods already filled out.  Let&#8217;s add some properties to this class.  Open up <strong>TodoCell.h</strong> and add the following code.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todocellh1.png"><img class="alignnone size-full wp-image-313" title="todocellh" src="/wp-content/uploads/2008/09/todocellh1.png" alt="" width="403" height="292" /></a></p>
<p style="text-align: left;">Let&#8217;s take this line by line&#8230;</p>
<p style="text-align: left;">First, we see a Todo object being declared.  Each cell will know which Todo item is associated with it.  This will help out when updating the data in each cell.  Next, we see 2 UILabels and a UIImageView.  To understand why these components are needed, here is a screenshot of how each cell will look.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/datasample1.png"><img class="alignnone size-full wp-image-314" title="datasample" src="/wp-content/uploads/2008/09/datasample1.png" alt="" width="320" height="45" /></a></p>
<p style="text-align: left;">We see the &#8220;Green Dot&#8221; which is an image being rendered by a UIImageView.  The word &#8220;low&#8221; and &#8220;Take out the trash&#8221; are both UILabels. After they are declared, we simply create them as properties.  Notice that we are NOT creating a property for the Todo object. We will not be synthesizing it either. This is because we want this variable to be private.  Setting this variable requires some additional code so we don&#8217;t want any code writer to simply be able to say cell.todo = foo;  You will see why this is so further on in this tutorial.</p>
<p style="text-align: left;">Below this are some method declarations.  First we see the method &#8220;imageForPriority&#8221;.  We will be using this method to decide which image (green, red, yellow) gets displayed for a given priority.  Next, we see the &#8220;getter and setter&#8221; methods for the todo object.  As I explained above, the setter will contain additonal code besides assigning the todo object.</p>
<p style="text-align: left;">Now open up <strong>TodoCell.m</strong>.  We will be writing quite a bit of code in here so I will break it up the best I can.  First, add the following code to create some of the initialization:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todocellm11.png"><img class="alignnone size-full wp-image-316" title="todocellm1" src="/wp-content/uploads/2008/09/todocellm11.png" alt="" width="485" height="344" /></a></p>
<p style="text-align: left;">Ok, some new stuff here.  First, we see 3 static UIImages.  These will hold reference to each of the three images (red, green, yellow).  Since we only need to allocate them once, we make them static.  Static means that they will be associated with the class not the instance.  So we can make as many TodoCells as we want but only 3 UIImages will be created.  On the next line there is a private interface.  This allows us to declare a private method that no one else can use except this class.  Following this is the synthesize line.  Notice again that we are NOT synthesizing the todo object.</p>
<p style="text-align: left;">Looking at the initialize method&#8230;  All that is going on here is we are intanciating each of our UIImages with the correct image for a given priority.  This initialize method will get called once when the first instance of the todocell class is built.  Moving on&#8230; Add the following code: (Note: it might be small and hard to read.  If this is the case, click on the image to open it and the text will be full size)</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todocellm21.png"><img class="size-full wp-image-317 aligncenter" title="todocellm2" src="/wp-content/uploads/2008/09/todocellm21.png" alt="" width="500" height="292" /></a></p>
<p style="text-align: left;">This is the initialiazation method for any UITableViewCell.  First, we need to call the super classe&#8217;s (UITableViewCell) initWithFrame to ensure that the underlying components of the cell get set up properly.  Next, we get a reference to the contentView.  The contentView is the view for each cell.  We will be adding all of our UI components to this view.</p>
<p style="text-align: left;">The next 3 lines initialize a UIImageView and add it to our view.  Notice that we are populating it with the priority1Image.  This will just be a dummy placeholder until we update it.</p>
<p style="text-align: left;">Following this, we initialize the todoTextLabel.  This label will display what it is we need &#8220;to do&#8221; such as &#8220;Take out the trash&#8221;.  There is a method that we will be calling called &#8220;newLabelWithPrimaryColor&#8221;.  This is a method I will detail a little further down.  What it will do is build a new label with the attributes that we specify when we call it.  This method was taken directly from Apple&#8217;s &#8220;Seismic XML&#8221; sample code.  It&#8217;s pretty handy.  After this gets called, we simply add the new label to our view and these steps get repeated for the todoPriorityLabel.</p>
<p style="text-align: left;">Finally, the method &#8220;bringSubviewToFront&#8221; is called on the priority UIImageView.  This method is used in case there is text that gets near the image.  It will cause the image to appear above the text.  <strong>You can use this for layering your UI components.</strong></p>
<p style="text-align: left;">Still with me?  Good&#8230; now let&#8217;s add the following &#8220;getter&#8221; and &#8220;setter&#8221; methods for the todo object.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todocellm31.png"><img class="size-full wp-image-318 aligncenter" title="todocellm3" src="/wp-content/uploads/2008/09/todocellm31.png" alt="" width="492" height="388" /></a></p>
<p style="text-align: left;">The first method todo is simple.  All it does is return our todo object.  The setTodo is a little more involved&#8230;</p>
<p style="text-align: left;">First, we set the incoming (newTodo) to our classe&#8217;s todo object.  Next, we update the UITextLabel so we can display the detailed todo information.  Following this we set the image of our UIImageView by calling the method imageforPriority.  I will detail this method further down in this tutorial but all it does is return an image for a given priority.  Last, we have a switch statement.  The syntax of a switch statement is the same in objective C as it is in most languages.  If you don&#8217;t know what a switch statement is <a href="switch statement" target="_blank">Google it</a>.  Based on the priority of the newTodo, the priority label gets updated with one of three words (High, Medium, Low).  The [self setNeedsDisplay] tells the cell to redisplay itself after this todo has been set.</p>
<p style="text-align: left;">Now, let&#8217;s add the code that lays out the cell.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todocellm41.png"><img class="size-full wp-image-319 aligncenter" title="todocellm4" src="/wp-content/uploads/2008/09/todocellm41.png" alt="" width="500" height="432" /></a></p>
<p style="text-align: left;">This method gets called automatically when a UITableViewCell is being displayed.  It tells the UITableView how to display your cell.  The define statements are similar to define statements in C.  The reason we are coding like this is because we can tweak these variables to get the display to our liking.  First, we call the layoutSubviews of the super class.  Next, we get a reference to the contentView.bounds.  This variable will allow us to figure out how much drawing area we have and allow us to line objects up properly.</p>
<p style="text-align: left;">The if(!self.editing) part is not neccessary but is good practice.  You would use this if you allowed editing of your cells.  This code is a little tough to explain by typing, but I will do the best that I can.  First, we declare our right-most column.  This is done by making a frame to hold the content.  This column will hold the text of the todo item.  Most of the code here is just positioning.  You can play with these numbers and see how it moves stuff around.  Once all of the positioning code is completed, the frame of our todoTextLabel gets set to this newly created frame.  This is done for each of our UI components.  You can lay them out however you like, as I may not have the best layout.</p>
<p>We have one more method to override.  It&#8217;s the setSelected method.  Go ahead and add the following code.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todocellm51.png"><img class="size-full wp-image-321 aligncenter" title="todocellm5" src="/wp-content/uploads/2008/09/todocellm51.png" alt="" width="380" height="276" /></a></p>
<p style="text-align: left;">This method gets called when the user taps on a given cell.  We need to tell the cell how to behave when it gets tapped on.  This method should look pretty straight forward.  First, we call the setSelected method of the super class.  Next, we update the background color depending on whether or not the cell was selected.  Finally, the labels get set to a white color if the cell gets selected.  This is to contrast the blue color that the background becomes when the cell is selected.</p>
<p style="text-align: left;">This last 2 methods that I want to talk about are the helper methods that we used earlier in the code.  Add the following methods to your code.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/todocellm61.png"><img class="size-full wp-image-322 aligncenter" title="todocellm6" src="/wp-content/uploads/2008/09/todocellm61.png" alt="" width="500" height="402" /></a></p>
<p style="text-align: left;"><strong>newLabelWithPrimaryColor</strong></p>
<p>This method got called when we were initializing our UILabels.  It takes a few parameters that should be pretty self explanatory.  Looking through the code, we first see the font being initialized with the size that we specified.  If bold was specified this is also accounted for.  Next, we instantiate a new UILabel and give it some properties.  Finally, this newly created UILabel gets returned.</p>
<p><strong><span style="color: #888888;">imageForPriority</span></strong></p>
<p>This method is actually quite simple.  It simply takes a priority and returns the UIImage that is associated with that priority.  Notice the default clause.  I decided to handle it like this instead of doing &#8220;case 1&#8243; to handle all other cases.  For whatever reason, if there is ever a priority that is not 1,2 or 3 it will, by default, have low priority.</p>
<p>Now that we have created our UITableViewCell, we need to display it in the table.  Open up RootViewController.m and add the following import statement.  This will allow us to use our TodoCell object.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/rootviewcontrollerm11.png"><img class="size-full wp-image-323 aligncenter" title="rootviewcontrollerm1" src="/wp-content/uploads/2008/09/rootviewcontrollerm11.png" alt="" width="130" height="18" /></a></p>
<p style="text-align: left;">Now find the numberOfRowsInSection method and add the following code</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/rootviewcontrollerm21.png"><img class="size-full wp-image-324 aligncenter" title="rootviewcontrollerm2" src="/wp-content/uploads/2008/09/rootviewcontrollerm21.png" alt="" width="500" height="52" /></a></p>
<p style="text-align: left;">I&#8217;m not going to really go over this, as this is almost the exact same code as in the Fruits example.  Basically, we are returning the number of todo items.</p>
<p>Now for the magic&#8230;We will now add our TodoCell to allow it to be displayed.  Find the cellForRowAtIndexPath method and add the following code.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/rootviewcontrollerm31.png"><img class="size-full wp-image-325 aligncenter" title="rootviewcontrollerm3" src="/wp-content/uploads/2008/09/rootviewcontrollerm31.png" alt="" width="500" height="194" /></a></p>
<p style="text-align: left;">This code is fairly similar to the default code that Apple has provided us.  The first change is we are instantiating our TodoCell object.  We are creating it with the initWithFrame method and passing our identifier to it.  Next, we get reference to the application&#8217;s appDelegate and use it to look up the todo item at the given index.  This should be familiar.  Finally, we set the todo item of the cell to the todo item at the row index and return the cell.  That&#8217;s it! Go ahead and click the Build and Go icon and see your todo list come to life.  Here is a screenshot of what your app should look like.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/screenshot2.png"><img class="alignnone size-full wp-image-327" title="screenshot" src="/wp-content/uploads/2008/09/screenshot2.png" alt="" width="386" height="742" /></a></p>
<p style="text-align: left;">That concludes part 2 of this tutorial. Join me next time as I show you how to display detailed todo info using some new UI controls that we haven&#8217;t seen yet. As always, post your questions and comments in the comments section of the blog. <a href="/wp-content/uploads/2008/09/todo-part-211.zip">Download The Sample Code</a></p>
<p>Happy iCoding!</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2008/09/02/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-2/feed/</wfw:commentRss>
		<slash:comments>111</slash:comments>
		</item>
		<item>
		<title>iPhone Programming Tutorial &#8211; Populating UITableView With An NSArray</title>
		<link>http://icodeblog.com/2008/08/08/iphone-programming-tutorial-populating-uitableview-with-an-nsarray/</link>
		<comments>http://icodeblog.com/2008/08/08/iphone-programming-tutorial-populating-uitableview-with-an-nsarray/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 18:29:44 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[iphone programming]]></category>
		<category><![CDATA[nsarray]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[uitableview]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=196</guid>
		<description><![CDATA[The goal of this tutorial is to show you how to populate a UITableView with data from an array of objects.  This will be the building block to display XML data as well as SQL data.
The theme of this application will be fruit.  We will create an array of &#8220;fruit&#8221; objects that have some additional information besides the name.  We will populate a UITableView with the names of the fruits.  When the user selects a fruit  ...]]></description>
				<content:encoded><![CDATA[<p style="text-align: left;"><script type="text/javascript"><!--
digg_url = 'http://digg.com/programming/iPhone_Programming_Tutorial_Populate_UITableView_With_Array';
// --></script><script src="http://digg.com/api/diggthis.js"></script>The goal of this tutorial is to show you how to populate a UITableView with data from an array of objects.  This will be the building block to display XML data as well as SQL data.</p>
<p style="text-align: left;">The theme of this application will be fruit.  We will create an array of &#8220;fruit&#8221; objects that have some additional information besides the name.  We will populate a UITableView with the names of the fruits.  When the user selects a fruit from the list, the view will transition to another one and display specific details about that fruit.</p>
<p style="text-align: left;">I will try to be as detailed as possible however it would be useful if you have completed the following tutorials as I will use concepts from each of them:</p>
<ul style="text-align: left;">
<li><a href="/2008/07/26/iphone-programming-tutorial-hello-world/">UITableView Hello World</a></li>
<li><a href="/2008/07/30/iphone-programming-tutorial-connecting-code-to-an-interface-builder-view/">Connecting Code To An Interface</a></li>
<li><a href="/2008/08/03/iphone-programming-tutorial-transitioning-between-views/">Transitioning Between Views</a></li>
</ul>
<div style="text-align: left;">In this tutorial you will learn:</div>
<div style="text-align: left;">
<ul>
<li><a href="#nav-based">Create a Navigation-Based Application</a></li>
<li><a href="#fruit-object">Create a New Fruit Class Object</a></li>
<li><a href="#nsarray">Create and Populate an NSArray</a></li>
<li><a href="#add-view">Add a New View To Your Project</a></li>
<li><a href="#connect-code">Connect The View To Code</a></li>
<li><a href="#populate-uitableview">Populate A UITableView With Object Data</a></li>
<li style="text-align: left;"><a href="#transition">Transition To New Views And Show Data Based On The Row Selected</a></li>
</ul>
<h2 style="text-align: left;"><a name="nav-based"></a></h2>
<div>Open up X-Code and Select <strong>File-&gt;New Project&#8230; </strong>Select <strong>Navigation-Based Application</strong> and click <strong>Choose&#8230;<br />
</strong></div>
<div style="text-align: center;"><a href="/wp-content/uploads/2008/08/01navigationbased1.png"><img class="alignnone size-full wp-image-198" title="01navigationbased" src="/wp-content/uploads/2008/08/01navigationbased1.png" alt="" width="500" height="368" /></a></div>
<div style="text-align: left;">Name your project <strong>Fruit.</strong></div>
<h2 style="text-align: left;"><a name="fruit-object"></a></h2>
<p style="text-align: left;">We are going to create our &#8220;fruit&#8221; objects that will be used in the application.  If you are not too familiar with object oriented programming&#8230;<a href="http://www.google.com/search?q=object+oriented+programming&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=org.mozilla:en-US:official&amp;client=firefox-a" target="_blank">Google it</a>.  That tutorial would be a little too huge for me to write.</p>
<p style="text-align: left;">Click on <strong>File -&gt; New File&#8230;</strong> The object we are creating will inherit from NSObject, so select <strong>NSObject Subclass</strong> and click <strong>Next.</strong></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/02newobject1.png"><img class="size-full wp-image-200 aligncenter" title="02newobject" src="/wp-content/uploads/2008/08/02newobject1.png" alt="" width="500" height="368" /></a></p>
<p style="text-align: left;">The next screen will ask you to name it. Go ahead and name it &#8220;Fruit&#8221; and make sure that &#8220;Also create Fruit.h&#8221; is checked.  It should look like the screen below.  Then, click <strong>Finish.</strong></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/03newobjecttoproject1.png"><img class="size-full wp-image-201 aligncenter" title="03newobjecttoproject" src="/wp-content/uploads/2008/08/03newobjecttoproject1.png" alt="" width="500" height="368" /></a></p>
<p style="text-align: left;">Now, we are going to define the properties of a &#8220;Fruit&#8221; object.  For this application a fruit will have a <strong>name </strong>and a <strong>description.</strong> Open Fruit.h and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/04fruith1.png"><img class="size-full wp-image-202 aligncenter" title="04fruith" src="/wp-content/uploads/2008/08/04fruith1.png" alt="" width="377" height="206" /></a></p>
<p style="text-align: left;">We have created the properties needed to represent our fruit.  There is one line that you may be unfamiliar with.  The line -(id)initWithName:(NSString*)n description:(NSString *)desc; is a definition for a function.  This function will be called to initialize a Fruit object.  All NSObjects have an <strong>init</strong> method, but we want to create our own so that we can pass in a name and description when the object is created.</p>
<p style="text-align: left;">Open up <strong>Fruit.m</strong> and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/05fruitm1.png"><img class="size-full wp-image-203 aligncenter" title="05fruitm" src="/wp-content/uploads/2008/08/05fruitm1.png" alt="" width="385" height="182" /></a></p>
<p style="text-align: left;">Here we implement the initWithName method.  The code here seems pretty straight forward.  We are basically setting our local copies of name and description to the arguments passed to this method.  The important thing to notice here is the return self line.  This is crucial for using this method as a constructor.  It allows this function to return the newly created instance of a fruit object.</p>
<div style="text-align: left;">Next, we are going to set the title of our main view. This is necessary to create a back button when transitioning between views. Open up <strong>RootViewController.m</strong>&#8230;In the<strong> viewDidLoad</strong> method, add the following code:</div>
<div style="text-align: center;"><a href="/wp-content/uploads/2008/08/01otitle1.png"><img class="alignnone size-full wp-image-199" title="01otitle" src="/wp-content/uploads/2008/08/01otitle1.png" alt="" width="416" height="192" /><br />
</a></div>
<div style="text-align: left;">We are setting the <strong>title</strong> property of the RootViewController object to the string &#8220;Fruits&#8221;.  Also, be sure to add the #import &#8220;Fruit.h&#8221; line at the top to include the fruit object in our project as well as @synthesize fruitView to add the &#8220;getter&#8221; and &#8220;setter&#8221; methods.</div>
<h2><a name="nsarray"></a></h2>
<p>Next, we are going to create an array of fruit objects.  Open up <strong>FruitAppDelegate.h</strong> and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/06fruitappdelegateh1.png"><img class="size-full wp-image-204 aligncenter" title="06fruitappdelegateh" src="/wp-content/uploads/2008/08/06fruitappdelegateh1.png" alt="" width="461" height="218" /></a></p>
<p style="text-align: left;">All we are really adding here is an NSMutableArray property.  I used NSMutableArray instead of NSArray because it has a few more methods making it more flexible.</p>
<p style="text-align: left;">Now, open up <strong>FruitAppDelegate.m</strong> and add @synthesize fruits to the top.  This is so other objects will have access to the fruits array.  Also, be sure to include the import statement for <strong>Fruit.h</strong>.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/07synthfruits1.png"><img class="size-full wp-image-205 aligncenter" title="07synthfruits" src="/wp-content/uploads/2008/08/07synthfruits1.png" alt="" width="209" height="132" /></a></p>
<p style="text-align: left;">Now add the following code to the <strong>applicationDidFinishLaunching</strong> method.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/08initfruits1.png"><img class="size-full wp-image-206 aligncenter" title="08initfruits" src="/wp-content/uploads/2008/08/08initfruits1.png" alt="" width="486" height="111" /></a></p>
<p style="text-align: left;">What we are doing here in the 1st three lines is creating new instances of a fruit object.  Notice that instead of calling <strong>init</strong>, we are calling the <strong>initWithName</strong> method that we created.  This is allowing us to pass in a name and a description to each of the fruits.</p>
<p style="text-align: left;">The next line <strong>[self.fruits = [[NSMutableArray alloc] initWithObjects:apple,orange,watermelon,nil]; </strong>builds a new array from the objects we just created.  It is important to pass in nil as the last argument in an NSMutableArray.  It will not work unless you remember this.</p>
<h2><a name="add-view"></a></h2>
<p>Now we are going to create the view that will be displayed when the user selects a fruit.  Double click on any one of your <strong>.xib</strong> files to open up <strong>Interface Builder.</strong></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/09interfacebuilder1.png"><img class="alignnone size-full wp-image-207" title="09interfacebuilder" src="/wp-content/uploads/2008/08/09interfacebuilder1.png" alt="" width="500" height="312" /></a></p>
<p style="text-align: left;">Click <strong>File -&gt; New </strong>and select <strong>view</strong> and click choose.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/10addview1.png"><img class="size-full wp-image-208 aligncenter" title="10addview" src="/wp-content/uploads/2008/08/10addview1.png" alt="" width="500" height="376" /></a></p>
<p style="text-align: left;">You should now see the new view (it will be blank) and the objects associated with it.  We need some sort of text field to display the description of the fruit.  For this tutorial I have chosen a <strong>UITextView </strong>as opposed to a <strong>UITextField</strong>.  This is because a <strong>UITextView</strong> is multi-line and is great for displaying more than one line of text.  So, go ahead and drag it on to your <strong>view.</strong> Your <strong>view</strong> window should now look like this:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/11uitextview1.png"><img class="size-full wp-image-209 aligncenter" title="11uitextview" src="/wp-content/uploads/2008/08/11uitextview1.png" alt="" width="320" height="502" /></a></p>
<p style="text-align: left;">Now click <strong>File -&gt; Save</strong>. One important thing to keep in mind is to make sure you are saving it in the current project&#8217;s directory.  It seems that every time I add a view to a project, the default directory is not my project&#8217;s directory.  Name this file <strong>FruitViewController</strong> and click <strong>Save.</strong></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/12saveview1.png"><img class="size-full wp-image-210 aligncenter" title="12saveview" src="/wp-content/uploads/2008/08/12saveview1.png" alt="" width="500" height="389" /></a></p>
<p style="text-align: left;">Another window should show up after you click save.  It is asking you if you want to add this view to your project.  Just check the box net to <strong>Fruit</strong> ,click <strong>Add</strong>, and close <strong>Interface Builder.</strong></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/13addviewtoproject1.png"><img class="size-full wp-image-211 aligncenter" title="13addviewtoproject" src="/wp-content/uploads/2008/08/13addviewtoproject1.png" alt="" width="431" height="290" /></a></p>
<p style="text-align: left;">Close Interface Builder and go back to X-Code.</p>
<h2><a name="connect-code"></a></h2>
<p>We need to create a ViewController to handle our <strong>View.</strong> Click <strong>File -&gt; New File&#8230;</strong> Select <strong>UIViewController subclass</strong> and click <strong>Next.</strong></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/14addviewcontrollerclass1.png"><img class="size-full wp-image-212 aligncenter" title="14addviewcontrollerclass" src="/wp-content/uploads/2008/08/14addviewcontrollerclass1.png" alt="" width="500" height="368" /></a></p>
<p style="text-align: left;">Name this file <strong>FruitViewController.m</strong> and check the box that says &#8220;Also create &#8220;FruitViewController.h&#8221; . Click <strong>Finish.</strong></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/15addtoproject1.png"><img class="size-full wp-image-214 aligncenter" title="15addtoproject" src="/wp-content/uploads/2008/08/15addtoproject1.png" alt="" width="500" height="368" /></a></p>
<p style="text-align: left;">Now we will declare an<strong> Interface Builder Outlet </strong>for the <strong>UITextView</strong> that we added to the view.  Open up <strong>FruitViewController.h</strong> and add the following code.<br />
<a href="/wp-content/uploads/2008/08/14afruitviewcontroller1.png"></a></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/14afruitviewcontroller1.png"><img class="size-full wp-image-213 aligncenter" title="14afruitviewcontroller" src="/wp-content/uploads/2008/08/14afruitviewcontroller1.png" alt="" width="407" height="150" /></a></p>
<p style="text-align: left;">This line allows us to associate the fruitDescription property with the UITextView we created.  Open up <strong>FruitViewController.m</strong> and add this code underneath @implementation keyword. This creates default &#8220;getter&#8221; and &#8220;setter&#8221; methods for the fruitDescription property.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/20fruitviewcontrollerm1.png"><img class="alignnone size-full wp-image-224" title="20fruitviewcontrollerm" src="/wp-content/uploads/2008/08/20fruitviewcontrollerm1.png" alt="" width="183" height="18" /></a></p>
<p style="text-align: left;">Double click on <strong>FruitViewController.xib </strong>to open it in interface builder.  We need to connect the view to the FruitViewController class we just created.  Click on the <strong>File&#8217;s Owner</strong> object.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/16clickfilesowner1.png"><img class="size-full wp-image-215 aligncenter" title="16clickfilesowner" src="/wp-content/uploads/2008/08/16clickfilesowner1.png" alt="" width="305" height="287" /></a></p>
<p style="text-align: left;">Click <strong>Tools -&gt; Identity Inspector.</strong> Select <strong>FruitViewController</strong> from the dropdown next to <strong>class. </strong>Notice that under the <strong>Class Outlets </strong>section you will see the <strong>UITextView</strong> property we created.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/17setclass1.png"><img class="size-full wp-image-216 aligncenter" title="17setclass" src="/wp-content/uploads/2008/08/17setclass1.png" alt="" width="287" height="708" /></a></p>
<p style="text-align: left;">The last step in interface builder is to connect the <strong>UITextView</strong>. Click <strong>Tools -&gt; Connection Inspector</strong>.  You should see an <strong>Outlet </strong>that says <strong>fruitDescription</strong>. Click in the corresponding circle and drag it over the <strong>UITextView</strong> on your view and release it.</p>
<p style="text-align: left;">Now, click the circle next the word <strong>View</strong> under outlets and drag it to the <strong>View </strong>object inside of the window that says <strong>FruitViewController</strong> in the title.  When you are done the screen should look like this:</p>
<p><a href="/wp-content/uploads/2008/08/18makeconnections1.png"><img class="size-full wp-image-223 alignnone" title="18makeconnections" src="/wp-content/uploads/2008/08/18makeconnections1.png" alt="" width="287" height="708" /></a></p>
<p style="text-align: left;">Now close <strong>Interface Builder.</strong></p>
<h2><a name="populate-uitableview"></a></h2>
<p>The first thing we are going to do here is create a property for our new view so that it can be transitioned to when a fruit gets clicked on.  Open <strong>RootViewController.h</strong> and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/21rootviewcontrollerh1.png"><img class="size-full wp-image-225 aligncenter" title="21rootviewcontrollerh" src="/wp-content/uploads/2008/08/21rootviewcontrollerh1.png" alt="" width="368" height="151" /></a></p>
<p style="text-align: left;">We are just creating a property for the <strong>fruitViewController</strong> that we added to the project.  Also note that I added the #import &#8220;<strong>FruitViewController.h</strong>&#8221; line.  this will allow us to create new instances of the <strong>FruitViewController</strong> object.</p>
<p style="text-align: left;">Now open the <strong>RootViewController.m</strong> and find the <strong>numberOfRowsInSection</strong> method.  This method tells the <strong>UITableView</strong> how many rows it will be displaying.  In our case it will be the size of the array of fruits.  Add the following code (click the image to enlarge):</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/22numberofrows1.png"><img class="size-full wp-image-226 aligncenter" title="22numberofrows" src="/wp-content/uploads/2008/08/22numberofrows1.png" alt="" width="499" height="58" /></a></p>
<p style="text-align: left;">The first line allows us to gain access to the appDelegate of our application.  This is where we defined the fruit array.  Once we have access to the delegate the <strong>count</strong> property of the fruit gets returned.</p>
<p style="text-align: left;">Now find the <strong>cellForRowAtIndexPath </strong>method and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/23cellforrowatindex11.png"><img class="size-full wp-image-250 aligncenter" title="23cellforrowatindex1" src="/wp-content/uploads/2008/08/23cellforrowatindex11.png" alt="" width="500" height="208" /></a></p>
<p style="text-align: left;">So the first line we added is the &#8220;FruitAppDelegate *appDelegate&#8230;&#8221; line.  Again, this is giving us access to the appDelegate object where we declared the fruit array.  The next line calls the <strong>objectAtIndex </strong>method on the <strong>Array</strong> of fruits.  The index we will be using can be accessed via <strong>indexPath.row</strong>.  This is an integer value representing each row of the <strong>UITableView</strong>.  Finally, we call the <strong>setText </strong>method of the cell, to display the name of the fruit in each cell at the given index.</p>
<p style="text-align: left;">
<h2><a name="transition"></a></h2>
<p>This is the last step.  We are going to detect which row in the <strong>UITableView</strong> the user selected.  Find the method called <strong>didSelectRow</strong> and add the following code(click image to enlarge).</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/24didselectrow1.png"><img class="size-full wp-image-228 aligncenter" title="24didselectrow" src="/wp-content/uploads/2008/08/24didselectrow1.png" alt="" width="587" height="160" /></a></p>
<p style="text-align: left;">This method gets called every time a user taps on a cell in the <strong>UITableView</strong>.  The parameter <strong>indexPath </strong>has a property called <strong>row</strong> that is the integer value of the cell the user clicked on.  To access this, we call <strong>indexPath.row</strong>.</p>
<p style="text-align: left;">The first line again gives us access to the appDelegate.  The next line indexes into the fruits array and makes a copy of the selected fruit object.</p>
<p style="text-align: left;">The next section starting with &#8220;if(self.fruitView == nil)&#8221;, initializes the viewController if it hasn&#8217;t already been initialized (see my previous tutorial if you need more of an explanation on this).  One thing to take note of: Make sure that the parameter you pass to <strong>initWithNibName</strong> matches the name of the <strong>.xib </strong>file you used for your view.  So in our case, its <strong>FruitViewController.</strong></p>
<p style="text-align: left;">Following this line is the line that pushes the viewController on to the navigationController stack.  This causes the view to transition to the new view.</p>
<p style="text-align: left;">The last 2 lines pass the fruit information to the new view.  They are fairly self explanitory.  We first set the title of the view to the name of the fruit and then set the description text to the description of the fruit.</p>
<p style="text-align: left;">Now click <strong>Build and Go</strong> and your app should launch.  Here are some screenshots of how it <em>should<strong> </strong>look.</em></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/25screenshot11.png"><img class="size-full wp-image-229 aligncenter" title="25screenshot1" src="/wp-content/uploads/2008/08/25screenshot11.png" alt="" width="386" height="742" /></a></p>
<p style="text-align: left;">And after clicking on a fruit&#8230;</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/26screenshot21.png"><img class="size-full wp-image-230 aligncenter" title="26screenshot2" src="/wp-content/uploads/2008/08/26screenshot21.png" alt="" width="386" height="742" /></a></p>
<p style="text-align: left;">Well, I hope that you got a lot out of this tutorial.  As always, if you have any questions or comments, feel free to leave them in the comments section of this post.  We also have a <a href="/forum">forum</a> to help you will all of you iphone related questions. If you get lost, you can download the sample code <a href="/wp-content/uploads/2008/08/fruit11.zip">here</a><a href="/wp-content/uploads/2008/08/fruit2.zip"></a></p>
<p style="text-align: left;">Happy iCoding!</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2008/08/08/iphone-programming-tutorial-populating-uitableview-with-an-nsarray/feed/</wfw:commentRss>
		<slash:comments>327</slash:comments>
		</item>
		<item>
		<title>iPhone Programming Tutorial &#8211; Transitioning Between Views</title>
		<link>http://icodeblog.com/2008/08/03/iphone-programming-tutorial-transitioning-between-views/</link>
		<comments>http://icodeblog.com/2008/08/03/iphone-programming-tutorial-transitioning-between-views/#comments</comments>
		<pubDate>Sun, 03 Aug 2008 22:41:33 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[iphone programming]]></category>
		<category><![CDATA[iphone sdk]]></category>
		<category><![CDATA[transition]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[uinavigation]]></category>
		<category><![CDATA[uitableview]]></category>
		<category><![CDATA[view]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=138</guid>
		<description><![CDATA[
This tutorial will focus on transitioning from one view to another.  We will be utilizing Apple&#8217;s UINavigationController.  I will be using the code from the &#8220;<a title="iPhone SDK Hello World" href="/2008/07/26/iphone-programming-tutorial-hello-world/">Hello World</a>&#8221; tutorial that I previously wrote.  So if you have not completed it yet, go ahead and do it and come back to this page. (It&#8217;s quick I promise).  You can view it <a title="UITableView Hello World" href="/2008/07/26/iphone-programming-tutorial-hello-world/">here</a>.
In this tutorial you will learn:

<a href="#add-a-new-view">Add A  ...]]></description>
				<content:encoded><![CDATA[<p><script type="text/javascript"><!--
digg_url = 'http://digg.com/apple/iPhone_Programming_Tutorial_Transitioning_Between_Views';
// --></script><script src="http://digg.com/api/diggthis.js"></script></p>
<p>This tutorial will focus on transitioning from one view to another.  We will be utilizing Apple&#8217;s UINavigationController.  I will be using the code from the &#8220;<a title="iPhone SDK Hello World" href="/2008/07/26/iphone-programming-tutorial-hello-world/">Hello World</a>&#8221; tutorial that I previously wrote.  So if you have not completed it yet, go ahead and do it and come back to this page. (It&#8217;s quick I promise).  You can view it <a title="UITableView Hello World" href="/2008/07/26/iphone-programming-tutorial-hello-world/">here</a>.</p>
<p>In this tutorial you will learn:</p>
<ul>
<li><a href="#add-a-new-view">Add A New View</a></li>
<li><a href="#add-a-view-controller">Add A View Controller</a></li>
<li><a href="#transition">Set Up The Transition To The View</a></li>
<li><a href="#connect-to-code">Connect The View To The Code</a></li>
<li><a href="#back-button">Add A Back Button</a></li>
</ul>
<p>The first thing we are going to do is change our &#8220;Hello World&#8221; text to say something that sounds more like navigation.  Go ahead and open <strong>RootViewController.m</strong>.  Location the <strong>cellForRowAtIndexPath</strong> method (it&#8217;s the one that you edited to display &#8220;Hello World&#8221; in the table cell.</p>
<p>Change the line: <strong>[cell setText:@&#8221;Hello World&#8221;] ; </strong>to <strong>[cell setText:@&#8221;Next View&#8221;];</strong></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/01-nextview1.png"><img class="alignnone size-full wp-image-140" title="01-nextview" src="/wp-content/uploads/2008/08/01-nextview1.png" alt="" width="205" height="35" /></a></p>
<h2><a name="add-a-new-view">Add A New View</a></h2>
<p>We will now add the view that we will be transitioning to.  Click on <strong>RootViewController.xib</strong> and this should open up <strong>Interface Builder.</strong> We don&#8217;t actually need to edit this file.  Once inside <strong>Interface Builder</strong> click on <strong>File -&gt; New </strong>and select <strong>View.</strong></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/02addnewview1.png"><img class="size-full wp-image-141 aligncenter" title="02addnewview" src="/wp-content/uploads/2008/08/02addnewview1.png" alt="" width="500" height="376" /></a></p>
<p style="text-align: left;">It will add a blank <strong>View </strong>to your project.  For now, we will keep it simple.  Go ahead and drag a new <strong>Label</strong> on to the <strong>View</strong>. Double click on the label and change the text to whatever you want.  I set mine to <strong>View 2</strong> (I know, not very imaginative).</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/035view1.png"><img class="size-full wp-image-155 aligncenter" title="035view" src="/wp-content/uploads/2008/08/035view1.png" alt="" width="320" height="502" /></a></p>
<p style="text-align: left;">Let&#8217;s save the view.  Click <strong>File -&gt; Save. </strong>Call it <strong>View2. </strong>Make sure that you save it inside your <strong>Hello World</strong> project&#8217;s directory.  It may want to save it somewhere else by default.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/03saveview1.png"><img class="size-full wp-image-142 aligncenter" title="03saveview" src="/wp-content/uploads/2008/08/03saveview1.png" alt="" width="500" height="389" /></a></p>
<p style="text-align: left;">Next, you will see a screen asking you if you want to add the <strong>View </strong>to your project.  Check the box next to <strong>Hello World</strong> and click <strong>Add</strong>.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/04addviewtoproject1.png"><img class="size-full wp-image-143 aligncenter" title="04addviewtoproject" src="/wp-content/uploads/2008/08/04addviewtoproject1.png" alt="" width="431" height="290" /></a></p>
<p style="text-align: left;">Close <strong>Interface Builder</strong>.  Drag the <strong>View2.xib</strong> file into the <strong>Resources</strong> folder, if it didn&#8217;t appear there by default (this will help maintain organization).</p>
<h2><a name="add-a-view-controller">Add A View Controller</a></h2>
<p style="text-align: left;">Now we need to create a <strong>ViewController</strong> class.  This class will be used to connect the <strong>view</strong> that we just created to our code.  Inside of <strong>Xcode</strong> click <strong>File -&gt; New File&#8230;</strong> Select <strong>UIViewController subclass </strong>and click <strong>Next.</strong></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/05addviewcontroller1.png"><img class="size-full wp-image-144 aligncenter" title="05addviewcontroller" src="/wp-content/uploads/2008/08/05addviewcontroller1.png" alt="" width="500" height="368" /></a></p>
<p style="text-align: left;">Name it <strong>View2ViewController</strong> and make sure &#8220;<strong>Also create &#8220;View2ViewController.h&#8221; &#8220;</strong> is checked.  Click <strong>Finish</strong> to continue.  This will add the new <strong>ViewController</strong> to your project.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/06nameviewcontroller1.png"><img class="size-full wp-image-145 aligncenter" title="06nameviewcontroller" src="/wp-content/uploads/2008/08/06nameviewcontroller1.png" alt="" width="500" height="368" /></a></p>
<p style="text-align: left;">For organization sake, drag your <strong>View2ViewController.h </strong>and <strong>.m</strong> files into the <strong>Classes</strong> folder if they didn&#8217;t appear there to begin with.</p>
<h2><a name="transition">Set Up The Transition To The New View</a></h2>
<p>Open up <strong>RootViewController.h</strong> and add the following code:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/07viewcontrollerh1.png"><img class="size-full wp-image-146 aligncenter" title="07viewcontrollerh" src="/wp-content/uploads/2008/08/07viewcontrollerh1.png" alt="" width="423" height="152" /></a></p>
<p style="text-align: left;">This code should be pretty self explanatory, but I will explain it anyway.  The import statement <strong>#import &#8220;View2ViewController.h&#8221;</strong> gets the header file of the <strong>ViewController </strong>that we created and allows us to create new instances of it.</p>
<p style="text-align: left;">Next, we declare a variable called <strong>view2ViewController</strong> which is of the type <strong>View2ViewController.</strong> One thing that I want to point out is the first part starts with a capitol &#8220;<strong>V</strong>&#8221; which is the type of object that we are declaring.  The second part starts with a lower case &#8220;<strong>v</strong>&#8220;.  This is our variable name that we will use to reference our <strong>ViewController </strong>object. Finally, we make our variable a property to set additional information.</p>
<p style="text-align: left;">Now, open up <strong>RootViewController.m</strong> and add the following code underneath <strong>@implementation RootViewController. </strong>This creates default &#8220;getter&#8221; and &#8220;setter&#8221; methods for our <strong>view2ViewController</strong> property.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/07viewcontroller11.png"><img class="size-full wp-image-159 aligncenter" title="07viewcontroller1" src="/wp-content/uploads/2008/08/07viewcontroller11.png" alt="" width="202" height="28" /></a></p>
<p style="text-align: left;">
<p style="text-align: left;">Next find the function <strong>didSelectRowAtIndexPath</strong>.  It may be commented out.  If it is, go ahead and uncomment it.  This method gets called (automatically) every time a table cell gets tapped.  Notice that it takes an <strong>indexPath.</strong> This will be useful later on when I show you how to populate a <strong>UITableView</strong> with an <strong>NSArray.</strong> We will ignore it for now.</p>
<p style="text-align: left;">Add the following code to the method.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/08viewcontrollerm1.png"><img class="alignnone size-full wp-image-147" title="08viewcontrollerm" src="/wp-content/uploads/2008/08/08viewcontrollerm1.png" alt="" width="671" height="137" /></a></p>
<p style="text-align: left;">First we check to see if <strong>self.view2ViewController</strong> is null.  This will happen the first time the user presses on the table cell.  After this, the <strong>viewController</strong> gets stored in memory to optimize performance.  Next we create a new instance of a <strong>View2ViewController </strong>and set it to our view2ViewController<strong>. </strong>Remember to pay attention to a capitol &#8220;V&#8221; verses a lowercase &#8220;v&#8221;.  After we set this viewController to our viewController, it should be released.  Remember, objective-c does not have a garbage collector, so we need to clear all of our unused objects from memory.</p>
<p style="text-align: left;">Finally, the last line of code is what actually transitions our view to the newly created view.  The <strong>navigationController </strong>object is a stack that contains <strong>viewControllers.</strong> The view at the top of the stack is the one that gets rendered.  So all we are doing is pushing a viewController onto this stack.  There last part <strong>animated:YES</strong>, tells the compiler that we want an animated transition to the next view.</p>
<h2><a name="connect-to-code">Connect The View To Code</a></h2>
<p>Before this code will execute, we must connect the code that we just wrote to the view we just created.  Double click on <strong>View2.xib</strong> to open it up in <strong>Interface Builder</strong>. We need to associate our <strong>View2ViewController </strong>class object with the view so click on the <strong>File&#8217;s Owner</strong> object and then click <strong>Tools -&gt; Identity Inspector</strong>.  <strong></strong></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/09selectfilesowner1.png"><img class="size-full wp-image-148 aligncenter" title="09selectfilesowner" src="/wp-content/uploads/2008/08/09selectfilesowner1.png" alt="" width="305" height="287" /></a></p>
<p style="text-align: left;">Click the dropdown next to <strong>class</strong> and select <strong>View2ViewController.</strong></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/10setclass1.png"><img class="size-full wp-image-149 aligncenter" title="10setclass" src="/wp-content/uploads/2008/08/10setclass1.png" alt="" width="287" height="708" /></a></p>
<p style="text-align: left;">Next click anywhere on your view to select it.  Click <strong>Tools -&gt; Connections Inspector. </strong>Click in the circle next to <strong>New Referencing Outlet</strong>, drag it to the <strong>File&#8217;s Owner</strong> object and release it.  The word <strong>view</strong> will popup.  Go ahead and click on it.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/11setviewtofilesowner1.png"><img class="size-full wp-image-150 aligncenter" title="11setviewtofilesowner" src="/wp-content/uploads/2008/08/11setviewtofilesowner1.png" alt="" width="287" height="708" /></a></p>
<p style="text-align: left;">Close <strong>Interface Builder</strong>. You can now click <strong>Build and Go</strong>. When you click on the words &#8220;Next View&#8221;, you will see the screen transition to your new view.  There is still one thing missing.  There is no back button in the <strong>NavigationController</strong> at the top.  Apple actually adds this for us, but we need to set a title on our main view.</p>
<h2><a name="back-button">Adding The Back Button</a></h2>
<p style="text-align: left;">Close the <strong>iPhone Simulator</strong> and open <strong>RootViewController.m</strong>.  In the <strong>viewDidLoad</strong> method (gets called when the view is first loaded) add the following code.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/12settitle1.png"><img class="size-full wp-image-151 aligncenter" title="12settitle" src="/wp-content/uploads/2008/08/12settitle1.png" alt="" width="413" height="75" /></a></p>
<p style="text-align: left;">Since RootViewController extends Apple&#8217;s class <strong>UITableViewController</strong>, it comes with a title property.  You can set this to anything you want.  I have set it to the string &#8220;Hello&#8221;.  Now click <strong>Build and Go</strong> and you are done.  Here are a few screenshots.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/13screenshot1.png"><img class="size-full wp-image-152 aligncenter" title="13screenshot" src="/wp-content/uploads/2008/08/13screenshot1.png" alt="" width="386" height="742" /></a></p>
<p style="text-align: left;">When you click on &#8220;Next View&#8221; it should transition to:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/14screenshot21.png"><img class="size-full wp-image-153 aligncenter" title="14screenshot2" src="/wp-content/uploads/2008/08/14screenshot21.png" alt="" width="386" height="742" /></a></p>
<p style="text-align: left;">Notice the back button at the top with the text &#8220;Hello&#8221;.  If you press it, your view will be popped from the <strong>NavigationController </strong>stack and the previous view will be shown.  If you have any problems/questions/comments post them in the comments.  I&#8217;m pretty good about answering them as it emails me when you do so and I receive them on my iPhone.  If you have any problems, you can download the source code here <a href="/wp-content/uploads/2008/08/hello-world-views1.zip">Hello World Views Source</a>.  Happy iCoding!</p>
<p style="text-align: left;">
<p style="text-align: center;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2008/08/03/iphone-programming-tutorial-transitioning-between-views/feed/</wfw:commentRss>
		<slash:comments>211</slash:comments>
		</item>
		<item>
		<title>iPhone Programming Tutorial &#8211; Connecting Code to An Interface Builder View</title>
		<link>http://icodeblog.com/2008/07/30/iphone-programming-tutorial-connecting-code-to-an-interface-builder-view/</link>
		<comments>http://icodeblog.com/2008/07/30/iphone-programming-tutorial-connecting-code-to-an-interface-builder-view/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 22:44:25 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[iphone example program]]></category>
		<category><![CDATA[iphone programming]]></category>
		<category><![CDATA[iphone sdk]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=94</guid>
		<description><![CDATA[
Finally, we get to write some real code! In this tutorial, I will show you how to create an interface using Interface Builder and connect it to your code.  We will be creating a UITextField, UILabel, and a Button. Now, don&#8217;t be intimidated that this tutorial is so long.  I have really went into detail explaining everything as I go.  You could easily scan over it and get the gist of it.  Here&#8217;s how the application  ...]]></description>
				<content:encoded><![CDATA[<p><script type="text/javascript"><!--
digg_url = 'http://digg.com/apple/Great_Beginner_iPhone_Programming_tutorial_2';
// --></script><script src="http://digg.com/api/diggthis.js"></script></p>
<p>Finally, we get to write some real code! In this tutorial, I will show you how to create an interface using Interface Builder and connect it to your code.  We will be creating a<strong> UITextField, UILabel, </strong>and a <strong>Button.</strong> Now, don&#8217;t be intimidated that this tutorial is so long.  I have really went into detail explaining everything as I go.  You could easily scan over it and get the gist of it.  Here&#8217;s how the application will work:</p>
<ol>
<li>The user will tap inside a text box which brings up the iPhone&#8217;s keyboard</li>
<li>The user will type their name using the keyboard</li>
<li>The user will press a button</li>
<li>The label will update with a greeting containing that user&#8217;s name (ex. &#8220;Hello Brandon!&#8221;)</li>
<li>If the user fails to enter in text, the label will say something like &#8220;Please Enter Your Name&#8221;</li>
</ol>
<p><strong>Prerequisites: </strong>This tutorial assumes that you have completed the following tutorials</p>
<ul>
<li><a href="/2008/07/26/iphone-programming-tutorial-hello-world/">Hello World Tutorial Using UITableView</a></li>
<li><a title="Learn to use the interface builder for iPhone" href="/2008/07/29/iphone-programming-tutorial-beginner-interface-builder-hello-world/">Beginner Interface Builder</a></li>
</ul>
<p>In this tutorial you will learn:</p>
<ul>
<li><a href="#view-based-application">Create a new <strong>View-Based</strong> application</a></li>
<li><a href="#iPhone-user-interface">Create a simple user interface</a></li>
<li><a href="#connect-interface">Write code to connect to an interface</a></li>
<li><a href="#connect-interface-to-code">Connect the interface to the code</a></li>
<li><a href="#update-interface">Update the interface based on user interaction</a></li>
</ul>
<h2><a name="view-based-application"></a></h2>
<p>Like the last tutorial I wrote, we are going to need only one view.  So we will just use Apple&#8217;s <strong>View-Based Application</strong> template.  So click <strong>File -&gt; New Project.</strong> Select <strong>View-Based </strong>Application and name it <strong>ViewBased</strong> (You can name it whatever you want).</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/07/screenshot_0111.png"><img class="size-full wp-image-95 aligncenter" title="View-Based Application" src="/wp-content/uploads/2008/07/screenshot_0111.png" alt="" width="500" height="368" /></a></p>
<p>So like last time, Apple has provided us with most of the code needed to get this app up and running.  You can click <strong>Build and Go</strong> to launch the simulator, but all you will see is blankness.</p>
<p>Let&#8217;s get started by double clicking on <strong>ViewBasedViewController.xib.</strong> This is a nib file that opens with <strong>Interface Builder.</strong> It contains information regarding the layout and controls of our view.  Once you do this, <strong>Interface Builder </strong>should open up.</p>
<h2><a name="iPhone-user-interface"></a></h2>
<p>It will look something like the screenshot below.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/07/screenshot_0311.png"><img class="size-full wp-image-97 aligncenter" title="screenshot_031" src="/wp-content/uploads/2008/07/screenshot_0311.png" alt="" width="500" height="312" /></a></p>
<p style="text-align: left;">A few notes about interface builder&#8230;</p>
<p style="text-align: left;"><strong>Library &#8211; </strong>The right-most window contains all of your controls that you can add to your view.  For this tutorial we will be using a <strong>TextField, Label, </strong>and <strong>Button.</strong></p>
<p style="text-align: left;">The next window to the left of that contains objects that we will connect our interface to.  <strong>View </strong>represents the view of this nib file (basically the interface).  <strong>File&#8217;s Owner</strong> is the object that links the interface to the code.</p>
<p style="text-align: left;"><strong>View -</strong> This is your user interface for your iPhone application.  This window is where you will drop controls from the right-most window.</p>
<p style="text-align: left;"><strong>Attributes &#8211; </strong>This is where we will set the styling properties of our controls</p>
<h3 style="text-align: left;">Add a Text Field</h3>
<p style="text-align: left;">The first thing you want to do is drag a <strong>Text Field </strong>from the <strong>library</strong> box on to your <strong>view </strong>window.  You will see some blue lines to guide you.  These are in place to help line up controls as well as center them.</p>
<p style="text-align: left;">Once you have added the <strong>Text Field</strong> to the <strong>View,</strong> move it around until it&#8217;s in a position that you are happy with.  Next, stretch each side of the <strong>text box </strong>so that it spans accross almost the entire <strong>view </strong>area.  (The blue lines on the right and left will let you know when to stop.)</p>
<p style="text-align: left;">Now we are going to set some of the attributes of the <strong>Text Field.</strong> If the <strong>Attributes Inspector </strong>doesn&#8217;t appear, click on the <strong>Text Field </strong>and then click <strong>Tools </strong>-&gt; <strong>Attributes Inspector.</strong></p>
<ul>
<li>In the <strong>Placeholder</strong> field type in <strong>Name.</strong> This is the default text that appears in the <strong>Text Field</strong> before a user types anything.</li>
<li>For <strong>Capitalize </strong>select <strong>Words &#8211; </strong>This tells XCode that we want to capitalize each word</li>
<li>For <strong>Return Key</strong> &#8211; Select <strong>Done.</strong> This makes the return key on the keyboard say <strong>Done</strong> rather than return.</li>
<li>Also, make sure <strong>Clear When Edit Begins </strong>is checked</li>
</ul>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/07/screenshot_211.png"><img class="size-full wp-image-109 aligncenter" title="Text Field Attributes" src="/wp-content/uploads/2008/07/screenshot_211.png" alt="" width="287" height="708" /></a></p>
<p style="text-align: left;">
<h3>Add a Label</h3>
<p>Drag a <strong>Label </strong>from the <strong>Library </strong>onto your <strong>view.</strong> Similarly, stretch it the length of your <strong>view </strong>and place it where you would like.  Let&#8217;s change the default text of the <strong>label.</strong> If the <strong>Attributes Inspector </strong>doesn&#8217;t appear, click on the <strong>Label </strong>and then click <strong>Tools </strong>-&gt; <strong>Attributes Inspector.<strong> </strong></strong>In the <strong>Text </strong>field type in &#8220;<strong>Enter your name above</strong>&#8221; (or below depending on where you chose to put the label in relation to the <strong>Text Field.</strong></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/07/screenshot_231.png"><img class="size-full wp-image-111 aligncenter" title="Label Attributes" src="/wp-content/uploads/2008/07/screenshot_231.png" alt="" width="287" height="708" /></a></p>
<h2>Add a Button</h2>
<p>Now drag a <strong>Button </strong>from the <strong>library </strong>onto your <strong>view.</strong> Stretch it and position it however you would like.  Now we need to add some text to the <strong>Button</strong>.  Click on the button and then click <strong>Tools -&gt; Attributes Inspector.</strong> In the <strong>Title</strong> field, type &#8220;<strong>Display</strong>&#8220;.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/07/screenshot_111.png"><img class="size-full wp-image-100 aligncenter" title="Button Attributes Inspector" src="/wp-content/uploads/2008/07/screenshot_111.png" alt="" width="287" height="708" /></a></p>
<p style="text-align: left;">We are now done creating our interface.  It should look something like this:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/07/screenshot_151.png"><img class="size-full wp-image-103 aligncenter" title="Interface Screenshot" src="/wp-content/uploads/2008/07/screenshot_151.png" alt="" width="320" height="482" /></a></p>
<p style="text-align: left;">Let&#8217;s return to our code&#8230; Close <strong>Interface Builder</strong> and go back to <strong>Xcode.</strong></p>
<h2 style="text-align: left;"><a name="connect-interface"></a></h2>
<p>The files that link an interface to some code are called <strong>View Controllers. </strong>Let&#8217;s open up <strong>ViewBasedViewController.h</strong>. This is the file where we will declare all of our interface variables.  Add the following code to you <strong>ViewBasedViewController.h</strong>.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/07/viewbasedcontrollerh1.png"><img class="alignnone size-full wp-image-132" title="viewbasedcontrollerh" src="/wp-content/uploads/2008/07/viewbasedcontrollerh1.png" alt="" width="363" height="198" /></a></p>
<p style="text-align: left;"><strong>Interface Builder</strong> uses <strong>IBOutlets </strong>and <strong>IBAction</strong>s to connect to the code.  Here is a brief explanation of each line of code.</p>
<ul>
<li><strong>IBOutlet UITextField *textName &#8211; </strong>creates an outlet to connect to the text field we created in interface builder.  We use this variable to get information from the text field.</li>
<li><strong>IBOutlet UILabel *lblHello &#8211; </strong>An outlet that connects our label on our interface to the code.  This variable is used to set the value of the label.</li>
</ul>
<p>Now that we have declared these variables, we need to make them <strong>properties</strong>. This allows us to set certain attributes that are associated with the variables.  <strong>Retain</strong> tells the compiler that we will handle the memory management of this object (don&#8217;t forget to release it when you are done).  Otherwise it will get &#8220;cleaned&#8221; after being instantiated.</p>
<p>There is one other thing here.</p>
<p><strong>- (IBAction) updateText:(id) sender;</strong></p>
<p>This is the function that will get called when the user presses the button that was created in <strong>Interface Builder.</strong> We are simply declaring it here in the header file.  We will implement this function a little later in the tutorial.  Now, we need to connect the interface to the code.  Double click on<strong> ViewBasedViewController.xib </strong>again to open up <strong>Interface Builder.</strong></p>
<h2><a name="connect-interface-to-code"></a></h2>
<ol>
<li>Connect the <strong>View</strong>
<ol>
<li>Click anywhere on the background of your view (anywhere that is not the Text Field, Label, or Button).  Now click <strong>Tools -&gt; Connections Inspector. </strong>Next to <strong>New Referencing Outlet,</strong> you will see a circle.  Click in that circle and drag the blue line to the <strong>File&#8217;s Owner</strong> object and release it. The word <strong>view </strong>should pop up. Click on it.  You have now connected the view to your proxy object.  You should now see:</li>
<li style="text-align: center;"><a href="/wp-content/uploads/2008/07/screenshot_161.png"><img class="alignnone size-full wp-image-104" title="View Connections" src="/wp-content/uploads/2008/07/screenshot_161.png" alt="" width="287" height="708" /></a></li>
</ol>
</li>
<li style="text-align: left;">Connect the <strong>Text Field</strong>
<ol>
<li style="text-align: left;">Click on the <strong>Text Field </strong>in your <strong>View</strong> window to select it.  Then click <strong>Tools -&gt; Connections Inspector.</strong> You will now see a circle next to <strong>New Referencing Outlet.</strong> Click in that circle and drag it over to the <strong>File&#8217;s Owner</strong> object.  A message will pop up with <strong>txtName.</strong> Click on <strong>txtName</strong> and the connection is made. You should now see:</li>
<li style="text-align: left;">
<p style="text-align: center;"><img class="alignnone size-full wp-image-102" title="screenshot_14" src="/wp-content/uploads/2008/07/screenshot_141.png" alt="" width="287" height="708" /></p>
</li>
</ol>
</li>
<li style="text-align: left;"> Connect the <strong>Label</strong>
<ol>
<li style="text-align: left;">Click on the <strong>Label </strong>in your <strong>View</strong> window to select it.  Then click <strong>Tools -&gt; Connections Inspector.</strong> You will now see a circle next to <strong>New Referencing Outlet.</strong> Click in that circle and drag it over to the <strong>File&#8217;s Owner</strong> object.  A message will pop up with <strong>lblHello.</strong> Click on <strong>lblHello </strong>and the connection is made. You should now see:</li>
<li style="text-align: center;"><a href="/wp-content/uploads/2008/07/screenshot_221.png"><img class="alignnone size-full wp-image-110" title="screenshot_22" src="/wp-content/uploads/2008/07/screenshot_221.png" alt="" width="287" height="708" /></a></li>
</ol>
</li>
<li style="text-align: left;">Connect the <strong>Button</strong>
<ol>
<li style="text-align: left;">Click on the <strong>Button </strong>in your <strong>View</strong> window to select it.  Then click <strong>Tools -&gt; Connections Inspector.</strong> You will now see a circle next to <strong>Touch Up Inside.</strong> This is the trigger that gets called when a user presses the button. Click in that circle and drag it over to the <strong>File&#8217;s Owner</strong> object.  A message will pop up with <strong>updateText.</strong> Click on <strong>updateText </strong>and the connection is made. You should now see:</li>
<li style="text-align: center;"><a href="/wp-content/uploads/2008/07/screenshot_0811.png"><img class="alignnone size-full wp-image-98" title="Button Connections" src="/wp-content/uploads/2008/07/screenshot_0811.png" alt="" width="287" height="708" /></a></li>
</ol>
</li>
</ol>
<p>Now all of the connections should be set up.  Go ahead and close <strong>Interface Builder.</strong> We are done using it.</p>
<h2><a name="update-interface"></a></h2>
<p>Open up the file <strong>ViewBasedViewController.m . </strong>This is the file where we will implement the <strong>updateText </strong>function.  Add the following code&#8230;</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/07/viewbasedcontroller1.png"><img class="alignnone size-full wp-image-130" title="viewbasedcontroller" src="/wp-content/uploads/2008/07/viewbasedcontroller1.png" alt="" width="465" height="310" /></a></p>
<p style="text-align: left;">This code is pretty straight forward and easy to follow.  I will explain it line-by-line:</p>
<p style="text-align: left;"><strong>@synthesize txtName,lblHello;</strong></p>
<p style="text-align: left;">Most of the time when creating (private) variables, you need to specify what are called &#8220;getter&#8221; and &#8220;setter&#8221; methods.  Theses functions get the value of a variable and set the value of a variable.  What <strong>synthesize </strong> does is creates these methods for you under the hood.  Pretty handy&#8230;</p>
<p style="text-align: left;">Next we will implement our <strong>updateText</strong> method.  I started by creating a temporary string.  This is the string that we will insert into the text of the label.</p>
<p style="text-align: left;">The next line checks to see if the user has entered any text int the <strong>Text Field.</strong> txtName.text returns an <strong>NSString. </strong>We are simply calling the length method on a string.  If the length is 0, then obviously the user has not entered any text.  If this is the case, we set the temporary string to &#8220;Please enter your name&#8221;: instructing the user to enter in their name.</p>
<p style="text-align: left;">If the user has entered in some text, a new string is allocated.  The initWithFormat method is similar to <strong>printf</strong> in C.  So, I used the string &#8220;Hello %@!&#8221;.  The &#8220;%@&#8221; in the string means we will be substituting it  for a string.  To get the value of the <strong>Text Field </strong>we again use the <strong>txtName.text</strong> property.</p>
<p style="text-align: left;">Finally, the value of the <strong>Label </strong>is set by calling <strong>lblHello.text</strong>. This calls the <strong>text</strong> property of label and sets the text to our temporary string variable.</p>
<p style="text-align: left;">The last line <strong>[text release];</strong> releases the temporary text field from memory.  This is necessary to write an efficient iPhone application.  If you want to know why a lot of iPhone apps crash, it&#8217;s because the developers don&#8217;t follow through with this step.</p>
<p style="text-align: left;">That&#8217;s it!  Click <strong>Build and Go.</strong> The application should launch in the <strong>iPhone Simulator</strong>.  When you click inside the <strong>Text Field</strong> it should bring up the <strong>iPhone&#8217;s</strong> keyboard (you can also type with your keyboard).  Enter in your name and click &#8220;Display&#8221;.  Here are a few screens of what your app should look like.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/07/screenshot_181.png"><img class="size-full wp-image-106 aligncenter" title="iPhone Screenshot 1" src="/wp-content/uploads/2008/07/screenshot_181.png" alt="" width="386" height="742" /></a></p>
<p style="text-align: center;">User Clicks <strong>Display</strong> without typing in their name</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/07/screenshot_191.png"><img class="alignnone size-full wp-image-107" title="iPhone Screenshot 2" src="/wp-content/uploads/2008/07/screenshot_191.png" alt="" width="386" height="742" /></a></p>
<p style="text-align: center;">User types in their name and clicks <strong>Display</strong></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/07/screenshot_201.png"><img class="alignnone size-full wp-image-108" title="iPhone Screenshot 3" src="/wp-content/uploads/2008/07/screenshot_201.png" alt="" width="386" height="742" /></a></p>
<p style="text-align: left;">Well, that concludes this tutorial.  I hope that I was able to help you out on your road to iPhone glory. If you get lost at any point you can download the code to this tutorial here <a href="/wp-content/uploads/2008/07/viewbased1.zip">ViewBasedSampleCode</a>.  As always, if you have any questions or comments, be sure to leave them in the comments section of this page.  Happy iCoding!</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2008/07/30/iphone-programming-tutorial-connecting-code-to-an-interface-builder-view/feed/</wfw:commentRss>
		<slash:comments>215</slash:comments>
		</item>
		<item>
		<title>iPhone Programming Tutorial &#8211; UITableView Hello World</title>
		<link>http://icodeblog.com/2008/07/26/iphone-programming-tutorial-hello-world/</link>
		<comments>http://icodeblog.com/2008/07/26/iphone-programming-tutorial-hello-world/#comments</comments>
		<pubDate>Sat, 26 Jul 2008 17:02:30 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=30</guid>
		<description><![CDATA[
In this tutorial I will walk to you through creating a simple &#8220;Hello World&#8221; application using a UITableView for the iPhone.  There are many ways that a Hello World program could be made on the iPhone, I am going to show you the simplest.  This tutorial assumes you have a basic understanding of Objective-C.  Apple has provided a very simple and straight forward tutorial on Objective-C.  You can find it <a href="http://developer.apple.com/iphone/gettingstarted/docs/objectivecprimer.action">here</a>.
You will learn how to:

<a  ...]]></description>
				<content:encoded><![CDATA[<p><script type="text/javascript"><!--
digg_url = 'http://digg.com/programming/Great_Beginner_iPhone_Programming_Tutorial';
// --></script><script src="http://digg.com/api/diggthis.js"></script></p>
<p>In this tutorial I will walk to you through creating a simple &#8220;Hello World&#8221; application using a UITableView for the iPhone.  There are many ways that a Hello World program could be made on the iPhone, I am going to show you the simplest.  This tutorial assumes you have a basic understanding of Objective-C.  Apple has provided a very simple and straight forward tutorial on Objective-C.  You can find it <a href="http://developer.apple.com/iphone/gettingstarted/docs/objectivecprimer.action">here</a>.</p>
<p>You will learn how to:</p>
<ul>
<li><a href="#create-a-new-navigation-based-application">Create a New Navigation-Based Application</a></li>
<li><a href="#learn-about-default-files">Learn About the Default Files</a></li>
<li><a href="#update-uitableview">Update the UITableView Cells to Display &#8220;Hello World&#8221; Text</a></li>
</ul>
<p>This tutorial assumes that you have already installed the iPhone SDK.  If you are unsure how to do this, <a title="iPhone Tutorial - Getting Set Up" href="/2008/07/24/iphone-programming-tutorial-1-getting-set-up/">click and follow the steps.</a></p>
<h2><a name="create-a-new-navigation-based-application">Creating a New Navigation-Based Application</a></h2>
<p><strong>Open Up Xcode</strong><br />
<img class="alignnone size-medium wp-image-32" title="xcode" src="/wp-content/uploads/2008/07/xcode-281x300.jpg" alt="" width="94" height="101" /><br />
You will be doing all of your development in Xcode. Then close the Welcome window (if it shows up)</p>
<p><strong>Start a new iPhone OS Project</strong><br />
Click Xcode &gt; New Project and a window should pop up like this:</p>
<p style="text-align: center;"><img class="size-full wp-image-33 aligncenter" title="New iPhone Project" src="/wp-content/uploads/2008/07/new-project1.png" alt="" width="500" height="368" /></p>
<p>Make sure <strong>Application</strong> is selected under <strong>iPhone OS</strong> and then select <strong>Navigation-Based Application.</strong> Click <strong>Choose&#8230; </strong>It will ask you to name your project.  Type in &#8220;Hello World&#8221; and let&#8217;s get started.</p>
<h2><a name="learn-about-default-files">Learn About the Default Files</a></h2>
<p><strong>What is all this stuff?<br />
<span style="font-weight: normal;">There are quite a few files that get added to your project.  At first glance, this looks kind of intimidating.  Don&#8217;t worry, we only need to edit one of them. Here is a quick explanation of the different files. </span></strong> You don&#8217;t have to read this part but having this many files is what confused me the most when I started developing for the iPhone.</p>
<ol>
<li><strong>CoreGraphics.framework, Foundation.framwork, UIKit.framework &#8211; </strong>You guessed it, this is a set of library functions provided by Apple that we use in our application. We use these as includes similar to any other language that includes library functions.</li>
<li><strong>HelloWorld.app &#8211; </strong>This is your app that gets installed on the iPhone.  We don&#8217;t really need to worry about this right now</li>
<li><strong>Hello_World_Prefix.pch &#8211; </strong>This is another include file that gets compiled separately from your other files so you don&#8217;t need to include it on each file. It contains some code to include the data inside the frameworks.<strong> </strong></li>
<li><strong>Hello_WorldAppDelegate.h &#8211; </strong>This is a header file that contains all of our definitions for variables that we will be using.  It&#8217;s very similar to a header file in C or C++;</li>
<li><strong>Hello_WorldAppDelegate.m &#8211; </strong>All of the magic starts here.  Consider this file our starting point for execution.  The main.m file invokes this object.</li>
<li><strong>Info.plist &#8211; </strong>This contains various meta information about your program.  You won&#8217;t really need to edit this until you are ready to start testing on the iPhone</li>
<li><strong>main.m &#8211; </strong>Like most programming language, this file contains our main function.  This is where execution begins.  The main function basically instantiates our object and starts the program.  You shouldn&#8217;t need to edit this file.</li>
<li><strong>MainWindow.xib &#8211; </strong>This contains the visual information of our main window.  If you double click on it, it will open in a program called &#8220;Interface Builder&#8221;.  We will get to this a little later.  Just on thing to note is this file does not contain any code.</li>
<li><strong>RootViewController.h, RootViewController.m -</strong> These are files for a view controller that gets added to our main window.  Basically, Apple has already created a simple interface when you clicked on <strong>Navigation-Based Application.</strong> Since most navigation-based applications use a Table View, Apple has provided it for us to use.</li>
<li><strong>RootViewController.xib &#8211; </strong>This is a view that Apple has provided that emulates a table.  It has rows and columns.  We will be displaying our &#8220;Hello World&#8221; text inside one of these rows</li>
</ol>
<div>Now, all of these files together create a basic program.  Go ahead and click on the <strong>Build and Go </strong>button at the top of Xcode. Make sure the drop-down on the top left says <strong>Simulator | Debug, </strong>this tells Xcode that we are testing on the iPhone simulator.</div>
<div style="text-align: center;"><a href="/wp-content/uploads/2008/07/buildandgo1.png"><img class="alignnone size-full wp-image-36" title="Build And Go" src="/wp-content/uploads/2008/07/buildandgo1.png" alt="" width="500" height="58" /></a></div>
<div>You will see the iPhone simulator start and your program will launch.  It&#8217;s not very interesting at the moment.  All it shows is the Table View that Apple has added for us.  So what we are going to do is add a row to this table view.</div>
<div style="text-align: center;"><span style="text-decoration: underline;"><a href="/wp-content/uploads/2008/07/iphone-11.png"></a><a href="/wp-content/uploads/2008/07/iphone-21.png"><img class="alignnone size-full wp-image-38" title="iPhone Simulator" src="/wp-content/uploads/2008/07/iphone-21.png" alt="" width="320" height="483" /></a></span></div>
<h2><a name="update-uitableview">Update the UITableView Cells to Display &#8220;Hello World&#8221; Text</a></h2>
<div><strong>Let&#8217;s write some code</strong></div>
<div>Start by opening <strong>RootViewController.m.</strong> This is the view controller that Apple added to our main view.  All of the functions you see already created in here are functions that have been overridden from the Table View super class.  Since we are editing a table, all of these functions will be related to editing a table.  So find the function called <strong>numberOfRowsInSection.</strong></div>
<div style="text-align: center;"><a href="/wp-content/uploads/2008/07/numberofrowsinsection1.png"><img class="alignnone size-full wp-image-39" title="numberofrowsinsection" src="/wp-content/uploads/2008/07/numberofrowsinsection1.png" alt="" width="500" height="46" /></a></div>
<div>This function tells the application how many rows are in our table.  Currently, it returns 0. Let&#8217;s change that to return 1. This will tell the application that we want 1 row in our table.  Now go down to the function called <strong>cellForRowAtIndexPath. </strong> This function gets called once for every row.  This is where we define what content to display in a given row.  In this case we want the row to be a string that says &#8220;Hello World&#8221;.</div>
<div style="text-align: center;"><span style="text-decoration: underline; color: #0000ee;"><a href="/wp-content/uploads/2008/07/cellforrowatindexpath1.png"></a><a href="/wp-content/uploads/2008/07/cellforrowatindexpath1.png"><img class="alignnone size-full wp-image-40" title="cellforrowatindexpath" src="/wp-content/uploads/2008/07/cellforrowatindexpath1.png" alt="" width="500" height="134" /></a></span></div>
<div>What this function is doing is creating a new cell object and returning it.  The code between the i(cell==null) block checks to see if we have created a cell before, if not build a new cell, otherwise use the one we created before.  This helps in performance so we don&#8217;t need to build new cells each time we visit this function.  So right before the <strong>// Set up the cell</strong> comment add the following code:</div>
<div>[cell setText:@&#8221;Hello World&#8221;];</div>
<div style="text-align: center;"><a href="/wp-content/uploads/2008/07/helloworld1.png"><img class="alignnone size-full wp-image-41" title="helloworld" src="/wp-content/uploads/2008/07/helloworld1.png" alt="" width="499" height="153" /></a></div>
<div>We are basically calling the setText method of the cell object and pass in the string &#8220;Hello World&#8221;.  As you should know from reading <a href="http://developer.apple.com/iphone/gettingstarted/docs/objectivecprimer.action" target="_blank">Apples Objective-C overview</a>, strings begin with an &#8220;@&#8221; symbol.</div>
<div>That&#8217;s it!  Click the <strong>Build and Go </strong>button again to launch the iPhone simulator.  You should now see a screen like this:</div>
<div style="text-align: center;"><a href="/wp-content/uploads/2008/07/hello-world1.png"><img class="alignnone size-full wp-image-42" title="hello-world" src="/wp-content/uploads/2008/07/hello-world1.png" alt="" width="321" height="482" /></a></div>
<div>You can now use this code as a base for creating a Navigation Based application.  In a later tutorial, I will detail how to populate this table view with an array of strings to create navigation for a simple program.  If you get lost at any time, you can download the sample code for this program here <a href="/wp-content/uploads/2008/07/hello-world1.zip">hello-world</a>.  I hope you enjoyed the tutorial and if you have any questions, feel free to leave them in the comments. Happy Xcoding!</div>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2008/07/26/iphone-programming-tutorial-hello-world/feed/</wfw:commentRss>
		<slash:comments>163</slash:comments>
		</item>
	</channel>
</rss>
