<?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; hello world</title>
	<atom:link href="/tag/hello-world/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>Back To Basics: Hello iPhone</title>
		<link>http://icodeblog.com/2011/08/17/back-to-basics-hello-iphone/</link>
		<comments>http://icodeblog.com/2011/08/17/back-to-basics-hello-iphone/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 21:01:43 +0000</pubDate>
		<dc:creator><![CDATA[brandontreb]]></dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iphone]]></category>

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

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
&nbsp;
    CGRect frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">10</span>, <span style="color: #2400d9;">10</span>, <span style="color: #2400d9;">300</span>, <span style="color: #2400d9;">25</span><span style="color: #002200;">&#41;</span>;
    UILabel <span style="color: #002200;">*</span>helloLabel <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UILabel alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>frame<span style="color: #002200;">&#93;</span>;
    helloLabel.text <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Hello iPhone!&quot;</span>;
    helloLabel.textColor <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIColor blueColor<span style="color: #002200;">&#93;</span>;
    helloLabel.textAlignment <span style="color: #002200;">=</span> UITextAlignmentCenter;
    <span style="color: #002200;">&#91;</span>self.view addSubview<span style="color: #002200;">:</span>helloLabel<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>helloLabel release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

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