<?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>Chris-Software.com &#187; Interface Builder</title>
	<atom:link href="http://chris-software.com/index.php/tag/interface-builder/feed/" rel="self" type="application/rss+xml" />
	<link>http://chris-software.com</link>
	<description>iPhone, iPod touch games, Objective-C Tutorials, krzysztofrutkowski.com</description>
	<lastBuildDate>Tue, 29 Mar 2011 09:58:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Interface Builder</title>
		<link>http://chris-software.com/index.php/2009/04/26/interface-builder/</link>
		<comments>http://chris-software.com/index.php/2009/04/26/interface-builder/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 23:28:02 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[SDK Tools]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[tool]]></category>

		<guid isPermaLink="false">http://chris-software.com/?page_id=587</guid>
		<description><![CDATA[

Interface Builder is a great utility for new developers. As you might know, it&#8217;s used to design your user interface.
In fact, everything you create in Interface Builder you can also programmatically in Xcode. What is more Xcode given you more possibilities of customization any objects like sliders, toolbars, switches, images, text fields and more.
So what are [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img class="size-full wp-image-588 aligncenter" title="interfacebuilder" src="http://chris-software.com/wp-content/uploads/2009/04/interfacebuilder.png" alt="interfacebuilder" width="546" height="430" /></p>
<p><img class="alignleft size-full wp-image-452" style="margin: 10px;" title="dev_interfacemedium" src="http://chris-software.com/wp-content/uploads/2009/04/dev_interfacemedium.png" alt="dev_interfacemedium" width="256" height="256" /></p>
<p><em>Interface Builder</em> is a great utility for new developers. As you might know, it&#8217;s used to design your user interface.</p>
<p>In fact, everything you create in<em> Interface Builder</em> you can also programmatically in <em>Xcode</em>. What is more <em>Xcode</em> given you more possibilities of customization any objects like <em>sliders</em>, <em>toolbars</em>, <em>switches</em>, <em>images</em>, <em>text fields</em> and more.</p>
<p>So what are advantages and disadvantages of using <em>Interface Builder</em>?</p>
<p>Disadvantages? <strong>None</strong><strong>.</strong> Advantages? <strong>A lot of!</strong></p>
<p><em>Interface Builder</em> is a great utility that allows you to visually create an <em>interface</em>, not to guess what is the proper width, height and placement using <strong>X</strong> and <strong>Y</strong> coordinates. You can create almost every 2D game and application faster if using <em>Interface Builder</em>. It makes your code shorter &#8211; you don&#8217;t have to create the outlets manually, they are already created for you.</p>
<p>I will show you an example. How to create programmatically in <em>Xcode</em> a <em>label</em>:</p>
<pre>UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 50.0, 70.0, 30.0);];
label.textColor = [UIColor blueColor];
label.backgroundColor = [UIColor brownColor];
[label setText:@"My Text"];
label.textAlignment = UITextAlignmentCenter;
[self.view addSubview:label];</pre>
<p>You will obtain the same result dragging &amp; dropping the <em>label</em> from the <em>I.B.</em> <em>Library</em> to the <em>view</em>. Customization &#8211; like changing the text or background colour can be chosen from it&#8217;s menus, and while doing this, you track the changes and have full control if the new look matches the interface. But, customization is limited to keep <em>I.B.</em> window clear. You can&#8217;t rotate an label for example, but once you created an object and connected it to the right outlet <em>Xcode</em> can do the rest.</p>
<p>That was only an example. <em>Interface Builder</em> is really helpful and being a good programmer doesn&#8217;t mean the you have to use only <em>Xcode</em>. Relax, and play with <em>I.B.</em> every static content can be loaded from <strong>.xib</strong> files <em>I.B.</em> creates for you. <em>Xcode</em> will be necessary usually only if there is unknown number of objects to be displayed in the application.</p>
]]></content:encoded>
			<wfw:commentRss>http://chris-software.com/index.php/2009/04/26/interface-builder/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Hello World</title>
		<link>http://chris-software.com/index.php/2009/04/25/hello-world/</link>
		<comments>http://chris-software.com/index.php/2009/04/25/hello-world/#comments</comments>
		<pubDate>Sat, 25 Apr 2009 14:33:08 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Obj-C Tutorials]]></category>
		<category><![CDATA[Hello World]]></category>
		<category><![CDATA[IB]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[lesson]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://chris-software.com/?page_id=516</guid>
		<description><![CDATA[Hello World &#8211; usually the first program every programmer, no matter in what programming language, creates. It&#8217;s very simple and a great example if I present it to you step by step.
Let&#8217;s start by opening Xcode. Close any welcome windows which may appear. Choose File =&#62; New Project.

Make sure the Application from iPhone OS category [...]]]></description>
			<content:encoded><![CDATA[<p><em>Hello World</em> &#8211; usually the first program every programmer, no matter in what programming language, creates. It&#8217;s very simple and a great example if I present it to you step by step.</p>
<p style="text-align: left; ">Let&#8217;s start by opening <em>Xcode</em>. Close any welcome windows which may appear. Choose <strong>File</strong> =&gt; <strong>New Project</strong>.</p>
<p style="text-align: center; "><a href="http://chris-software.com/wp-content/uploads/2009/04/xcode_template.png"><img class="size-medium wp-image-543  aligncenter" title="xcode_template" src="http://chris-software.com/wp-content/uploads/2009/04/xcode_template-300x289.png" alt="xcode_template" width="300" height="289" /></a></p>
<p>Make sure the <strong>Application</strong> from <strong>iPhone OS</strong> category is selected on the left and choose <strong>Window-Based Application</strong> and press <strong>Choose&#8230;</strong> . [ <em>Window-Based Application</em> is the simplest template you can choose to begin, other are more complex but can save your time if they suit your application ]. Save your project wherever you like and name it properly for example <em>HelloWorld</em>.</p>
<p style="text-align: center; "><a href="http://chris-software.com/wp-content/uploads/2009/04/helloworld_start.png"><img class="size-medium wp-image-545 aligncenter" title="helloworld_start" src="http://chris-software.com/wp-content/uploads/2009/04/helloworld_start-300x220.png" alt="helloworld_start" width="300" height="220" /></a></p>
<p><em>Xcode</em> created some main files already for you. These files are categorized into 5 groups:</p>
<ul>
<li><em>Classes</em> &#8211; where <strong>HelloWorldAppDelegate.h</strong> (header) and <strong>.m</strong> (implementation) are. In header file you have declaration of the class, it&#8217;s objects, frameworks and other files inclusion. In implementation file you have class methods.</li>
<li><em>Other Sources</em> &#8211; <strong>HelloWorld_Prefix.pch</strong> is the prefix header for all other source files within your project, so if in future you need to import the same file to all other classes you can use this file instead of modifying each of the classes. <strong>main.m</strong> is the standard <em>main()</em> function known from <em>C language</em> that is called first.</li>
<li><em>Resources</em> &#8211; <strong>MainWindow.xib</strong> is called <em>NIB file</em> (like other with <em>.xib</em> extension) to create user interface with <em>Interface Builder</em>. <em>Info.plist</em> is the configuration file, where you can change some project settings &#8211; Bundle name and so on.</li>
<li><em>Frameworks</em> &#8211; source files created by Apple providing you basic and major functionality.</li>
<li><em>Products</em> &#8211; compiled project goes there</li>
</ul>
<p>Sounds difficult? I don&#8217;t think so. You can organize you files in a way that suits you. You will modify only files from <em>Classes</em> and create other classes.</p>
<p>To create the simplest <em>Hello World</em> example we could just edit a <em>MainWindow.xib</em> with Interface Builder but you wouldn&#8217;t learn anything at all. We are going to create your own <em>view</em> (iPhone application consists of only one window any many views displayed in window) and its <em>view controller</em>.</p>
<p>In <em>Xcode</em> choose <strong>File</strong> =&gt; <strong>New File</strong> and from <em>Cocoa Touch Classes</em> select <em>UIViewController subclass</em>, <strong>Next</strong>. Enter<strong> MyView.m</strong>, as file name, and without changing anything else press <strong>Finish</strong>.</p>
<p style="text-align: center; "><a href="http://chris-software.com/wp-content/uploads/2009/04/newfileviewcontr.png"></a><a href="http://chris-software.com/wp-content/uploads/2009/04/viewcontroller.png"><img class="alignnone size-medium wp-image-567" title="viewcontroller" src="http://chris-software.com/wp-content/uploads/2009/04/viewcontroller-300x230.png" alt="viewcontroller" width="300" height="230" /></a><img class="size-medium wp-image-546 aligncenter" title="newfileviewcontr" src="http://chris-software.com/wp-content/uploads/2009/04/newfileviewcontr-300x230.png" alt="newfileviewcontr" width="300" height="230" /></p>
<p>Now let&#8217;s create a view, where the text will be displayed. Choose <strong>File</strong> =&gt; <strong>New File</strong> and from <em>User Interfaces</em> choose<em> View XIB</em>, <strong>Next</strong>, call it <strong>MyView.xib</strong>, <strong>Finish</strong>.</p>
<p>Double click <strong>MyView.xib</strong> and it will be opened in <em>Interface Builder</em>.</p>
<p style="text-align: center; "><a href="http://chris-software.com/wp-content/uploads/2009/04/interfacebuildermainview.png"><img class="size-medium wp-image-547 aligncenter" title="interfacebuildermainview" src="http://chris-software.com/wp-content/uploads/2009/04/interfacebuildermainview-300x239.png" alt="interfacebuildermainview" width="300" height="239" /></a></p>
<p style="text-align: left; ">As you see <strong>MyView.xib</strong> currently consist of three objects. <strong>File&#8217;s Owner</strong> &#8211; there we will assign our <em>MyView</em> class soon, <strong>First Responder</strong> &#8211; handler of touches, buttons pressing, entering data, and <strong>View</strong> the part that in next few minutes will be visible on simulator screen.</p>
<p style="text-align: left; ">In <em>Interface Builder</em> go to <strong>Tools</strong> =&gt; <strong>Library</strong>. Find on the list <em>Label</em> (<em>UILabel</em>) and drag and drop it on the <em>View</em>. Click on the label and resize it. Double click and change it text to <em>Hello World</em>. You can also go to <strong>Tools</strong> =&gt; <strong>Inspector</strong> and change it aligment, color, shadow &#8211; it&#8217;s up to you, don&#8217;t close the <em>Inspector</em> window.</p>
<p style="text-align: left; ">Now select <strong>File&#8217;s Owner</strong> and go to the last tab in <em>Inspector</em> (<strong>Object Identity</strong>). View won&#8217;t be shown by itself that is why we already prepared the <em>MyView</em> class that&#8217;s going to control the view; under <strong>Class Identity</strong> in <em>Inspector</em> enter <em>MyView</em> as <strong>Class</strong>. As you see auto-completion works great (also in <em>Xcode</em>) so just entering <strong>m</strong> refers to <em>MyView</em>. Now <strong>File&#8217;s Owner</strong> is a type of <em>MyView,</em> not <em>NSObject</em> any longer. That&#8217;s not all. One <strong>.xib</strong> file may consists of many views and even if you have only one right now, you have to set is as a main view of <em>MyView</em> class. Again in <strong>File&#8217;s Owner&#8217;s Inspector</strong> in second tab (<strong>MyView connections</strong>) you have 3 outlets, one of them is called <em>view</em>.</p>
<p style="text-align: center; "><a href="http://chris-software.com/wp-content/uploads/2009/04/interfacebuildernoconnection.png"><img class="size-medium wp-image-548 aligncenter" title="interfacebuildernoconnection" src="http://chris-software.com/wp-content/uploads/2009/04/interfacebuildernoconnection-283x300.png" alt="interfacebuildernoconnection" width="283" height="300" /></a></p>
<p style="text-align: left; ">Click on the circle that&#8217;s on the right next to <em>view</em> hold your mouse button and drop it on the <em>View</em> window or <em>View</em> from <strong>MyView.xib</strong> window. The connection is set. While using an <em>Interface Builder</em> you have no options to give a unique names of objects like <em>views</em>, <em>labels</em>, <em>switches</em>, <em>buttons</em> or any other part of the interface. You have to prepare them in your class files like <em>MyView.h </em>and<em> .m</em>, and later you connect them just like before. Because <em>MyView</em> class is a <em>UIViewController</em> it already expects a <em>view</em> you can create programmaticaly or using <em>Interface Builder</em>.</p>
<p style="text-align: center; "><a href="http://chris-software.com/wp-content/uploads/2009/04/interfacemyviewconnections.png"><img class="size-medium wp-image-549 aligncenter" title="interfacemyviewconnections" src="http://chris-software.com/wp-content/uploads/2009/04/interfacemyviewconnections-175x300.png" alt="interfacemyviewconnections" width="175" height="300" /></a></p>
<p style="text-align: left; ">Let&#8217;s choose <strong>File</strong> =&gt; <strong>Save</strong> and go back to <em>Xcode</em>. Now we have to create some code. <em>MyView</em> class is already prepared to display <em>Hello World</em>, but we have nowhere entered any information to call this class. Let&#8217;s fix it.</p>
<p style="text-align: left; ">Go to <strong>HelloWorldAppDelegate.h</strong> and under:</p>
<pre><span>#import </span>&lt;UIKit/UIKit.h&gt;</pre>
<p>enter:</p>
<pre><span>#import </span>"MyView.h"
@class<span> </span><span>MyView</span><span>;</span></pre>
<p>These two lines of code allows you to use <em>MyView</em> class within <strong>HelloWorldAppDelegate.h</strong> and <strong>.h</strong> &#8211; files that are called when the application start. Let&#8217;s now go to the implementation file &#8211; <strong>HelloWorldAppDelegate.m</strong> and in the <em>applicationDidFinishLaunching</em> method enter these two lines:</p>
<pre><span>MyView *viewController = [[MyView</span><span> </span><span>alloc</span><span>] </span><span>initWithNibName</span><span>:</span><span>@"MyView"</span><span> </span><span>bundle</span><span>:[</span><span>NSBundle</span><span> </span><span>mainBundle</span><span>]];
<span>[</span><span>window</span><span> </span><span>addSubview</span><span>:[viewController </span><span>view</span><span>]];</span></span></pre>
<p>it should look like below:</p>
<pre>- (<span>void</span>)applicationDidFinishLaunching:(<span>UIApplication</span> *)application {    
<span><span>	</span></span><span>MyView</span><span> *viewController = [[</span><span>MyView</span><span> </span>alloc<span>] </span>initWithNibName<span>:</span><span>@"MyView"</span><span> </span>bundle<span>:[</span><span>NSBundle</span><span> </span>mainBundle<span>]];
<span>	</span>[<span>window</span> <span>addSubview</span>:[viewController <span>view</span>]];
<span>   	// Override point for customization after application launch
<span>	[</span><span>window</span><span> </span>makeKeyAndVisible<span>];
}</span></span></span></pre>
<p>First line creates the new <em>MyView</em> objects, we will use only one instance<em> </em>of it, but in some cases we can create (<em>alloc</em>) more objects from the same class. The second file put the <em>MyView</em> class <em>view</em> on the application window, in other words the <em>view</em> is a subview of a window. Everything what is part of the interface is a kind of view. The <em>label</em> with <em>HelloWorld</em> is a subview of <em>MyView&#8217;s view</em>.</p>
<p>That&#8217;s all. Let&#8217;s press Build &amp; Run. In the iPhone Simulator we should see our application.</p>
<p style="text-align: center;"><a href="http://chris-software.com/wp-content/uploads/2009/04/helloworldsimulator.png"><img class="size-medium wp-image-555 aligncenter" title="helloworldsimulator" src="http://chris-software.com/wp-content/uploads/2009/04/helloworldsimulator-161x300.png" alt="helloworldsimulator" width="161" height="300" /></a></p>
<p>That was to simple, wasn&#8217;t it? So, I would like to show you how to handle actions, like pressing a button. When the button is pressed the <em>label</em> shows <em>Hello Universe</em>.</p>
<p>Let&#8217;s go to the <strong>MyView.h</strong> and between <strong>{</strong> and <strong>}</strong> let&#8217;s add an <em>label</em> outlet and before <strong>@end</strong> an action. The outlet looks like: <em>IBOutlet UILabel *myText</em> that&#8217;s mean that my name of the object is <em>myText</em> of type <em>UILabel</em>, <em>IBOutlet</em> keyword means that it&#8217;s visible in <em>Interface Builder</em>. The action looks like <em>-(IBAction)changeText;</em> &#8211; I declare an action &#8211; method named <em>changeText</em> which will be visible in <em>Interface Builder</em> because of <em>IBAction</em> keyword. If you compile (Build) your project now, you will get two warnings, because in header file you declare the <em>changeText</em> but in implementation file doesn&#8217;t provide any code what this method does.</p>
<p><strong>MyView.h</strong> should look like:</p>
<pre><span>#import </span>&lt;UIKit/UIKit.h&gt;
<span>@interface</span> MyView : UIViewController {
<span>	</span><span>IBOutlet</span> <span>UILabel</span> *myText;
}
-(<span>IBAction</span>)changeText;
@end</pre>
<p>Now we have to implement <em>changeText</em> method in <strong>MyView.m</strong> file. Add this code below <em>@implementation MyView</em>:</p>
<pre>-(IBAction)changeText {
<span><span>	</span></span><span>myText</span><span>.</span><span>text</span><span> = [</span><span>NSString</span><span> </span><span>stringWithFormat</span><span>:</span>@"Hello Universe"<span>];
}</span></pre>
<p>As you see, we change the <em>text</em> of the <em>label</em> by using it&#8217;s name <em>myText</em> and and <em>text</em> keyword &#8211; property with dot between them, why not only <em>myText</em>? That&#8217;s because <em>myText</em> is the bigger object<em> </em>not only a text placeholder, you can easily change it&#8217;s background colour, font, opacity, shadows, just like you could in <em>Interface Builder</em>.</p>
<p>Build your project, it&#8217;s everything from the code site, now we have to go back to improve an interface. If you have already quited <em>Interface Builder</em> double click <strong>MyView.xib</strong> to reopen it. From the library, select <em>Round Rect Button</em> (<em>UIButton</em>) and drag&#8217;n'drop it to the view. Just like with the <em>label</em> you can double click on it and enter some text, for example <em>Change Text</em>. Let&#8217;s go to the <strong>File&#8217;s Owner Connections Inspector</strong> and you should see <em>MyText</em> as one of the outlets, and <em>changeText</em> as one of the actions:</p>
<p style="text-align: center; "><img class="size-full wp-image-561  aligncenter" title="connections" src="http://chris-software.com/wp-content/uploads/2009/04/connections.png" alt="connections" width="301" height="268" /></p>
<p>Just in the same way you connected <em>view</em> outlet to your view, connect <em>myText</em> to the label &#8211; click on the circle next to <em>myText</em> and release your button on the <em>Hello World</em> label. Although it&#8217;s only one label on the view, you are doing it to show the compiler what/where is the <em>label</em> called <em>myText</em>. Now let&#8217;s connect an action with the button. Click on the circle next to <em>changeText</em> and release your mouse button on the <em>Change Text</em> <em>button</em>. The menu should appear.</p>
<p style="text-align: center; "><img class="size-full wp-image-562 aligncenter" title="buttonactions" src="http://chris-software.com/wp-content/uploads/2009/04/buttonactions.png" alt="buttonactions" width="158" height="252" /></p>
<p>As you see there are few predicted user behaviours how he can press the button. We are interested in <em>Touch Up Inside</em> &#8211; it will call the <em>changeText</em> method when user press and release his finger without draging it out from the button. Now, <em>MyView Connections</em> should look like:</p>
<p style="text-align: center; "><img class="size-full wp-image-563 aligncenter" title="connectionsall" src="http://chris-software.com/wp-content/uploads/2009/04/connectionsall.png" alt="connectionsall" width="301" height="268" /></p>
<p>Save <strong>MyView.xib</strong> and press <em>Build and Go</em>. Now pressing the <em>Change Text</em> button changes the <em>label&#8217;s</em> text from <em>Hello World</em> to <em>Hello Universe</em>. Pressing the button again calls the <em>changeText</em> method but there is no change visual to us. It&#8217;s almost all but we forgot about one thing, so called <em>Memory Management</em>. We declared <em>myText</em> outlet and it will stay in memory until user reboot the device. You need to fix it &#8211; go to the <em>dealloc</em> method in <strong>MyView.m</strong> (should be on the bottom) and add a line <em>[myText release];</em>.</p>
<pre>- (<span>void</span>)dealloc {
    [<span>super</span> <span>dealloc</span>];
<span>    [</span><span>myText</span><span> </span>release<span>];
}</span></pre>
<p>That&#8217;s all. <em>Hello World</em> is something you should do all by yourself from now. I tried to show you everything as simple as possible. In next examples I will not tell you everything so detailed and post screenshots for every single step.</p>
<p style="text-align: center; "><a href="http://chris-software.com/wp-content/uploads/2009/04/helloworld.zip"><img class="size-full wp-image-564 aligncenter" title="xcodeproj" src="http://chris-software.com/wp-content/uploads/2009/04/xcodeproj.png" alt="xcodeproj" width="128" height="128" /></a><a href="http://chris-software.com/wp-content/uploads/2009/04/helloworld.zip">Download the project</a></p>
]]></content:encoded>
			<wfw:commentRss>http://chris-software.com/index.php/2009/04/25/hello-world/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
	</channel>
</rss>

