<?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>MacFan &#187; IB</title>
	<atom:link href="http://www.jkraft.fr/tag/ib/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jkraft.fr</link>
	<description>Tout sur le web et le développement</description>
	<lastBuildDate>Mon, 23 Jan 2012 09:07:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Tutorial Objective-c  Xcode et Interface Builder</title>
		<link>http://www.jkraft.fr/2009/02/26/tutorial-objective-c-xcode-et-interface-builder/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=tutorial-objective-c-xcode-et-interface-builder</link>
		<comments>http://www.jkraft.fr/2009/02/26/tutorial-objective-c-xcode-et-interface-builder/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 21:28:21 +0000</pubDate>
		<dc:creator>kraft</dc:creator>
				<category><![CDATA[Développement]]></category>
		<category><![CDATA[Iphone]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[IB]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://www.jkraft.fr/?p=216</guid>
		<description><![CDATA[Je me lance dans un nouveau petit tutorial sur la création d&#8217;une application simple pour iPhone en utilisant Interface Builder. Je ne vais pas m&#8217;attarder sur les notions de la programmation objet ou aux spécificités de Xcode. Pour un tutorial sur Xcode voir le précédent. On va donc créer une application calculant un montant TTC [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: left;">Je me lance dans un nouveau petit tutorial sur la création d&#8217;une application simple pour iPhone en utilisant Interface Builder. Je ne vais pas m&#8217;attarder sur les notions de la programmation objet ou aux spécificités de Xcode. Pour un tutorial sur Xcode voir le <a title="Tutorial xcode" href="http://www.jkraft.fr/2008/05/11/tutorial-objective-c-et-xcode/" target="_self">précédent</a>.</p>
<p style="text-align: left;">On va donc créer une application calculant un montant TTC à partir d&#8217;un montant HT et d&#8217;une TVA. Il y aura 2 champs de saisie et un bouton. Allez c&#8217;est parti !</p>
<p style="text-align: left;"><span id="more-216"></span></p>
<h3 style="text-align: left;">I) Création du projet</h3>
<p style="text-align: left;">Dans un premier temps on va créer un projet, dans la section iPhone sélectionnez View-Based Application.</p>
<p style="text-align: center;"><a href="http://www.jkraft.fr/wp-content/uploads/2009/02/image-2.jpg"><img class="size-medium wp-image-223 aligncenter" title="Création du projet" src="http://www.jkraft.fr/wp-content/uploads/2009/02/image-2-300x86.jpg" alt="Création du projet" width="300" height="86" /></a></p>
<p style="text-align: left;">Donnez lui un nom et choisissez son emplacement. Moi je vais le nommer <em>TutoIB</em>.<br />
En choisissant un projet de type View-Based Application on gagne un peu de temps car Xcode va nous créer un projet contenant déjà une classe gérant l&#8217;application et une classe controller gérant la vue.</p>
<p style="text-align: left;">On a donc 4 fichiers dans le dossier Classes, en fonction du nom de votre projet les fichiers seront différents<br />
Dans mon projet j&#8217;ai :</p>
<ul style="text-align: left;">
<li>TutoIBAppDelegate.h</li>
<li>TutoIBAppDelegate.m</li>
<li>TutoIBViewController.h</li>
<li>TutoIBViewController.m</li>
</ul>
<p style="text-align: left;">Je parlerais pas des différences entre les .h et .m voir mon tutorial <a title="Tutorial xcode" href="http://www.jkraft.fr/2008/05/11/tutorial-objective-c-et-xcode/" target="_self">précédent</a>.</p>
<p style="text-align: left;">Et dans le dossier Ressources il y a 2 fichiers qui nous intéressent.</p>
<ul style="text-align: left;">
<li>TutoIBViewController.xib</li>
<li>MainWindow.xib</li>
</ul>
<p style="text-align: left;">Doubles cliquez dessus et cela va lancer Interface Builder car ce sont ces fichiers qui décrivent l&#8217;interface graphique de notre application. Le MainWindow.xib appelant TutoIBViewController.xib c&#8217;est ce dernier qui va nous intéresser.</p>
<h3 style="text-align: left;">II) Interface Builder</h3>
<p style="text-align: left;">Vous devriez voir ceci :</p>
<p style="text-align: center;"><a href="http://www.jkraft.fr/wp-content/uploads/2009/02/image-2.png"><img class="size-medium wp-image-224 aligncenter" title="Interface Builder" src="http://www.jkraft.fr/wp-content/uploads/2009/02/image-2-300x187.png" alt="Edition de l'interface" width="300" height="187" /></a></p>
<p style="text-align: left;">Si ce n&#8217;est pas le cas, dans la fenêtre double cliquez sur View &#8211; UiView.<br />
C&#8217;est donc ici que nous allons rajouter les éléments de notre interface. Dans la librairie sélectionnez un Text Field et glissez le dans la fenêtre. Ajoutez en encore 2, puis des labels devant et enfin un boutton pour finalement obtenir ceci.</p>
<p style="text-align: center;"><img class="size-medium wp-image-225 aligncenter" title="Interface" src="http://www.jkraft.fr/wp-content/uploads/2009/02/image-3-192x300.png" alt="Interface terminée" width="192" height="300" /></p>
<p style="text-align: left;">L&#8217;interface est maintenant terminée, elle est simpliste. Je vous laisse regarder les possibilités de configuration des TextField.</p>
<h3 style="text-align: left;">III) Xcode</h3>
<p style="text-align: left;">On attaque maintenant la partie code. Le projet etant simpliste, on ne vas pas créer de classe. On va juste modifier la classe TutoIBViewController. Ouvrez le fichier TutoIBViewController.h et modifiez le code comme ceci.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span>
<span style="color: #a61390;">@interface</span> TutoIBViewController <span style="color: #002200;">:</span> UIViewController <span style="color: #002200;">&#123;</span>
IBOutlet    UITextField <span style="color: #002200;">*</span>fieldHT;
IBOutlet    UITextField <span style="color: #002200;">*</span>fieldTTC;
IBOutlet    UITextField <span style="color: #002200;">*</span>fieldTVA;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet UITextField <span style="color: #002200;">*</span>fieldHT;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet UITextField  <span style="color: #002200;">*</span>fieldTTC;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet UITextField <span style="color: #002200;">*</span>fieldTVA;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>calculate;
<span style="color: #a61390;">@end</span></pre></td></tr></table></div>

<p style="text-align: left;">On déclare nos 3 champs, pour pouvoir récuperer les valeurs saisies mais aussi modifier la valeur du champs fieldTTC qui est notre résultat. On déclare un IBAction qui est notre fonction de calcule.</p>
<p style="text-align: left;">Ensuite on implémente tout ca dans  TutoIBViewController.m.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;TutoIBViewController.h&quot;</span>
<span style="color: #a61390;">@implementation</span> TutoIBViewController
<span style="color: #a61390;">@synthesize</span> fieldHT;
<span style="color: #a61390;">@synthesize</span> fieldTVA;
<span style="color: #a61390;">@synthesize</span> fieldTTC;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>calculate <span style="color: #002200;">&#123;</span>
<span style="color: #a61390;">float</span> a <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>fieldHT text<span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">float</span> b <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>fieldTVA text<span style="color: #002200;">&#93;</span> floatValue<span style="color: #002200;">&#93;</span>;
<span style="color: #a61390;">float</span> c <span style="color: #002200;">=</span> a<span style="color: #002200;">*</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>b<span style="color: #002200;">/</span><span style="color: #2400d9;">100</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">+</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>;
fieldTTC.text <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%f&quot;</span>,c<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p style="text-align: left;">On récupère simplement les valeurs saisie dans les champs et on fait le calcule, et on affiche le résultat dans le fieldTTC. Simple non? Mais, comment le compilateur peut il savoir que la variable fiedTTC, fieldTVA et fieldHT correspondent aux textfields qu&#8217;on a placé dans l&#8217;interface?<br />
Réponse, il faut le lui préciser, et cela se fait très facilement. Sauvegardez les 2 fichiers, et retournez sur Interface Builder.</p>
<h3 style="text-align: left;">IV) Les connections</h3>
<p style="text-align: left;">Il faut donc faire la liaison entre les variables de la classe TutoIBViewController et l&#8217;interface graphique. Il suffit pour cela de les lier. Dans la fenêtre TutoIBViewController.xib cliquez sur File&#8217;s Owner puis sur Pomme-2 pour afficher le &laquo;&nbsp;Connection&#8217;s inspector&nbsp;&raquo;. La vous devriez voir les 3 noms de variable que nous avons déclarer dans la classe. Et donc il suffit de les lier avec les champs correspondant dans notre interface.</p>
<p style="text-align: center;"><a href="http://www.jkraft.fr/wp-content/uploads/2009/02/image-6.png"><img class="aligncenter size-medium wp-image-242" title="interfaceB" src="http://www.jkraft.fr/wp-content/uploads/2009/02/image-6-300x187.png" alt="interfaceB" width="300" height="187" /></a></p>
<p style="text-align: center;">
<p style="text-align: left;">Faites de même pour les 3 textfield. Enfin, il faut donner une fonction au bouton. Cette fois, au lieu de partir du File&#8217;s Owner, on va partir du bouton. Fait un ctrl-click sur le bouton, et glissez jusqu&#8217;au File&#8217;s Owner, et sélectionner notre fonction calculate.</p>
<p style="text-align: center;"><a href="http://www.jkraft.fr/wp-content/uploads/2009/02/image-5.png"><img class="aligncenter size-full wp-image-236" title="Inspecteur" src="http://www.jkraft.fr/wp-content/uploads/2009/02/image-5.png" alt="Inspecteur" width="291" height="255" /></a></p>
<p style="text-align: left;">Et voilà, tout est lié, il ne reste plus qu&#8217;à lancer la compilaton et l&#8217;execution, un pomme-R fera l&#8217;affaire !</p>
<p style="text-align: left;">Nous avons donc fait une petite application très simple. On peut l&#8217;améliorer en gérant l&#8217;affichage du clavier sur l&#8217;iPhone, le nombre de chiffre après la virgule etc..<br />
Je vous laisse chercher <img src='http://www.jkraft.fr/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script></p>
<div align="center">
<a href="http://twitter.com/share" class="twitter-share-button"<br />
data-url="<?php the_permalink() ?>&nbsp;&raquo;<br />
data-via=&nbsp;&raquo;@j_kraft&nbsp;&raquo;<br />
data-text=&nbsp;&raquo;<?php the_title(); ?>&nbsp;&raquo;<br />
data-count=&nbsp;&raquo;horizontal&nbsp;&raquo;>Tweet</a>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.jkraft.fr/2009/02/26/tutorial-objective-c-xcode-et-interface-builder/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
	</channel>
</rss>

