<?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>iCodeBlog &#187; Interface Builder</title>
	<atom:link href="/category/interface-builder/feed/" rel="self" type="application/rss+xml" />
	<link>http://icodeblog.com</link>
	<description>iPhone Programming Tutorials</description>
	<lastBuildDate>Tue, 22 Dec 2009 19:04:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using NSXMLParser to Pull UIImages From the Web</title>
		<link>http://icodeblog.com/2009/06/19/using-nsxmlparser-to-pull-uiimages-from-the-web/</link>
		<comments>http://icodeblog.com/2009/06/19/using-nsxmlparser-to-pull-uiimages-from-the-web/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 23:35:15 +0000</pubDate>
		<dc:creator>Collin</dc:creator>
				<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[NSXMLParser]]></category>
		<category><![CDATA[UIImage]]></category>
		<category><![CDATA[UIScrollView]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=945</guid>
		<description><![CDATA[<a href="/2009/06/19/using-nsxmlparser-to-pull-uiimages-from-the-web/"><img align="left" hspace="5" width="150" height="150" src="/wp-content/uploads/2009/06/picture-32-150x150.png" class="alignleft wp-post-image tfe" alt="picture-32" title="" /></a>Introduction
Hello everyone, welcome to my third screeencast. This screencast is the result of a request made in the comments of my first post. I am going to be covering many topics in this post. But the general idea of the app we will build is that it will use an XML file online to get [...]]]></description>
			<content:encoded><![CDATA[<h1><span style="color: #ff6600;">Introduction</span></h1>
<p>Hello everyone, welcome to my third screeencast. This screencast is the result of a request made in the comments of my <a title="Creating a Custom UITableViewCell using Interface Builder" href="/2009/05/24/custom-uitableviewcell-using-interface-builder/" target="_blank">first post</a>. I am going to be covering many topics in this post. But the general idea of the app we will build is that it will use an XML file online to get the URL and title of a given picture. For each URL and Title pair a view will be created with a UIImageView showing the image and a UILabel showing the title. Each of these views will be placed in a UIScrollView to flip through, like th functinoality of the Photos app.</p>
<h2><span style="color: #000000;">Skill Level</span> <span style="color: #ff6600;">Medium</span></h2>
<p>This app is going to require a decent amount of experience with Objective C and xCode. Also some minimal understanding of XML and XML schema/structure would be valuable.</p>
<h1><span style="color: #ff6600;">Source Code<br />
</span></h1>
<h3><a title="Using NSXMLParser to Pull UIImages from the Web" href="/wp-content/uploads/2009/06/iCodeBlogXMLImage.zip"><span style="text-decoration: underline;">Available Here</span></a></h3>
<h1><span style="color: #ff6600;">Screencast</span></h1>
<p>I film myself coding out the entire sample project for each post. I personally think going through the Screencast is the best way to learn. But feel free to look through the slides and text if that suites you better.</p>
<p><object width="600" height="375"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=5515556&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=ff9933&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id=5515556&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=ff9933&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="600" height="375"></embed></object>
<p><a href="http://vimeo.com/5515556">iCodeBlog: Using NSXMLParser to pull UIImages off the Web</a> from <a href="http://vimeo.com/user2008025">Collin Ruffenach</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<h2><span style="color: #ff6600;">SCREENCAST ADDITION</span></h2>
<pre style="padding-left: 30px;">Adding a final line to layoutSubviews should be:</pre>
<pre style="padding-left: 30px;">     [scrollview setFrame:workingFrame];

this will stop the scroll view from bouncing up and down.</pre>
<h1><span style="color: #ff6600;">Tutorial</span></h1>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-947" src="/wp-content/uploads/2009/06/picture-32.png" alt="picture-32" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-948" src="/wp-content/uploads/2009/06/picture-41.png" alt="picture-41" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-949" src="/wp-content/uploads/2009/06/picture-5.png" alt="picture-5" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-950" src="/wp-content/uploads/2009/06/picture-61.png" alt="picture-61" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-951" src="/wp-content/uploads/2009/06/picture-71.png" alt="picture-71" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-952" src="/wp-content/uploads/2009/06/picture-81.png" alt="picture-81" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-953" src="/wp-content/uploads/2009/06/picture-91.png" alt="picture-91" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-954" src="/wp-content/uploads/2009/06/picture-101.png" alt="picture-101" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-955" src="/wp-content/uploads/2009/06/picture-111.png" alt="picture-111" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-956" src="/wp-content/uploads/2009/06/picture-121.png" alt="picture-121" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-957" src="/wp-content/uploads/2009/06/picture-131.png" alt="picture-131" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-958" src="/wp-content/uploads/2009/06/picture-141.png" alt="picture-141" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-959" src="/wp-content/uploads/2009/06/picture-151.png" alt="picture-151" width="620" height="387" /></span></p>
<h2><span style="color: #ff6600;">iCodeBlogXMLImagesViewController.h:</span></h2>
<pre style="padding-left: 60px;">@interface iCodeBlogXMLImagesViewController : UIViewController
{
	IBOutlet UIScrollView *scrollview;
}

@property (nonatomic, retain) IBOutlet UIScrollView *scrollview;</pre>
<h2><span style="color: #ff6600;">iCodeBlogXMLImagesViewController.m</span></h2>
<pre style="padding-left: 60px;">@synthesize scrollview;</pre>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-960" src="/wp-content/uploads/2009/06/picture-161.png" alt="picture-161" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-961" src="/wp-content/uploads/2009/06/picture-171.png" alt="picture-171" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-962" src="/wp-content/uploads/2009/06/picture-181.png" alt="picture-181" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-963" src="/wp-content/uploads/2009/06/picture-191.png" alt="picture-191" width="620" height="387" /></span></p>
<h2><span style="color: #ff6600;">iCodeBlogXMLView.h:</span></h2>
<pre style="padding-left: 60px;">@interface iCodeBlogXMLView : UIView
{
	IBOutlet UIImageView *imageView;
	IBOutlet UILabel *title;
}

@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UILabel *title;

@end</pre>
<h2><span style="color: #ff6600;">iCodeBlogXMLView.m</span></h2>
<pre style="padding-left: 60px;">@synthesize imageView;
@synthesize title;</pre>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-964" src="/wp-content/uploads/2009/06/picture-201.png" alt="picture-201" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-965" src="/wp-content/uploads/2009/06/picture-211.png" alt="picture-211" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-966" src="/wp-content/uploads/2009/06/picture-221.png" alt="picture-221" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-967" src="/wp-content/uploads/2009/06/picture-23.png" alt="picture-23" width="620" height="387" /></span></p>
<h2><span style="color: #ff6600;">iCodeBlogXMLElement.h:</span></h2>
<pre style="padding-left: 60px;">@interface iCodeBlogXMLElement : NSObject
{
	UIImage *image;
	NSString *imageTitle;
}

@property (nonatomic, retain) UIImage *image;
@property (nonatomic, retain) NSString *imageTitle;

@end</pre>
<h2><span style="color: #ff6600;">iCodeBlogXMLElement.m</span></h2>
<pre style="padding-left: 60px;">@synthesize image;
@synthesize imageTitle;</pre>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-968" src="/wp-content/uploads/2009/06/picture-241.png" alt="picture-241" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-969" src="/wp-content/uploads/2009/06/picture-251.png" alt="picture-251" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-970" src="/wp-content/uploads/2009/06/picture-261.png" alt="picture-261" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-971" src="/wp-content/uploads/2009/06/picture-27.png" alt="picture-27" width="620" height="387" /></span></p>
<h2><span style="color: #ff6600;">iCodeBlogXMLImagesViewController.h:</span></h2>
<pre style="padding-left: 60px;">#import &lt;UIKit/UIKit.h&gt;
#import "iCodeBlogXMLElement.h"
#import "iCodeBlogXMLView.h"

@interface iCodeBlogXMLImagesViewController : UIViewController
{
	IBOutlet UIScrollView *scrollview;

	NSXMLParser *parser;
	NSMutableString *currentAttribute;
	NSMutableArray *xmlElementObjects;

	iCodeBlogXMLElement *tempElement;
}

@property (nonatomic, retain) IBOutlet UIScrollView *scrollview;

@property (nonatomic, retain) NSXMLParser *parser;
@property (nonatomic, retain) NSMutableString *currentAttribute;
@property (nonatomic, retain) NSMutableArray *xmlElementObjects;

@property (nonatomic, retain) iCodeBlogXMLElement *tempElement;

-(void)layoutSubview;

@end</pre>
<h2><span style="color: #ff6600;">iCodeBlogXMLImagesViewController.m</span></h2>
<pre style="padding-left: 60px;">@implementation iCodeBlogXMLImagesViewController

@synthesize scrollview;
@synthesize parser;
@synthesize currentAttribute;
@synthesize xmlElementObjects;
@synthesize tempElement;

...

- (void)viewDidLoad
{
    [super viewDidLoad];

	xmlElementObjects = [[NSMutableArray alloc] init];

	parser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://losectrl-gaincommand.com/iCodeBlogHelper/Tutorial3/iCodeBlogImageXML.xml"]];
	[parser setDelegate:self];
	[parser parse];
}

...

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{

}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{

}</pre>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-972" src="/wp-content/uploads/2009/06/picture-281.png" alt="picture-281" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-973" src="/wp-content/uploads/2009/06/picture-291.png" alt="picture-291" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-974" src="/wp-content/uploads/2009/06/picture-301.png" alt="picture-301" width="620" height="387" /></span></p>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-975" src="/wp-content/uploads/2009/06/picture-311.png" alt="picture-311" width="620" height="387" /></span></p>
<h2><span style="color: #ff6600;">iCodeBlogXMLImagesViewController.m</span></h2>
<pre style="padding-left: 60px;">- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
	if(![elementName compare:@"PictureInfo"])
	{
		tempElement = [[iCodeBlogXMLElement alloc] init];
	}

	else if(![elementName compare:@"imageURL"])
	{
		currentAttribute = [NSMutableString string];
	}

	else if(![elementName compare:@"imageTitle"])
	{
		currentAttribute = [NSMutableString string];
	}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
	if(![elementName compare:@"PictureInfo"])
	{
		[xmlElementObjects addObject:tempElement];
	}

	else if(![elementName compare:@"imageURL"])
	{
		NSURL *imageURL = [NSURL URLWithString:currentAttribute];
		NSData *data =  [NSData dataWithContentsOfURL:imageURL];
		UIImage *image = [[UIImage alloc] initWithData:data];

		[tempElement setImage:image];
	}

	else if(![elementName compare:@"imageTitle"])
	{
		NSLog(@"The image title is %@", currentAttribute);
		[tempElement setImageTitle:currentAttribute];
	}

	else if(![elementName compare:@"Pictures"])
	{
		[self layoutSubview];
	}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
	if(self.currentAttribute)
	{
		[self.currentAttribute appendString:string];
	}
}</pre>
<p><span style="color: #ff6600;"><img class="aligncenter size-full wp-image-976" src="/wp-content/uploads/2009/06/picture-321.png" alt="picture-321" width="622" height="388" /></span></p>
<h2><span style="color: #ff6600;">iCodeBlogXMLImagesViewController.m</span></h2>
<pre style="padding-left: 60px;"><span style="color: #ff6600;"><span style="color: #000000;">-(void)layoutSubview
{
	CGRect workingFrame;

	workingFrame.origin.x = 0;
	workingFrame.origin.y = 0;
	workingFrame.size.height = 480;
	workingFrame.size.width = 320;

	iCodeBlogXMLView *myView;

	for(iCodeBlogXMLElement *element in [self xmlElementObjects])
	{
		myView = [[iCodeBlogXMLView alloc] initWithFrame:workingFrame];

		NSLog(@"Element title is: %@", [element imageTitle]);

		NSArray *topLeveObjects = [[NSBundle mainBundle] loadNibNamed:@"iCodeBlogXMLView" owner:nil options:nil];

		for(id currentObject in topLeveObjects)
		{
			if([currentObject isKindOfClass:[iCodeBlogXMLView class]])
			{
				myView = (iCodeBlogXMLView *)currentObject;
			}
		}

		[[myView imageView] setImage:[element image]];
		[[myView title] setText:[element imageTitle]];
		[myView setFrame:workingFrame];

		[scrollview addSubview:myView];

		workingFrame.origin.x = workingFrame.origin.x + 320;
	}

	workingFrame.size.width = workingFrame.origin.x;
	[scrollview setContentSize:workingFrame.size];

	workingFrame.origin.x = 0;
	workingFrame.origin.y = 0;
	workingFrame.size.width = 320;
	workingFrame.size.height = 480;
}</span>

<img class="aligncenter size-large wp-image-996" src="/wp-content/uploads/2009/06/picture-33-1024x640.png" alt="picture-33" width="623" height="389" />
</span></pre>
<h2><span style="color: #ff6600;">iCodeBlogXMLImagesViewController.m</span></h2>
<pre>	parser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://losectrl-gaincommand.com/iCodeBlogHelper/Tutorial3/iCodeBlogImageXMLB.xml"]];</pre>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2009/06/19/using-nsxmlparser-to-pull-uiimages-from-the-web/feed/</wfw:commentRss>
		<slash:comments>35</slash:comments>
		</item>
		<item>
		<title>Custom UITableViewCell Using Interface Builder</title>
		<link>http://icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/</link>
		<comments>http://icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/#comments</comments>
		<pubDate>Mon, 25 May 2009 03:27:36 +0000</pubDate>
		<dc:creator>Collin</dc:creator>
				<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[iPhone Articles]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[uitableview]]></category>
		<category><![CDATA[UITableViewCell]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=752</guid>
		<description><![CDATA[<a href="/2009/05/24/custom-uitableviewcell-using-interface-builder/"><img align="left" hspace="5" width="150" height="150" src="/wp-content/uploads/2009/05/picture-1-150x150.png" class="alignleft wp-post-image tfe" alt="picture-1" title="" /></a>Hey everyone, welcome to my first of many screencasts coming in the next few weeks. Today I am going to show you how to layout a UITableViewCell in Interface Builder and have a UITableView populate with those type of cells. I am adopting a new structure for my screencasts which will be 5 or so [...]]]></description>
			<content:encoded><![CDATA[<p>Hey everyone, welcome to my first of many screencasts coming in the next few weeks. Today I am going to show you how to layout a UITableViewCell in Interface Builder and have a UITableView populate with those type of cells. I am adopting a new structure for my screencasts which will be 5 or so mintues of keynote slides giving background info followed by 20 &#8211; 25 mintues of step by step development. The entire video will be directly below this paragraph, but scrolling down you will see a text based step by step of the whole tutorial as well. Hope you guys enjoy.</p>
<h2><span style="color: #000000;">Skill Level</span> <span style="color: #ff6600;">MEDIUM</span></h2>
<p>Here is a link to the screencast to watch. We are working on getting an embedded version in, but I figure this is basically just as functional. Have fun!</p>
<h2><a href="http://eu-video-cloud.s3.amazonaws.com/available/114103480/asset-61/encoded_hidef.mp4">Custom UITableViewCell Screencast Video</a></h2>
<h1><span style="color: #ff6600;">Source Code<br />
</span></h1>
<h3><a title="Custom UITableViewCell's Using Interface Builder" href="http://losectrl-gaincommand.com/iCodeBlogHelper/Tutorial1/CustomUITableViewCell.zip"><span style="text-decoration: underline;">Available Here</span></a></h3>
<h2><span style="color: #008080;">Background Information</span></h2>
<p><img class="aligncenter size-full wp-image-814" src="/wp-content/uploads/2009/05/picture-1.png" alt="picture-1" width="622" height="388" /></p>
<p><img class="aligncenter size-full wp-image-815" src="/wp-content/uploads/2009/05/picture-2.png" alt="picture-2" width="622" height="388" /></p>
<p><img class="aligncenter size-full wp-image-816" src="/wp-content/uploads/2009/05/picture-3.png" alt="picture-3" width="622" height="388" /></p>
<p><img class="aligncenter size-full wp-image-817" src="/wp-content/uploads/2009/05/picture-4.png" alt="picture-4" width="622" height="388" /></p>
<p><img class="aligncenter size-full wp-image-818" src="/wp-content/uploads/2009/05/picture-5.png" alt="picture-5" width="622" height="388" /></p>
<p><img class="aligncenter size-full wp-image-819" src="/wp-content/uploads/2009/05/picture-6.png" alt="picture-6" width="622" height="388" /></p>
<p><img class="aligncenter size-full wp-image-820" src="/wp-content/uploads/2009/05/picture-7.png" alt="picture-7" width="622" height="388" /></p>
<p><img class="aligncenter size-full wp-image-821" src="/wp-content/uploads/2009/05/picture-8.png" alt="picture-8" width="622" height="388" /></p>
<p><img class="aligncenter size-full wp-image-822" src="/wp-content/uploads/2009/05/picture-9.png" alt="picture-9" width="622" height="388" /></p>
<p><img class="aligncenter size-full wp-image-823" src="/wp-content/uploads/2009/05/picture-10.png" alt="picture-10" width="622" height="388" /></p>
<p><img class="aligncenter size-full wp-image-824" src="/wp-content/uploads/2009/05/picture-11.png" alt="picture-11" width="622" height="388" /></p>
<h2><span style="color: #008080;">Building The App</span></h2>
<h3><span style="color: #008080;"><span style="color: #993300;">Step 1</span></span></h3>
<p><img class="aligncenter size-full wp-image-825" src="/wp-content/uploads/2009/05/picture-12.png" alt="picture-12" width="622" height="388" /></p>
<p style="padding-left: 60px;"><span style="color: #008080;"><span style="color: #993300;"><span style="color: #000000;">This step shouldn&#8217;t require any extra information.</span></span></span></p>
<h3><span style="color: #008080;"><span style="color: #993300;">Step 2</span></span></h3>
<p><img class="aligncenter size-full wp-image-826" src="/wp-content/uploads/2009/05/picture-13.png" alt="picture-13" width="622" height="388" /></p>
<p><img class="aligncenter size-full wp-image-836" src="/wp-content/uploads/2009/05/datasourceconnection.png" alt="datasourceconnection" width="622" height="377" /></p>
<h3><span style="color: #008080;"><span style="color: #993300;">Step 3</span></span></h3>
<p><img class="aligncenter size-full wp-image-827" src="/wp-content/uploads/2009/05/picture-14.png" alt="picture-14" width="622" height="388" /></p>
<p style="padding-left: 30px;">In CustomTableCellTutorialViewController.m you must define the two required UITableViewDataSource methods. These methods will fill up the table view with data. For now we will put in dummy data just to make sure all of our connections are working.</p>
<p style="padding-left: 30px;">- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section</p>
<p style="padding-left: 30px;">{</p>
<p style="padding-left: 60px;">return 10;</p>
<p style="padding-left: 30px;">}</p>
<p style="padding-left: 30px;">- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath</p>
<p style="padding-left: 30px;">{</p>
<p style="padding-left: 60px;">static NSString *CellIdentifier = @&#8221;Cell&#8221;;</p>
<p style="padding-left: 60px;">UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];</p>
<p style="padding-left: 60px;">if (cell == nil){</p>
<p style="padding-left: 90px;">cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];</p>
<p style="padding-left: 60px;">}</p>
<p style="padding-left: 60px;">[cell setText:[NSString stringWithFormat:@"I am cell %d", indexPath.row]];</p>
<p style="padding-left: 60px;">return cell;</p>
<p style="padding-left: 30px;">}</p>
<h3><span style="color: #008080;"><span style="color: #993300;">Step 4</span></span></h3>
<p><img class="aligncenter size-full wp-image-828" src="/wp-content/uploads/2009/05/picture-15.png" alt="picture-15" width="622" height="388" /></p>
<p style="padding-left: 30px;">Here you will need to be in xCode and go to File -&gt; New File&#8230;</p>
<p style="padding-left: 30px;">Select Objective C Class and make sure it is a UITableViewCell subclass, depending on your version of the SDK selecting this will differ. Look around and you will find it, call it iCodeBlogCustomCell. With this done enter these IBOutlets in the iCodeBlogCustomCell.h file enter the following IBOutlets:</p>
<p style="padding-left: 30px;">IBOutlet UILabel *articleName;<br />
IBOutlet UILabel *authorName;<br />
IBOutlet UILabel *date;</p>
<p>IBOutlet UIImageView *imageView;<br />
IBOutlet UIView *viewForBackground;</p>
<p style="padding-left: 30px;">Add the @property and synthesize them in the main.</p>
<h3><span style="color: #008080;"><span style="color: #993300;">Step 5</span></span></h3>
<p><img class="aligncenter size-full wp-image-829" src="/wp-content/uploads/2009/05/picture-16.png" alt="picture-16" width="622" height="388" /></p>
<p style="padding-left: 30px;">This step does not require and code but does require a lot of work in Interface Builder. I highly recommend you watch the screencast to see the step by step procedure here. Essentially what I do is create a new View XIB file. Opening this, I delete the standard UIView in the XIB and drag a UITableViewCell from my library into my document window. I assign the UITableViewCell to be of type iCodeBlogCustomCell. With this done layout the interface with the proper elements and hook them up by right clicking on the UITableViewCell in the document window.</p>
<h3><span style="color: #008080;"><span style="color: #993300;">Step 6<br />
</span></span></h3>
<p><img class="aligncenter size-full wp-image-830" src="/wp-content/uploads/2009/05/picture-17.png" alt="picture-17" width="622" height="388" /></p>
<p style="padding-left: 30px;">This is where the real magic is. We are going to return to CustomTableCellTutorialViewController.m and edit the UITableViewDataSource methods we implemented earlier. The code I use has me putting in 4 separate PNG files that I add to my project. You can find your own to put inside the cells. Make sure the UIImageView inside the cell is set for Aspect Fit so you don&#8217;t have to worry about resizing the images.  The functions should be changed to be:</p>
<p style="padding-left: 30px;">- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section<br />
{<br />
return 100;<br />
}</p>
<p style="padding-left: 30px;">- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath<br />
{<br />
static NSString *CellIdentifier = @&#8221;iCodeBlogCustomCell&#8221;;</p>
<p style="padding-left: 60px;">iCodeBlogCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];</p>
<p style="padding-left: 60px;">if (cell == nil){<br />
NSLog(@&#8221;New Cell Made&#8221;);</p>
<p style="padding-left: 60px;">NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@&#8221;iCodeBlogCustomCell&#8221; owner:nil options:nil];</p>
<p style="padding-left: 60px;">for(id currentObject in topLevelObjects)<br />
{<br />
if([currentObject isKindOfClass:[iCodeBlogCustomCell class]])<br />
{<br />
cell = (iCodeBlogCustomCell *)currentObject;<br />
break;<br />
}<br />
}<br />
}</p>
<p style="padding-left: 60px;">
<p style="padding-left: 60px;">if(indexPath.row % 4 == 0)<br />
{<br />
[[cell authorName] setText:@&#8221;Collin Ruffenach&#8221;];<br />
[[cell articleName] setText:@&#8221;Test Article 1&#8243;];<br />
[[cell date] setText:@&#8221;May 5th, 2009&#8243;];<br />
[[cell imageView] setImage:[UIImage imageNamed:@"1.png"]];<br />
}</p>
<p style="padding-left: 60px;">else if(indexPath.row % 4 == 1)<br />
{<br />
[[cell authorName] setText:@&#8221;Steve Jobs&#8221;];<br />
[[cell articleName] setText:@&#8221;Why iPhone will rule the world&#8221;];<br />
[[cell date] setText:@&#8221;May 5th, 2010&#8243;];<br />
[[cell imageView] setImage:[UIImage imageNamed:@"2.png"]];<br />
}</p>
<p style="padding-left: 60px;">else if(indexPath.row % 4 == 2)<br />
{<br />
[[cell authorName] setText:@&#8221;The Woz&#8221;];<br />
[[cell articleName] setText:@&#8221;Why I&#8217;m coming back to Apple&#8221;];<br />
[[cell date] setText:@&#8221;May 5th, 2012&#8243;];<br />
[[cell imageView] setImage:[UIImage imageNamed:@"3.png"]];<br />
}</p>
<p style="padding-left: 60px;">else if(indexPath.row % 4 == 3)</p>
<p style="padding-left: 60px;">{<br />
[[cell authorName] setText:@&#8221;Aaron Hillegass&#8221;];<br />
[[cell articleName] setText:@&#8221;Cocoa: A Brief Introduction&#8221;];<br />
[[cell date] setText:@&#8221;May 5th, 2004&#8243;];<br />
[[cell imageView] setImage:[UIImage imageNamed:@"4.png"]];</p>
<p style="padding-left: 60px;">}</p>
<p style="padding-left: 30px;">return cell;<br />
}</p>
<h3><span style="color: #008080;"><span style="color: #993300;">The End</span></span></h3>
<p style="padding-left: 30px;">So that is it for my first new post. I will be doing many more. Let me know your thoughts on this format in the comments. If you see anything organization wise that you think should be changed/add/removed let me know. Good Luck!</p>
<p style="padding-left: 30px;">
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2009/05/24/custom-uitableviewcell-using-interface-builder/feed/</wfw:commentRss>
		<slash:comments>60</slash:comments>
		</item>
		<item>
		<title>iPhone Coding &#8211; Learning About UIWebViews by Creating a Web Browser</title>
		<link>http://icodeblog.com/2008/12/19/iphone-coding-learning-about-uiwebviews-by-creating-a-web-browser/</link>
		<comments>http://icodeblog.com/2008/12/19/iphone-coding-learning-about-uiwebviews-by-creating-a-web-browser/#comments</comments>
		<pubDate>Fri, 19 Dec 2008 21:54:30 +0000</pubDate>
		<dc:creator>brandontreb</dc:creator>
				<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[iPhone Coding]]></category>
		<category><![CDATA[uiwebview]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=549</guid>
		<description><![CDATA[<a href="/2008/12/19/iphone-coding-learning-about-uiwebviews-by-creating-a-web-browser/"><img align="left" hspace="5" width="150" height="150" src="/wp-content/uploads/2008/12/screenshot_02-150x150.png" class="alignleft wp-post-image tfe" alt="screenshot_02" title="screenshot_02" /></a>Wow! It has been a long time since my last tutorial&#8230; As I wrote in my last post, I had to take a break due to my wife having our baby.  But, now I&#8217;m back and have a great tutorial for you guys.  
Today I will be showing you how to work with a  UIWebview [...]]]></description>
			<content:encoded><![CDATA[<p>Wow! It has been a long time since my last tutorial&#8230; As I wrote in my last post, I had to take a break due to my wife having our baby.  But, now I&#8217;m back and have a great tutorial for you guys.  </p>
<p>Today I will be showing you how to work with a  UIWebview to create a basic web browser.  Here is a screenshot of the app we are going to create.</p>
<p style="text-align: center; "><img class="size-full wp-image-550 aligncenter" title="screenshot_02" src="/wp-content/uploads/2008/12/screenshot_02.png" alt="screenshot_02" width="386" height="742" /></p>
<h2>Create a View-Based Application</h2>
<p style="text-align: left; ">Ok, so let&#8217;s get started.  Start by opening up Xcode and click <strong>File -&gt; New Project</strong>.  Next select <strong>View-Based Application</strong> and click <strong>Choose&#8230; </strong>Name this project something like <strong>iCodeBrowser</strong> and click <strong>Save</strong>.  </p>
<p style="text-align: center; "><img class="alignnone size-full wp-image-552" title="screenshot_03" src="/wp-content/uploads/2008/12/screenshot_03.png" alt="screenshot_03" width="600" height="442" /></p>
<p style="text-align: center; "><img class="alignnone size-full wp-image-551" title="screenshot_04" src="/wp-content/uploads/2008/12/screenshot_04.png" alt="screenshot_04" width="518" height="413" /></p>
<p style="text-align: left; ">Now we are ready to begin coding&#8230;</p>
<h2>Create IBOutlets and IBActions</h2>
<p>Before we create the interface for our web browser, we need to establish the IBOutles and Actions to interface with the UI elements in code.  Start by opening up iCodeBrowserViewController.h and add the following code:</p>
<p style="text-align: center; "><img class="alignnone size-full wp-image-554" title="screenshot_06" src="/wp-content/uploads/2008/12/screenshot_06.png" alt="screenshot_06" width="480" height="248" /></p>
<p style="text-align: left; ">Let&#8217;s take a look at this code line by line.  The first thing we added was the <strong>&lt;UIWebViewDelegate&gt;</strong> to the end of our interface declaration.  This is telling the app that this class will be the delegate for our UIWebview.  </p>
<p style="text-align: left; ">What does it mean to be the delegate you ask? Great question&#8230; A delegate is like a handler.  It is responsible for implementing certain methods in order to handle events sent by the object they are the delegate for.  So in our example, we are simply saying that we will implement some of the functionality of the UIWebView.  This is needed so we can capture certain actions of the UIWebView such as a click on a link or so we can tell when a page has started/finished loading.  If it&#8217;s still unclear, ask me clarifying questions in the comments section.</p>
<p style="text-align: left; ">Next, we see our 3 lines of declaring IBOutlets.  These are the UI elements that we will be interacting with.  In case you didn&#8217;t know, the UIActivityIndicator is the little spinner/loading animation that you see on various apps when content is loading.  We will be using this to show that a page is currently loading.</p>
<p style="text-align: left; ">Following this code, there are 3 IBActions.  IBActions are functions that get called in response to a user interaction with the application (such as tapping a button).  For our basic browser, we are only offering 3 types of functionality.  gotoAddress which will take a user to the address they type in the address bar and goBack/Forward should be pretty self explanatory.  </p>
<h2>Creating the User Interface </h2>
<p style="text-align: left; ">Now, let&#8217;s create the interface using Interface Builder.  I am going to be showing you how to do this in the video below.</p>
<p style="text-align: left; "> </p>
[See post to watch QuickTime movie]
<h2>Implementing the IBActions</h2>
<p>Now that we have our interface, let&#8217;s make the app function.  We need to implement our methods.  Open up <strong>iCodeBrowserViewController.m </strong>and add the following code.</p>
<p style="text-align: center; "><a href="/wp-content/uploads/2008/12/screenshot_01.png"><img class="alignnone size-full wp-image-570" title="screenshot_01" src="/wp-content/uploads/2008/12/screenshot_01.png" alt="screenshot_01" width="318" height="49" /></a></p>
<p style="text-align: left; ">We need to synthesize our properties to allow us to interact with them.  Synthesizing automatically creates &#8220;getter&#8221; and &#8220;setter&#8221; methods for our properties.  Next, let&#8217;s implement the viewDidLoad method.  This is where we will be loading our &#8220;homepage&#8221;.  Add the following code to the viewDidLoad method.</p>
<p style="text-align: center; "><img class="alignnone size-full wp-image-556" title="screenshot_08" src="/wp-content/uploads/2008/12/screenshot_08.png" alt="screenshot_08" width="396" height="159" /></p>
<p style="text-align: left; ">The viewDidLoad method gets called automatically by our application whenever this view first loads.  We can know for sure that it will get called, so we can put our initialization code here.</p>
<p style="text-align: left; ">The first thing we see is the urlAddress string.  This will be our &#8220;homepage&#8221;.   You can change this to any address you wish to start with.  Next, we build a URL object with our string.  We need to do this so we can make a web request.  Following this, we build our web request and load it into the webView.  This will display the homepage inside of our webview.  Finally, we set the text of the address bar to the homepage address.  This part is more for aesthetics to let the user know what page they are on.</p>
<p style="text-align: left; ">Next, we implement the method that we connected to the UITextField&#8217;s DidEndOnExit method gotoAddress.  Add the following code:</p>
<p style="text-align: center; "><img class="alignnone size-full wp-image-557" title="screenshot_09" src="/wp-content/uploads/2008/12/screenshot_09.png" alt="screenshot_09" width="397" height="118" /></p>
<p style="text-align: left; ">This is similar to the code we wrote in the viewDidLoad method, except for the fact that we are getting our URL string from the address bar.  This method gets called when the user presses the &#8220;Go&#8221; button on the keyboard.  The last thing to note here is we call the <strong>[addressBar resignFirstResponder]</strong> method.  This simply tells the app to hide the keyboard when this method gets called.</p>
<p style="text-align: left; ">The implementation of our <strong>Back </strong>and <strong>Forward </strong>methods are pretty easy.  Go ahead and add the following code.</p>
<p style="text-align: center; "><img class="alignnone size-full wp-image-558" title="screenshot_10" src="/wp-content/uploads/2008/12/screenshot_10.png" alt="screenshot_10" width="215" height="103" /></p>
<p style="text-align: left; ">UIWebViews are pretty cool because of the functionality they offer us built right in to them.  We simply call <strong>[webView goBAck]</strong> to go back and <strong>[webView goForward]</strong> to go forward.  This greatly simplifies the interactions with the webview.  If we were to code that functionality from scratch, we would have to create a stack of URLs and continually push and pop them off the stack to keep track of where we need to go.  Thanks Apple for not making us implement this.</p>
<p style="text-align: left; ">Finally, we need to implement the delegate methods for UIWebview.  These methods allow us to write our own code to respond to actions by the UIWebview.  The first methods we will implement are the <strong>webViewDidStartLoad</strong> and the <strong>webViewDidFinishLoad</strong> methods.  We will use these to show and hide the activity indicator. Add the following code:</p>
<p style="text-align: center; "><img class="alignnone size-full wp-image-559" title="screenshot_11" src="/wp-content/uploads/2008/12/screenshot_11.png" alt="screenshot_11" width="317" height="114" /></p>
<p style="text-align: left; ">So when the request is first made for a ULR (before the page starts loading) the <strong>webViewDidStartLoad</strong> method gets called automatically.  We use this opportunity to start our activity indicator to let the user know the page is loading.  If you don&#8217;t have something like this, it simply feels like the app is frozen when in fact, it&#8217;s just loading the page.  Finally, the <strong>webViewDidFinishLoad</strong> method gets called when the page is fully loaded.  After this, we can stop the indicator (and it automatically hides itself).</p>
<p style="text-align: left; ">The very last thing we need to do is define what happens when a user clicks on a link. Add the following method:</p>
<p style="text-align: center; "><img class="alignnone size-full wp-image-561" title="screenshot_13" src="/wp-content/uploads/2008/12/screenshot_13.png" alt="screenshot_13" width="526" height="194" /></p>
<p style="text-align: left; ">This method gets called automatically whenever the user clicks a link.  This method can be very useful if you want to make a native iPhone application that integrates with a web app.  You can use this method to trap the user&#8217;s clicks and have your application respond to web links that get clicked.  In our case, we need it to do 2 things.  The first is to set the text of the address bar to the URL of the link that was clicked on and to load that address into the webview.</p>
<p style="text-align: left; ">One thing to make note of: We do a check to see if the URL scheme is &#8220;http&#8221;. This is to ensure that the user typed http before their URL.  You can add an else statement here that auto prepends the http if the user did not add it.  This would allow you to type in a url such as &#8220;icodeblog.com&#8221; rather than having to type &#8220;http://icodeblog.com&#8221;.  I chose to omit it for this tutorial.</p>
<p style="text-align: left; ">Remember, all of this added functionality of a UIWebView can only be gotten if you tell your class that it implements the UIWebViewDelegate protocol as we did in our .h file.</p>
<p style="text-align: left; ">The app should be complete! Click on Build and Go to see this baby in action.  Remember, you must put &#8220;http://&#8221; in front of your URL&#8217;s.  </p>
<p style="text-align: left; ">I hope you have enjoyed this tutorial.  If you have any questions or comments, feel free to leave them in the comments section of this post.  You can download the source <a href="/wp-content/uploads/2008/12/icodebrowser.zip">here</a> . Happy iCoding!</p>
<p style="text-align: left; "> </p>
<p style="text-align: left; "> </p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2008/12/19/iphone-coding-learning-about-uiwebviews-by-creating-a-web-browser/feed/</wfw:commentRss>
		<slash:comments>67</slash:comments>
<enclosure url="http://icodeblog.com/wp-content/uploads/2008/12/UIWebView.mov" length="10646044" type="video/quicktime" />
		</item>
		<item>
		<title>iPhone Programming Tutorial &#8211; Animating a Ball Using An NSTimer</title>
		<link>http://icodeblog.com/2008/10/28/iphone-programming-tutorial-animating-a-ball-using-an-nstimer/</link>
		<comments>http://icodeblog.com/2008/10/28/iphone-programming-tutorial-animating-a-ball-using-an-nstimer/#comments</comments>
		<pubDate>Tue, 28 Oct 2008 14:44:06 +0000</pubDate>
		<dc:creator>brandontreb</dc:creator>
				<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[iphone programming]]></category>
		<category><![CDATA[iphone tutorial]]></category>
		<category><![CDATA[NSTimer]]></category>
		<category><![CDATA[UIImageView]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=523</guid>
		<description><![CDATA[<a href="/2008/10/28/iphone-programming-tutorial-animating-a-ball-using-an-nstimer/"><img align="left" hspace="5" width="150" height="150" src="/wp-content/uploads/2008/10/intro-150x150.png" class="alignleft wp-post-image tfe" alt="" title="intro" /></a>I have noticed recently many people wanting to create games for the iPhone and are unsure where to start.  A common misconception is that you must use OpenGL ES to create any game with graphics.  I am going to show you today how you can do some simple animation by moving a ball around the [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-medium wp-image-529" style="display:none;" title="intro" src="/wp-content/uploads/2008/10/intro-300x187.png" alt="" width="300" height="187" />I have noticed recently many people wanting to create games for the iPhone and are unsure where to start.  A common misconception is that you must use OpenGL ES to create any game with graphics.  I am going to show you today how you can do some simple animation by moving a ball around the screen.  We will not be using OpenGL ES to move the ball.  We will simply be using an NSTimer and a UIImageView.</p>
<p>The code for this could be used in many game applications.  Games such as pong, brick breaker, etc&#8230; I will soon do an iPhone game programming tutorial series.  For now, here is a quick tutorial to get you started and excited&#8230;</p>
<p>You will need to download this image for this tutorial:</p>
<p><a href="/wp-content/uploads/2008/10/ball.png"><img class="alignnone size-full wp-image-525" title="ball" src="/wp-content/uploads/2008/10/ball.png" alt="" width="32" height="32" /></a></p>
[See post to watch QuickTime movie]
<p>Here are the steps involved in creating this application:</p>
<h2>Start With a View-Based Application</h2>
<p>We will not need any navigation components for this app.  All we need is a view that we can add a UIImageView to.</p>
<h2>Add The Ball Image To Your Project</h2>
<p>You can use any image you would like for this.  I used my mad skills in Photoshop to create a shiny red ball for you to use.  This will be the image that we will be &#8220;bouncing&#8221; around the screen.</p>
<h2>Add An IBOutlet For the Ball</h2>
<p>This step is necessary as it allows us the link the ball we add to the view in Interface Builder to the code.  This is so we can update the position of the ball at each timestep.</p>
<h2>Add The Ball To The View In Interface Builder</h2>
<p>All we need to draw the ball on the screen is a UIImageView.  Since we added the ball.png file to our project, it should appear in the drop-down in the attributes section of the UIImageView.  Also, at this step, we need to connect the UIImageView to the &#8216;ball&#8217; variable by dragging from &#8216;new referencing outlet&#8217; in the connection inspector of the UIImageView to the &#8216;File&#8217;s Owner&#8217; object.</p>
<h2>Add The Code To Initialize The NSTimer</h2>
<p>An NSTimer is very simple to use.  It simple requires that you specify an interval (I used .05) and a function to call at each time-step.  The function we are calling in this tutorial is the onTimer function.  This is a function that we create and will contain the logic to move the ball.</p>
<h2>Moving The Ball</h2>
<p>This code is all fairly straight forward.  First we add the pos.x and pos.y values (14.0 and 7.0) to the center of the ball object this causes the ball to move diagonally.  Next we simply check to see if the ball has collided with the wall.  If the ball does collide with the wall, we simple reverse the pos.x or the pos.y.</p>
<p>That concludes the animation tutorial.  I hope that you go out an make lots of great games after this&#8230; Soon, I will start another tutorial series where I we will be creating a full blown game.  If you have any suggestions for games, please leave them in the comments.  Make sure that they are fun but not too in depth, we need to limit the series.  Think in terms of pong, asteroids, etc&#8230;</p>
<p>You can download the source code <a href="/wp-content/uploads/2008/10/ball.zip">here</a> .  If you have any comments or questions, feel free to leave them in the comments of this post.  You may also post them in the <a href="/forum">forums</a>. Happy iCoding!</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2008/10/28/iphone-programming-tutorial-animating-a-ball-using-an-nstimer/feed/</wfw:commentRss>
		<slash:comments>74</slash:comments>
<enclosure url="http://icodeblog.com/wp-content/uploads/2008/10/Animation.mov" length="19459984" type="video/quicktime" />
		</item>
		<item>
		<title>iPhone Programming Tutorial &#8211; Using A TabBarView To Switch Between Views</title>
		<link>http://icodeblog.com/2008/10/13/iphone-programming-tutorial-using-tabbarview-to-switch-between-views/</link>
		<comments>http://icodeblog.com/2008/10/13/iphone-programming-tutorial-using-tabbarview-to-switch-between-views/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 08:00:30 +0000</pubDate>
		<dc:creator>cruffenach</dc:creator>
				<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[iPhone app tutorial]]></category>
		<category><![CDATA[iphone dev]]></category>
		<category><![CDATA[UITabBarView]]></category>
		<category><![CDATA[uitableview]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=474</guid>
		<description><![CDATA[<a href="/2008/10/13/iphone-programming-tutorial-using-tabbarview-to-switch-between-views/"><img align="left" hspace="5" width="150" height="150" src="/wp-content/uploads/2008/10/screenshot_031-150x150.png" class="alignleft wp-post-image tfe" alt="" title="screenshot_031" /></a>

In this tutorial I will show you how to develop a UITabBarController which contains a custom UIView from one of the tabs and a UINavigationController with a UITableView dictated byUISegmentControl in the second tab.
This tutorial was contributed by the user cruffenach.  You can check out his website at http://losectrl-gaincommand.com.
If you would like to contribute a tutorial to iCodeBlog.com, contact me brandon@icodeblog.com
[See post to watch QuickTime [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-medium wp-image-490" style="display:none;" title="screenshot_031" src="/wp-content/uploads/2008/10/screenshot_031-300x189.png" alt="" width="300" height="189" /></p>
<p><script type="text/javascript"><!--
digg_url = 'http://digg.com/apple/iPhone_Programming_Tutorial_Using_A_TabBarView';
// --></script><script src="http://digg.com/api/diggthis.js"></script></p>
<p>In this tutorial I will show you how to develop a UITabBarController which contains a custom UIView from one of the tabs and a UINavigationController with a UITableView dictated byUISegmentControl in the second tab.</p>
<p>This tutorial was contributed by the user <a href="http://losectrl-gaincommand.com">cruffenach</a>.  You can check out his website at <a href="http://losectrl-gaincommand.com/" target="_blank">http://losectrl-gaincommand.com.</a></p>
<p>If you would like to contribute a tutorial to iCodeBlog.com, contact me <a href="mailto:brandon@icodeblog.com">brandon@icodeblog.com</a><br />
[See post to watch QuickTime movie]
<p>The final product of this tutorial should look something like this:</p>
<p style="text-align: center; "><a href="/wp-content/uploads/2008/10/screenshot_011.png"><img class="alignnone size-full wp-image-488" title="screenshot_011" src="/wp-content/uploads/2008/10/screenshot_011.png" alt="" width="253" height="469" /></a></p>
<p>Setting up the User Interface</p>
<p>The ﬁrst thing we need to do is open up MainWindow.xib. This will launch interface builder and show what our UITabBarController looks like. In the ﬁrst tab we see a custom UIView that can be changed however we like, but we are more concerned about the second tab.</p>
<p>Clicking on the TabBarController within the instance window of interface builder and then looking to the Inspector will let us set the type of view controller which each tab will be displaying. The ﬁrst tab uses a ViewController, and for the second tab we want to use a Navigation Controller type because we will be utilizing the navigation bar to hold our segmented control.</p>
<p>Once in the second tab, we will drag a UISegmentedControl out of our library and onto the navigation bar. The UISegmentedControl will change its look depending on the context you place it in. Here we will essentially be making the TitleView of the NavigationItem a UISegmentedView. This will be important when we get into our code.</p>
<p>With that done, we click on the view of the second tab and set it to be identiﬁed as a SegmentedTableViewController or type UITableViewController. We will create that class later in xCode. With that set we drag in a UITableView and connect its DataSource and Delegate methods to our view controller which has now been deﬁned as SegmentedTableViewController. That is all we need to do in interface builder.</p>
<p>Going into xCode we do not need to modify any of the generated classes, however we do need to create a new one. Hit Apple + N and create a new UITableViewController called SegmentedTableViewController. This class will have the datasource methods for a UITableView all ready.</p>
<p>In this class we are going to need an int called selectedSegment, this will be updated to represent which segment is selected. Within the main we need to uncomment the viewDidLoad method and enter the following code.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/10/screenshot_01.png"><img class="alignnone size-full wp-image-475" title="screenshot_01" src="/wp-content/uploads/2008/10/screenshot_01.png" alt="" width="500" height="160" /></a></p>
<p style="text-align: left; ">
<p>This code gets the UISegmentedControl from the titleView of the navigationItem, and modiﬁes it to add an action so that when the selected segment changes a method called segmentAction is called. The selectedSegment int is set to the initially selected segment and the titleView is reset with the modiﬁed UISegmentedControl.</p>
<p>Next we need to create the method which the UISegmentedControl will call. Here is the code for that method, following the same syntax as a IBAction Method.</p>
<p style="text-align: center; "><a href="/wp-content/uploads/2008/10/screenshot_02.png"><img class="alignnone size-full wp-image-476" title="screenshot_02" src="/wp-content/uploads/2008/10/screenshot_02.png" alt="" width="443" height="127" /></a></p>
<p style="text-align: left; ">
<p>This code takes the sender which will be out UISegmentedControl and gets the new selectedSegment from it and sets out variable to that value. Finally it tells the UITableView that something has happened and the data in the table needs to be reloaded.</p>
<p>The ﬁnal steps is to tell the UITableView that is will be displaying 100 cells and telling the returned cell for each row to have some text which is a product of the selected segment. I chose to have each cell read “I AM CELL (selectedSegment * indexPath.row)”. This essentially means that when segment 0 is selected every cell will read.</p>
<p>I AM CELL 0</p>
<p>When segment 1 is selected the cells will read.</p>
<p>I AM CELL 0</p>
<p>I AM CELL 1</p>
<p>I AM CELL 2</p>
<p>&#8230;.</p>
<p>When segment 2 is selected the cells will read.</p>
<p>I AM CELL 0</p>
<p>I AM CELL 2</p>
<p>I AM CELL 4</p>
<p>I AM CELL 6</p>
<p>&#8230;.segment. I chose to</p>
<p>The code to make this happens is&#8230;</p>
<p style="text-align: center; "><a href="/wp-content/uploads/2008/10/screenshot_03.png"><img class="alignnone size-full wp-image-477" title="screenshot_03" src="/wp-content/uploads/2008/10/screenshot_03.png" alt="" width="500" height="345" /></a></p>
<p style="text-align: left;">You can download the source for this tutorial <a href="/wp-content/uploads/2008/10/icodesegmentcontrol.zip">here. icodesegmentcontrol</a></p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2008/10/13/iphone-programming-tutorial-using-tabbarview-to-switch-between-views/feed/</wfw:commentRss>
		<slash:comments>47</slash:comments>
<enclosure url="http://icodeblog.com/wp-content/uploads/2008/10/segmentedTableView.mov" length="67818395" type="video/quicktime" />
		</item>
		<item>
		<title>UITabBar iPhone Tutorial</title>
		<link>http://icodeblog.com/2008/09/28/uitabbar-iphone-tutorial/</link>
		<comments>http://icodeblog.com/2008/09/28/uitabbar-iphone-tutorial/#comments</comments>
		<pubDate>Sun, 28 Sep 2008 21:06:43 +0000</pubDate>
		<dc:creator>brandontreb</dc:creator>
				<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[iPhone apps]]></category>
		<category><![CDATA[iphone programming]]></category>
		<category><![CDATA[UITabBar]]></category>
		<category><![CDATA[UITabBarItem]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=449</guid>
		<description><![CDATA[<a href="/2008/09/28/uitabbar-iphone-tutorial/"><img align="left" hspace="5" width="150" height="150" src="/wp-content/uploads/2008/09/sh-150x150.png" class="alignleft wp-post-image tfe" alt="" title="Screenshot" /></a>

[See post to watch QuickTime movie]
This tutorial will show you the basics of using the UITabBar and UITabBarItem controls when programming for the iPhone.
I will be showing you step by step in the video how to create a project from a UITabBar template and add additional UITabBarItems as well as additonal views to be displayed [...]]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript"><!--
digg_url = 'http://digg.com/apple/UITabBar_iPhone_Programming_Tutorial';
// --></script><script src="http://digg.com/api/diggthis.js"></script><br />
<br />
[See post to watch QuickTime movie]
<p>This tutorial will show you the basics of using the <strong>UITabBar</strong> and <strong>UITabBarItem</strong> controls when programming for the iPhone.</p>
<p>I will be showing you step by step in the video how to create a project from a <strong>UITabBar</strong> template and add additional <strong>UITabBarItems</strong> as well as additonal views to be displayed with these items.  Here is a brief explanation of each step:</p>
<h3>1. Create a new project from a UITabBar template</h3>
<p>This is pretty straight forward.  We will be using Apple&#8217;s default UITabBar template to start our project.  This is a fully functioning app.  You can click Build and Go to see it run.</p>
<h3>2. Open Interface Builder (click on mainWindow.xib) and Add addtional UITabBar items</h3>
<p>To add additonal UITabBarItems, you simply drag them from the library on to the UITabBar.  For each new item, you must specify the name of the view that will be shown when the item is clicked.  This is done by clicking on the tab bar item and clicking Tools -&gt; attributes inspector.  There should be a box titled &#8220;Nib Name&#8221;.  Whatever you enter here will be the name of the view that will be loaded when this tab is clicked.</p>
<p>So for this tutorial, we entered ThirdView.  So, naturally when we created our view to be loaded here, we saved it as &#8220;ThirdView.xib&#8221;.  This is one Gotcha when working with these components.</p>
<h3>3. Changing the text of the UITabBarItems</h3>
<p>This is took me a while to figure out.  To change the text, image, or style of the UITabBarItem, double click on it.  You should now be able to access these settings in the Attributes Inspector.</p>
<h3>4. Set the Background Color For Views 1 and 2</h3>
<p>This was pretty easy.  For the first view, we simply clicked on it and clicked the box next to &#8220;Background&#8221; in the Attributes Inspector.  Next, we did the same to the secondView by opening up SecondView.xib in Interface Builder.</p>
<h3>5. Create ThirdView</h3>
<p>We need to create the third view to be loaded when the user clicks on the UITabBarItem that we inserted.  This was done inside of Interface Builder by clicking File -&gt; new and selecting View. The next thing we did was drag a label from the library and add it to the view.  Finally, we set the background color like we did in the other views.</p>
<p>Be sure when you save this view, you name it ThirdView.xib (this corrisponds the name that we put in the &#8220;Nib Name&#8221; box in step 2).  Also, be sure to save this view in your projects folder (sometimes Interface Builder does not navigate here by default).</p>
<h3>6. Connecting ThirdView to the ViewController</h3>
<p>In this step we connect the ThirdView to our FirstViewController.  Notice that all of the views get connected to this view controller.  This will handle all of the UI components put on to each view. *Note: this step can&#8217;t be done until ThirdView is saved and added to the project.</p>
<p>So to connect this view to FirstViewController, simply click on the File&#8217;s Owner object and select FirstViewController from the drop-down in the Identity Inspector.</p>
<p>The last thing we need to do is connect the view.  So control-click from File&#8217;s Owner and drag the line to the View object. The word &#8220;View&#8221; should pop up.  Click on it and the views are now connected.</p>
<p>That should be it! Click Build and Go and you are done.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/09/sh.png"><img class="size-full wp-image-452 aligncenter" title="Screenshot" src="/wp-content/uploads/2008/09/sh.png" alt="" width="324" height="483" /></a></p>
<p style="text-align: left;">Well, I hope you guys have enjoyed the video tutorial.  I know that the audio is kind of low.  This is because I used my computer&#8217;s built in microphone and it&#8217;s not the greatest.  I have since purchased a new microphone (thanks again to all who have donated thus far) and future tutorials will have much better audio quality.  I would LOVE to hear your feedback on the videos.  I&#8217;m a total noob when it comes to screencasts and would love your <strong>constructive</strong> criticism.  Thank you all and Happy iCoding!</p>
<p style="text-align: left;">Download  the source code for this tutorial <a href="/wp-content/uploads/2008/09/tabbartutorial.zip">here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2008/09/28/uitabbar-iphone-tutorial/feed/</wfw:commentRss>
		<slash:comments>60</slash:comments>
<enclosure url="http://icodeblog.com/wp-content/uploads/2008/09/1-UITabView.mov" length="30720219" type="video/quicktime" />
		</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>Tue, 23 Sep 2008 00:47:39 +0000</pubDate>
		<dc:creator>brandontreb</dc:creator>
				<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[iphone programming]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[sqlite3]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=392</guid>
		<description><![CDATA[<a href="/2008/09/22/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-4/"><img align="left" hspace="5" width="150" height="150" src="/wp-content/uploads/2008/09/mainscreen-150x150.png" class="alignleft wp-post-image tfe" alt="" title="screenshot" /></a>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:

iPhone Programming Tutorial &#8211; Creating 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/mainscreen.png"><img class="size-full wp-image-394 aligncenter" title="screenshot" src="/wp-content/uploads/2008/09/mainscreen.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/viewdidload.png"><img class="size-full wp-image-396 aligncenter" title="viewdidload" src="/wp-content/uploads/2008/09/viewdidload.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/todo.png"><img class="alignnone size-full wp-image-398" title="todo" src="/wp-content/uploads/2008/09/todo.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/todomstatic.png"><img class="size-full wp-image-400 aligncenter" title="todomstatic" src="/wp-content/uploads/2008/09/todomstatic.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/todom3.png"><img class="size-full wp-image-399 aligncenter" title="todom3" src="/wp-content/uploads/2008/09/todom3.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/dehydrate.png"><img class="size-full wp-image-401 aligncenter" title="dehydrate" src="/wp-content/uploads/2008/09/dehydrate.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-rootviewcontroller.png"><img class="size-full wp-image-422 aligncenter" title="1-rootviewcontroller" src="/wp-content/uploads/2008/09/1-rootviewcontroller.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-appdelegateh.png"><img class="size-full wp-image-423 aligncenter" title="2-appdelegateh" src="/wp-content/uploads/2008/09/2-appdelegateh.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-appdelegatem.png"><img class="size-full wp-image-424 aligncenter" title="3-appdelegatem" src="/wp-content/uploads/2008/09/3-appdelegatem.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-todoviewcontrollerh.png"><img class="alignnone size-full wp-image-425" title="5-todoviewcontrollerh" src="/wp-content/uploads/2008/09/5-todoviewcontrollerh.png" alt="" width="430" height="271" /></a><a href="/wp-content/uploads/2008/09/5-todoviewcontrollerh.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-todoviewcontrollerm.png"><img class="size-full wp-image-426 aligncenter" title="5-todoviewcontrollerm" src="/wp-content/uploads/2008/09/5-todoviewcontrollerm.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-interfacebuilder.png"><img class="size-full wp-image-427 aligncenter" title="6-interfacebuilder" src="/wp-content/uploads/2008/09/6-interfacebuilder.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-ib.png"><img class="size-full wp-image-434 aligncenter" title="12-ib" src="/wp-content/uploads/2008/09/12-ib.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-appdelegateh.png"><img class="alignnone size-full wp-image-428" title="7-appdelegateh" src="/wp-content/uploads/2008/09/7-appdelegateh.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-appdelegatem.png"><img class="size-full wp-image-429 aligncenter" title="8-appdelegatem" src="/wp-content/uploads/2008/09/8-appdelegatem.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-todom.png"><img class="size-full wp-image-431 aligncenter" title="10-todom" src="/wp-content/uploads/2008/09/10-todom.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-rootviewcontroller1.png"><img class="size-full wp-image-432 aligncenter" title="11-rootviewcontroller1" src="/wp-content/uploads/2008/09/11-rootviewcontroller1.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-4.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>87</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 02:39:29 +0000</pubDate>
		<dc:creator>brandontreb</dc:creator>
				<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[SQLite]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[iphone programming]]></category>
		<category><![CDATA[todo]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[uitableview]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=339</guid>
		<description><![CDATA[<a href="/2008/09/10/iphone-programming-tutorial-creating-a-todo-list-using-sqlite-part-3/"><img align="left" hspace="5" width="150" height="150" src="/wp-content/uploads/2008/09/screenshot1-150x150.png" class="alignleft wp-post-image tfe" alt="" title="screenshot1" /></a>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.

iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 1
iPhone Programming Tutorial &#8211; Creating a ToDo List Using SQLite Part 2

The focus of this tutorial will mainly be on [...]]]></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/screenshot1.png"><img class="size-full wp-image-390 aligncenter" title="screenshot1" src="/wp-content/uploads/2008/09/screenshot1.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/todoh1.png"><img class="alignnone size-full wp-image-370" title="todoh1" src="/wp-content/uploads/2008/09/todoh1.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/synth1.png"><img class="alignnone size-full wp-image-382" title="synth1" src="/wp-content/uploads/2008/09/synth1.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/todom2.png"><img class="size-full wp-image-371 aligncenter" title="todom2" src="/wp-content/uploads/2008/09/todom2.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;&#8217;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-todom.png"><img class="size-full wp-image-357 aligncenter" title="00-todom" src="/wp-content/uploads/2008/09/00-todom.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/title.png"><img class="alignnone size-full wp-image-381" title="title" src="/wp-content/uploads/2008/09/title.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-control.png"><img class="alignnone size-full wp-image-340" title="segmented-control" src="/wp-content/uploads/2008/09/segmented-control.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/ibscreenshot.png"><img class="alignnone size-full wp-image-369" title="ibscreenshot" src="/wp-content/uploads/2008/09/ibscreenshot.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-save.png"><img class="size-full wp-image-343 aligncenter" title="2-save" src="/wp-content/uploads/2008/09/2-save.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-save.png"><img class="alignnone size-full wp-image-344" title="3-save" src="/wp-content/uploads/2008/09/3-save.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/14addviewcontrollerclass.png"><img class="size-full wp-image-212 aligncenter" title="14addviewcontrollerclass" src="/wp-content/uploads/2008/08/14addviewcontrollerclass.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-viewcontroller.png"><img class="size-full wp-image-345 aligncenter" title="4-viewcontroller" src="/wp-content/uploads/2008/09/4-viewcontroller.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-todoviewcontroller.png"><img class="alignnone size-full wp-image-359" title="04-todoviewcontroller" src="/wp-content/uploads/2008/09/04-todoviewcontroller.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/synthtodoviewcontroller1.png"><img class="alignnone size-full wp-image-364" title="synthtodoviewcontroller1" src="/wp-content/uploads/2008/09/synthtodoviewcontroller1.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/todoviewcontrollerm.png"><img class="size-full wp-image-368 aligncenter" title="todoviewcontrollerm" src="/wp-content/uploads/2008/09/todoviewcontrollerm.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-owner.png"><img class="size-full wp-image-349 aligncenter" title="07-files-owner" src="/wp-content/uploads/2008/09/07-files-owner.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/todoviewcontrollerfilesowner.png"><img class="alignnone size-full wp-image-380" title="todoviewcontrollerfilesowner" src="/wp-content/uploads/2008/09/todoviewcontrollerfilesowner.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-connect.png"><img class="size-full wp-image-350 aligncenter" title="08-view-connect" src="/wp-content/uploads/2008/09/08-view-connect.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/uibutton1.png"><img class="alignnone size-full wp-image-362" title="uibutton1" src="/wp-content/uploads/2008/09/uibutton1.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/uisegementedcontrol1.png"><img class="alignnone size-full wp-image-366" title="uisegementedcontrol1" src="/wp-content/uploads/2008/09/uisegementedcontrol1.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-rootviewcontroller.png"><img class="size-full wp-image-354 aligncenter" title="11-rootviewcontroller" src="/wp-content/uploads/2008/09/11-rootviewcontroller.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-synth.png"><img class="size-full wp-image-355 aligncenter" title="12-synth" src="/wp-content/uploads/2008/09/12-synth.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/rootviewcontrollermwillapprea.png"><img class="size-full wp-image-372 aligncenter" title="rootviewcontrollermwillapprea" src="/wp-content/uploads/2008/09/rootviewcontrollermwillapprea.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/didselectrowatindex.png"><img class="size-full wp-image-373 aligncenter" title="didselectrowatindex" src="/wp-content/uploads/2008/09/didselectrowatindex.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/applicationwillterminate.png"><img class="size-full wp-image-374 aligncenter" title="applicationwillterminate" src="/wp-content/uploads/2008/09/applicationwillterminate.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/screenshot1.png"><img class="size-full wp-image-390 aligncenter" title="screenshot1" src="/wp-content/uploads/2008/09/screenshot1.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-3.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>50</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 19:29:44 +0000</pubDate>
		<dc:creator>brandontreb</dc:creator>
				<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></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[<a href="/2008/08/08/iphone-programming-tutorial-populating-uitableview-with-an-nsarray/"><img align="left" hspace="5" width="150" height="150" src="/wp-content/uploads/2008/08/01navigationbased-150x150.png" class="alignleft wp-post-image tfe" alt="" title="01navigationbased" /></a>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 [...]]]></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/01navigationbased.png"><img class="alignnone size-full wp-image-198" title="01navigationbased" src="/wp-content/uploads/2008/08/01navigationbased.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/02newobject.png"><img class="size-full wp-image-200 aligncenter" title="02newobject" src="/wp-content/uploads/2008/08/02newobject.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/03newobjecttoproject.png"><img class="size-full wp-image-201 aligncenter" title="03newobjecttoproject" src="/wp-content/uploads/2008/08/03newobjecttoproject.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/04fruith.png"><img class="size-full wp-image-202 aligncenter" title="04fruith" src="/wp-content/uploads/2008/08/04fruith.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/05fruitm.png"><img class="size-full wp-image-203 aligncenter" title="05fruitm" src="/wp-content/uploads/2008/08/05fruitm.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/01otitle.png"><img class="alignnone size-full wp-image-199" title="01otitle" src="/wp-content/uploads/2008/08/01otitle.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/06fruitappdelegateh.png"><img class="size-full wp-image-204 aligncenter" title="06fruitappdelegateh" src="/wp-content/uploads/2008/08/06fruitappdelegateh.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/07synthfruits.png"><img class="size-full wp-image-205 aligncenter" title="07synthfruits" src="/wp-content/uploads/2008/08/07synthfruits.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/08initfruits.png"><img class="size-full wp-image-206 aligncenter" title="08initfruits" src="/wp-content/uploads/2008/08/08initfruits.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/09interfacebuilder.png"><img class="alignnone size-full wp-image-207" title="09interfacebuilder" src="/wp-content/uploads/2008/08/09interfacebuilder.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/10addview.png"><img class="size-full wp-image-208 aligncenter" title="10addview" src="/wp-content/uploads/2008/08/10addview.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/11uitextview.png"><img class="size-full wp-image-209 aligncenter" title="11uitextview" src="/wp-content/uploads/2008/08/11uitextview.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/12saveview.png"><img class="size-full wp-image-210 aligncenter" title="12saveview" src="/wp-content/uploads/2008/08/12saveview.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/13addviewtoproject.png"><img class="size-full wp-image-211 aligncenter" title="13addviewtoproject" src="/wp-content/uploads/2008/08/13addviewtoproject.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/14addviewcontrollerclass.png"><img class="size-full wp-image-212 aligncenter" title="14addviewcontrollerclass" src="/wp-content/uploads/2008/08/14addviewcontrollerclass.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/15addtoproject.png"><img class="size-full wp-image-214 aligncenter" title="15addtoproject" src="/wp-content/uploads/2008/08/15addtoproject.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/14afruitviewcontroller.png"></a></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/14afruitviewcontroller.png"><img class="size-full wp-image-213 aligncenter" title="14afruitviewcontroller" src="/wp-content/uploads/2008/08/14afruitviewcontroller.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/20fruitviewcontrollerm.png"><img class="alignnone size-full wp-image-224" title="20fruitviewcontrollerm" src="/wp-content/uploads/2008/08/20fruitviewcontrollerm.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/16clickfilesowner.png"><img class="size-full wp-image-215 aligncenter" title="16clickfilesowner" src="/wp-content/uploads/2008/08/16clickfilesowner.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/17setclass.png"><img class="size-full wp-image-216 aligncenter" title="17setclass" src="/wp-content/uploads/2008/08/17setclass.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/18makeconnections.png"><img class="size-full wp-image-223 alignnone" title="18makeconnections" src="/wp-content/uploads/2008/08/18makeconnections.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/21rootviewcontrollerh.png"><img class="size-full wp-image-225 aligncenter" title="21rootviewcontrollerh" src="/wp-content/uploads/2008/08/21rootviewcontrollerh.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/22numberofrows.png"><img class="size-full wp-image-226 aligncenter" title="22numberofrows" src="/wp-content/uploads/2008/08/22numberofrows.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/23cellforrowatindex1.png"><img class="size-full wp-image-250 aligncenter" title="23cellforrowatindex1" src="/wp-content/uploads/2008/08/23cellforrowatindex1.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;"> </p>
<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/24didselectrow.png"><img class="size-full wp-image-228 aligncenter" title="24didselectrow" src="/wp-content/uploads/2008/08/24didselectrow.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/25screenshot1.png"><img class="size-full wp-image-229 aligncenter" title="25screenshot1" src="/wp-content/uploads/2008/08/25screenshot1.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/26screenshot2.png"><img class="size-full wp-image-230 aligncenter" title="26screenshot2" src="/wp-content/uploads/2008/08/26screenshot2.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/fruit1.zip">here</a><a href="/wp-content/uploads/2008/08/fruit.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>251</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 23:41:33 +0000</pubDate>
		<dc:creator>brandontreb</dc:creator>
				<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[iPhone Programming 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[<a href="/2008/08/03/iphone-programming-tutorial-transitioning-between-views/"><img align="left" hspace="5" width="150" src="/wp-content/uploads/2008/08/01-nextview.png" class="alignleft wp-post-image tfe" alt="" title="01-nextview" /></a>
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;Hello World&#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 [...]]]></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:@"Hello World"] ; </strong>to <strong>[cell setText:@"Next View"];</strong></p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/08/01-nextview.png"><img class="alignnone size-full wp-image-140" title="01-nextview" src="/wp-content/uploads/2008/08/01-nextview.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/02addnewview.png"><img class="size-full wp-image-141 aligncenter" title="02addnewview" src="/wp-content/uploads/2008/08/02addnewview.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/035view.png"><img class="size-full wp-image-155 aligncenter" title="035view" src="/wp-content/uploads/2008/08/035view.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/03saveview.png"><img class="size-full wp-image-142 aligncenter" title="03saveview" src="/wp-content/uploads/2008/08/03saveview.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/04addviewtoproject.png"><img class="size-full wp-image-143 aligncenter" title="04addviewtoproject" src="/wp-content/uploads/2008/08/04addviewtoproject.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/05addviewcontroller.png"><img class="size-full wp-image-144 aligncenter" title="05addviewcontroller" src="/wp-content/uploads/2008/08/05addviewcontroller.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/06nameviewcontroller.png"><img class="size-full wp-image-145 aligncenter" title="06nameviewcontroller" src="/wp-content/uploads/2008/08/06nameviewcontroller.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/07viewcontrollerh.png"><img class="size-full wp-image-146 aligncenter" title="07viewcontrollerh" src="/wp-content/uploads/2008/08/07viewcontrollerh.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/07viewcontroller1.png"><img class="size-full wp-image-159 aligncenter" title="07viewcontroller1" src="/wp-content/uploads/2008/08/07viewcontroller1.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/08viewcontrollerm.png"><img class="alignnone size-full wp-image-147" title="08viewcontrollerm" src="/wp-content/uploads/2008/08/08viewcontrollerm.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/09selectfilesowner.png"><img class="size-full wp-image-148 aligncenter" title="09selectfilesowner" src="/wp-content/uploads/2008/08/09selectfilesowner.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/10setclass.png"><img class="size-full wp-image-149 aligncenter" title="10setclass" src="/wp-content/uploads/2008/08/10setclass.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/11setviewtofilesowner.png"><img class="size-full wp-image-150 aligncenter" title="11setviewtofilesowner" src="/wp-content/uploads/2008/08/11setviewtofilesowner.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/12settitle.png"><img class="size-full wp-image-151 aligncenter" title="12settitle" src="/wp-content/uploads/2008/08/12settitle.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/13screenshot.png"><img class="size-full wp-image-152 aligncenter" title="13screenshot" src="/wp-content/uploads/2008/08/13screenshot.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/14screenshot2.png"><img class="size-full wp-image-153 aligncenter" title="14screenshot2" src="/wp-content/uploads/2008/08/14screenshot2.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-views.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>150</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 23:44:25 +0000</pubDate>
		<dc:creator>brandontreb</dc:creator>
				<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[iPhone Programming 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[<a href="/2008/07/30/iphone-programming-tutorial-connecting-code-to-an-interface-builder-view/"><img align="left" hspace="5" width="150" height="150" src="/wp-content/uploads/2008/07/screenshot_011-150x150.png" class="alignleft wp-post-image tfe" alt="" title="View-Based Application" /></a>
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 [...]]]></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_011.png"><img class="size-full wp-image-95 aligncenter" title="View-Based Application" src="/wp-content/uploads/2008/07/screenshot_011.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_031.png"><img class="size-full wp-image-97 aligncenter" title="screenshot_031" src="/wp-content/uploads/2008/07/screenshot_031.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_21.png"><img class="size-full wp-image-109 aligncenter" title="Text Field Attributes" src="/wp-content/uploads/2008/07/screenshot_21.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_23.png"><img class="size-full wp-image-111 aligncenter" title="Label Attributes" src="/wp-content/uploads/2008/07/screenshot_23.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_11.png"><img class="size-full wp-image-100 aligncenter" title="Button Attributes Inspector" src="/wp-content/uploads/2008/07/screenshot_11.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_15.png"><img class="size-full wp-image-103 aligncenter" title="Interface Screenshot" src="/wp-content/uploads/2008/07/screenshot_15.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/viewbasedcontrollerh.png"><img class="alignnone size-full wp-image-132" title="viewbasedcontrollerh" src="/wp-content/uploads/2008/07/viewbasedcontrollerh.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_16.png"><img class="alignnone size-full wp-image-104" title="View Connections" src="/wp-content/uploads/2008/07/screenshot_16.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_14.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_22.png"><img class="alignnone size-full wp-image-110" title="screenshot_22" src="/wp-content/uploads/2008/07/screenshot_22.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_081.png"><img class="alignnone size-full wp-image-98" title="Button Connections" src="/wp-content/uploads/2008/07/screenshot_081.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/viewbasedcontroller.png"><img class="alignnone size-full wp-image-130" title="viewbasedcontroller" src="/wp-content/uploads/2008/07/viewbasedcontroller.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>;</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_18.png"><img class="size-full wp-image-106 aligncenter" title="iPhone Screenshot 1" src="/wp-content/uploads/2008/07/screenshot_18.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_19.png"><img class="alignnone size-full wp-image-107" title="iPhone Screenshot 2" src="/wp-content/uploads/2008/07/screenshot_19.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_20.png"><img class="alignnone size-full wp-image-108" title="iPhone Screenshot 3" src="/wp-content/uploads/2008/07/screenshot_20.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/viewbased.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>148</slash:comments>
		</item>
		<item>
		<title>iPhone Programming Tutorial &#8211; Beginner Interface Builder Hello World</title>
		<link>http://icodeblog.com/2008/07/29/iphone-programming-tutorial-beginner-interface-builder-hello-world/</link>
		<comments>http://icodeblog.com/2008/07/29/iphone-programming-tutorial-beginner-interface-builder-hello-world/#comments</comments>
		<pubDate>Tue, 29 Jul 2008 16:29:12 +0000</pubDate>
		<dc:creator>brandontreb</dc:creator>
				<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[iPhone Programming Tutorials]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[iphone code]]></category>
		<category><![CDATA[iphone tutorial]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://icodeblog.com/?p=71</guid>
		<description><![CDATA[<a href="/2008/07/29/iphone-programming-tutorial-beginner-interface-builder-hello-world/"><img align="left" hspace="5" width="150" height="150" src="/wp-content/uploads/2008/07/screenshot_01-150x150.png" class="alignleft wp-post-image tfe" alt="" title="screenshot_01" /></a>digg_url = 'http://digg.com/apple/iPhone_Programming_Tutorial_Beginner_Interface_Builder';
In my last tutorial UITableView Hello World I said that there are many ways to write a &#8220;Hello World&#8221; tutorial for the iPhone.  Here is one using Interface Builder. Last time, I demonstrated a simple way to populate one cell in a UITableView with some text.  Today&#8217;s tutorial is even simpler.  I will [...]]]></description>
			<content:encoded><![CDATA[<p><script>digg_url = 'http://digg.com/apple/iPhone_Programming_Tutorial_Beginner_Interface_Builder';</script><script src="http://digg.com/api/diggthis.js"></script></p>
<p>In my last tutorial <a href="/2008/07/26/iphone-programming-tutorial-hello-world/">UITableView Hello World</a> I said that there are many ways to write a &#8220;Hello World&#8221; tutorial for the iPhone.  Here is one using Interface Builder. Last time, I demonstrated a simple way to populate one cell in a UITableView with some text.  Today&#8217;s tutorial is even simpler.  I will show you how to work with Interface Builder to create a simple layout for you application.  In fact, you won&#8217;t write any code at all! In my next tutorial, I will detail how to interface with your UI components in code.</p>
<p>In this tutorial you will learn:</p>
<ul>
<li><a href="#new-window-based-project">Create a New View Based Project</a></li>
<li><a href="#iphone-simulator">Opening the iPhone Simulator</a></li>
<li><a href="#interface-builder">Adding UI Elements to your home screen</a></li>
<li><a href="#executing-the-code">Executing the code</a></li>
</ul>
<h2><a name="new-window-based-project">Create a New View Based Project</a></h2>
<p>Let&#8217;s start by opening up XCode and Creating a new View-Based Application.  Do this by clicking on<br />
<strong>File &gt; New Project. </strong>Make sure that <strong>Application </strong>is highlighted under <strong>iPhone OS</strong> and select <strong>View-Based Application. </strong>Name this project HelloWorldViews.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/07/screenshot_01.png"><img class="size-full wp-image-72 aligncenter" title="screenshot_01" src="/wp-content/uploads/2008/07/screenshot_01.png" alt="" width="500" height="368" /></a></p>
<p style="text-align: left;">So at this point, Apple has added some default files to get us started.  They have added the main window for us to edit along with the code to launch the window.  Now in iPhone terms, each window is called a <strong>View</strong>.  Since you can only see one view at a time, you will need to create a new view for each screen in your application.  In this tutorial, we will be sticking to editing the view Apple has provided us.  In later tutorials, I will go into how to add new views and transition between them.  Go ahead and click <strong>Build and Go</strong>.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/07/buildandgo.png"><img class="size-full wp-image-36 aligncenter" title="Build And Go" src="/wp-content/uploads/2008/07/buildandgo.png" alt="" width="500" height="58" /></a></p>
<p style="text-align: left;">
<h2 style="text-align: left;"><a name="iphone-simulator">Opening the iPhone Simulator</a></h2>
<p style="text-align: left;">The program should compile and launch the <strong>iPhone Simululator</strong>. Your screen should look something like this.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/07/screenshot_02.png"><img class="size-full wp-image-73 aligncenter" title="screenshot_02" src="/wp-content/uploads/2008/07/screenshot_02.png" alt="" width="386" height="742" /></a></p>
<p style="text-align: left;">Not very interesting right? Excuse my crappy screenshot.  So let&#8217;s add some UI components to our view to make it more interesting.  To do this we will be editing HelloWorldViewsViewController.xib . A file with the .xib extension is known as a nib file.  These files open with Interface Builder and are where you define the look and feel of your application.  Apple is nice enough to have provided us with one.</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/07/screenshot_05.png"><img class="size-full wp-image-75 aligncenter" title="screenshot_05" src="/wp-content/uploads/2008/07/screenshot_05.png" alt="" width="264" height="82" /></a></p>
<h2 style="text-align: left;"><a name="interface-builder">Adding UI Elements to You Home Screen</a></h2>
<p style="text-align: left;">Once you open Interface Builder, you should see a few different windows&#8230; You should see a blank window that has the same dimentions as the iPhone that says <strong>View </strong>up in the top.  If not click the <strong>View</strong> icon in the smaller box.  This is the main window we will be editing.</p>
<p style="text-align: left;">To the right, you should see a tool box that contains many different UI components.  Most of them should look familiar if you have ever used an iPhone application.  So let&#8217;s add a few of them to our view.  Just click on the one&#8217;s you want and drag them on to your view. Make sure you at least use a <strong>Label</strong>.  For this tutorial, I have used a <strong>Label, Search Bar, </strong>and a <strong>Tab Bar</strong>.  Notice when you drag the search bar and tab bar onto the view, they size correctly to the screen.  Also, if you move the items around, you will see blue lines that guide you.  These are in place to help you line up components and center them.</p>
<p style="text-align: left;">After you have added the label, click on it.  Now let&#8217;s open the <strong>Attributes Inspector</strong> to change the text of the label.  To do this click <strong>Tools &gt; Attributes Inspector.</strong> At the top of the Attributes Inspector you should see a box labeled <strong>Text. </strong>If you don&#8217;t click on the label again to select it.  Put whatever you would like the label to say in that box.  I put &#8220;Hello World&#8221;.  You can update other properties of the label including color and font using the Attributes Inspector.  You may need to resize the Label to accommodate your new text.  To do this click on the label and you will see little boxes around it that you can drag to resize the text area.</p>
<p style="text-align: left;">After you are done, your interface should look something like this:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/07/screenshot_07.png"><img class="size-full wp-image-76 aligncenter" title="screenshot_07" src="/wp-content/uploads/2008/07/screenshot_07.png" alt="" width="320" height="482" /></a></p>
<p style="text-align: left;">Now press Apple-S to save this file.  We are done with Interface Builder so you can close it.  Your application should now be ready to execute.</p>
<h2 style="text-align: left;"><a name="executing-the-code">Executing the Code</a></h2>
<p style="text-align: left;">Go ahead and click <strong>Build and Go</strong> again to launch the <strong>iPhone Simulator.</strong> Your screens should look something like this:</p>
<p style="text-align: center;"><a href="/wp-content/uploads/2008/07/screenshot_08.png"><img class="size-full wp-image-77 aligncenter" title="screenshot_08" src="/wp-content/uploads/2008/07/screenshot_08.png" alt="" width="386" height="742" /></a></p>
<p style="text-align: left;">There you have it! A simple iPhone application without writing a line of code. If you click in the search box, the iPhone will automatically pull up the keyboard.  Thank you for reading this tutorial and I hope that you have learned something.  In my next tutorial, I will show you how to use code to interface with our UI components.  If you have any questions, feel free to leave them in the comments. Happy iCoding!</p>
]]></content:encoded>
			<wfw:commentRss>http://icodeblog.com/2008/07/29/iphone-programming-tutorial-beginner-interface-builder-hello-world/feed/</wfw:commentRss>
		<slash:comments>49</slash:comments>
		</item>
	</channel>
</rss>
