<?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; iphone sdk</title>
	<atom:link href="/tag/iphone-sdk/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>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>
	</channel>
</rss>
