<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2enclosuresfull.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Tuto Mobile » Tutoriels iPhone</title>
	
	<link>http://www.tutomobile.fr</link>
	<description>Tutorial Android, iPhone, Windows mobile</description>
	<lastBuildDate>Fri, 23 Dec 2011 10:48:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/TutoMobileTutorielsIphone" /><feedburner:info uri="tutomobiletutorielsiphone" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><media:copyright>Tuto Mobile</media:copyright><media:category scheme="http://www.itunes.com/dtds/podcast-1.0.dtd">Technology/Software How-To</media:category><itunes:author>Axon</itunes:author><itunes:explicit>no</itunes:explicit><itunes:subtitle>Tutorial Android, iPhone, Windows mobile</itunes:subtitle><itunes:category text="Technology"><itunes:category text="Software How-To" /></itunes:category><item>
		<title>Création dʼun SplashScreen animé [Tutoriel iPhone n°12]</title>
		<link>http://feedproxy.google.com/~r/TutoMobileTutorielsIphone/~3/gYcSm4Oc_X4/</link>
		<comments>http://www.tutomobile.fr/creation-d%ca%bcun-splashscreen-anime-tutoriel-iphone-n%c2%b012/18/07/2011/#comments</comments>
		<pubDate>Mon, 18 Jul 2011 21:50:00 +0000</pubDate>
		<dc:creator>Axon</dc:creator>
				<category><![CDATA[Tutoriels iPhone]]></category>
		<category><![CDATA[CoreAnimation]]></category>
		<category><![CDATA[Launch image]]></category>
		<category><![CDATA[Retina]]></category>
		<category><![CDATA[Splash Screen]]></category>
		<category><![CDATA[SplashScreen]]></category>
		<category><![CDATA[XCode 4]]></category>

		<guid isPermaLink="false">http://www.tutomobile.fr/?p=1880</guid>
		<description><![CDATA[Vous avez créé une app.]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Vous avez créé une app. formidable qui s&#8217;annonce comme la meilleure vente de 2011 sur le Store. Malheureusement, elle est tellement complète qu&#8217;elle fait mouliner l&#8217;iPhone pendant 15 secondes à chaque lancement. Le Splash Screen est fait pour vous ! Cette image qui s&#8217;affiche dès que l&#8217;icône a été touchée permet de faire patienter l&#8217;utilisateur pendant le chargement de votre bijou d&#8217;Objective-C.</p>
<p><span id="more-1880"></span></p>
<p style="text-align: justify;">Avant de commencer il faut savoir que la mise en place d’un Splash Screen, écran de loading n’est pas conseillé par Apple : <em>The &laquo;&nbsp;splash screen&nbsp;&raquo; isn&#8217;t supposed to be for branding or displaying a logo, it&#8217;s supposed to look like the default condition of the app so it appears to start up quickly.<br />
</em>En gros le splash screen n’est pas supposé être utilisé pour l’affichage d’une marque ou faire de la pub mais doit afficher la capture de votre première vue au lancement de votre application.</p>
<p><a href="http://www.tutomobile.fr/wp-content/uploads/2011/07/SplashScreen-Bourse.png" rel="lightbox[1880]"><img class="aligncenter size-full wp-image-1881" title="SplashScreen Bourse" src="http://www.tutomobile.fr/wp-content/uploads/2011/07/SplashScreen-Bourse.png" alt="" width="619" height="426" /></a></p>
<p style="text-align: justify;">Exemple avec l’application <strong><em>Bourse</em></strong> : l’image utilisée pour le splash screen est la copie de la vue chargée au lancement.<br />
Bon maintenant une grande majorité des applications sur l’AppStore ne prennent pas en considération cette préconisation Apple et affichent soit leur logo ou encore de la pub.</p>
<p style="text-align: justify;">Alors comment fait-on ? Deux étapes : la première ajouter les images du splash screen (Une Retina et une autre “normale”) et la seconde mettre en place le code pour l&#8217;animation.</p>
<p style="text-align: justify;">
<h3>Créer le projet et mettre en place les images</h3>
<p style="text-align: justify;">Ouvrir un nouveau projet dans Xcode. On choisira ici une <strong><em>Window-based Application</em></strong> ou <strong><em>Empty Application</em></strong> (pour les dernière version d’Xcode), que l’on va appeler SplashScreen. La première fenêtre qui s’ouvre une fois le projet créé correspond aux informations du projet et de l&#8217;application en développement. La partie <strong><em>&laquo;&nbsp;Launch Images&nbsp;&raquo;</em></strong>, vous permet de déclarer les deux images &laquo;&nbsp;SplashScreen&nbsp;&raquo; : une standard et une deux fois plus grande pour le Retina Display de l&#8217;iPhone 4.</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2011/07/SplashScreen-SplashScreen.xcodeproj.png" rel="lightbox[1880]"><img class="aligncenter size-full wp-image-1883" title="SplashScreen - SplashScreen.xcodeproj" src="http://www.tutomobile.fr/wp-content/uploads/2011/07/SplashScreen-SplashScreen.xcodeproj.png" alt="" width="600" height="407" /></a></p>
<p style="text-align: justify;">Les deux images doivent être au format PNG. La image doit faire 480 x 320 pixels et la seconde (Retina) 960 x 640 pixels.</p>
<p>Ajouter vos images au projet en les faisant glisser depuis le Finder vers les zones concernées. La fenêtre qui s&#8217;ouvre alors vous propose d&#8217;ajouter une copie de ces éléments au projet, ce qu&#8217;il est très fortement conseillé de faire.</p>
<p><a href="http://www.tutomobile.fr/wp-content/uploads/2011/07/Xcode.png" rel="lightbox[1880]"><img class="aligncenter size-full wp-image-1884" title="Xcode" src="http://www.tutomobile.fr/wp-content/uploads/2011/07/Xcode.png" alt="" width="385" height="219" /></a></p>
<p style="text-align: justify;">A la fin de cette étape, vous pouvez constater que votre image s&#8217;affiche bien lors du chargement. Cette section &laquo;&nbsp;Launch Images&nbsp;&raquo;, extrêmement facile à paramétrer depuis la version 4 d&#8217;XCode permet de faire un SplashScreen simple, sans aucune ligne de code. Mais avec une jolie animation, ce serait quand même beaucoup mieux : c&#8217;est l&#8217;objet de la seconde partie.</p>
<h3>Mise en place du code</h3>
<p style="text-align: justify;">Rendez-vous dans le fichier <strong><em>&laquo;&nbsp;SpashScreenAppDelegate.h&nbsp;&raquo;</em></strong>, pour y ajouter une ImageView et une nouvelle méthode :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import UIKit/UIKit.h</span>
&nbsp;
<span style="color: #a61390;">@interface</span> SplashScreenAppDelegate <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span>  <span style="color: #002200;">&#123;</span>
    UIImageView <span style="color: #002200;">*</span>splashView;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> IBOutlet UIWindow <span style="color: #002200;">*</span>window;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>startupAnimationDone<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>animationID finished<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>finished context<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>context;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p style="text-align: justify;">Accédez au fichier <strong><em>&laquo;&nbsp;SplashScreenAppDelegate.m&nbsp;&raquo;</em></strong> pour y modifier la méthode <strong><em>&laquo;&nbsp;didFinishLaunchingWithOptions&nbsp;&raquo;</em></strong> qui a été automatiquement ajoutée par XCode lors de la création du projet.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>application<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application didFinishLaunchingWithOptions<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>launchOptions
<span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Override point for customization after application launch.</span>
    <span style="color: #002200;">&#91;</span>self.window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
&nbsp;
    splashView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIImageView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>,<span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">320</span>, <span style="color: #2400d9;">480</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    splashView.image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;screen_tuto_mobile.png&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>_window addSubview<span style="color: #002200;">:</span>splashView<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>_window bringSubviewToFront<span style="color: #002200;">:</span>splashView<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>UIView beginAnimations<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> context<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>UIView setAnimationDuration<span style="color: #002200;">:</span><span style="color: #2400d9;">0.5</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>UIView setAnimationTransition<span style="color: #002200;">:</span>UIViewAnimationTransitionNone forView<span style="color: #002200;">:</span>_window cache<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>UIView setAnimationDelegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>UIView setAnimationDidStopSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span> <span style="color: #002200;">&#40;</span>startupAnimationDone<span style="color: #002200;">:</span>finished<span style="color: #002200;">:</span>context<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
    splashView.frame <span style="color: #002200;">=</span> CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #002200;">-</span><span style="color: #2400d9;">60</span>, <span style="color: #002200;">-</span><span style="color: #2400d9;">60</span>, <span style="color: #2400d9;">440</span>, <span style="color: #2400d9;">600</span><span style="color: #002200;">&#41;</span>; splashView.alpha <span style="color: #002200;">=</span> <span style="color: #2400d9;">0.0</span>; <span style="color: #002200;">&#91;</span>UIView commitAnimations<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Pour résumer ce qui est mis en place :</p>
<ul>
<li>on crée notre image view qui fait toute la taille de l’écran</li>
<li>on lui renseigne son image</li>
<li>on l’ajoute à la vue principale ici window</li>
<li>on la met en premier plan</li>
<li>on lui affecte les effets d’animation (vous pouvez jouer sur les durées de transition)</li>
</ul>
<p style="text-align: justify;">Créez maintenant la méthode &laquo;&nbsp;startupAnimationDone:&nbsp;&raquo;. Nous avons précisé dans le code ci-dessus que c&#8217;est cette méthode qui doit être appelée lorsque l&#8217;animation est terminée.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>startupAnimationDone<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>animationID finished<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>finished context<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>context <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>splashView removeFromSuperview<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>splashView release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Ici on retire la vue splashView de la superview (window) et on release l’objet.</p>
<p style="text-align: justify;">Voila ! Il ne reste plus qu’à tester et vous verrez alors l’application se lancer avec votre image de chargement et celle-ci disparaîtra au chargement complet de l’application dans un effet de transition.</p>
<p style="text-align: justify;">
<h3>Petit message en guise de conclusion</h3>
<p style="text-align: justify;">Ce tutoriel a été écrit et réalisé par Keno974. C&#8217;est sa première page sur Tutomobile, n&#8217;hésitez pas à lui faire vos remarques, à poser vos questions et surtout à l&#8217;encourager dans les commentaires. L&#8217;objectif est évidemment de lui donner envie d&#8217;en réaliser d&#8217;autres ! <img src='http://www.tutomobile.fr/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/TutoMobileTutorielsIphone/~4/gYcSm4Oc_X4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.tutomobile.fr/creation-d%ca%bcun-splashscreen-anime-tutoriel-iphone-n%c2%b012/18/07/2011/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<feedburner:origLink>http://www.tutomobile.fr/creation-d%ca%bcun-splashscreen-anime-tutoriel-iphone-n%c2%b012/18/07/2011/</feedburner:origLink></item>
		<item>
		<title>[Concours] Résultats du concours pour gagner 4 formation video2brain iPhone</title>
		<link>http://feedproxy.google.com/~r/TutoMobileTutorielsIphone/~3/nnVTTaFVsp8/</link>
		<comments>http://www.tutomobile.fr/concours-resultats-du-concours-pour-gagner-4-formation-video2brain-iphone/29/05/2011/#comments</comments>
		<pubDate>Sun, 29 May 2011 16:43:02 +0000</pubDate>
		<dc:creator>Axon</dc:creator>
				<category><![CDATA[Tutoriels iPhone]]></category>
		<category><![CDATA[concours]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[video2brain]]></category>

		<guid isPermaLink="false">http://www.tutomobile.fr/?p=1870</guid>
		<description><![CDATA[Comme promis je vous communique]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Comme promis je vous communique les résultats du <a href="http://www.tutomobile.fr/concours-gagner-4-formations-videos-pour-apprendre-le-developpement-iphone/23/05/2011/">concours que nous avons organisé cette semaine</a>. Pour rappel, il y avait quatre formations iPhone video2brain à gagner pour un total d&#8217;une valeur de 199.20€.<span id="more-1870"></span></p>
<p style="text-align: justify;">Vous avez été 44 à participer sur Twitter et 47 via les commentaires, merci à vous tous d&#8217;avoir tenter votre chance.</p>
<p style="text-align: justify;">On ne va pas faire durer le suspens plus longtemps, les deux gagnants du concours via Twitter sont :</p>
<ul style="text-align: justify;">
<li>@<a href="http://twitter.com/pcmacactufr/status/72621828874575872" target="_blank">pcmacactufr</a></li>
<li>@<a href="http://twitter.com/netaneo/status/72772188574461952" target="_blank">netaneo</a></li>
</ul>
<p style="text-align: justify;">Concernant le concours  via les commentaires, après deux tirages au sort fait grâce au site random.org, les deux gagnants sont :</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2011/05/resultats_iphone_1.png" rel="lightbox[1870]"><img class="alignnone size-full wp-image-1871" title="resultats_iphone_1" src="http://www.tutomobile.fr/wp-content/uploads/2011/05/resultats_iphone_1.png" alt="" width="161" height="192" /></a> <a href="http://www.tutomobile.fr/wp-content/uploads/2011/05/resultats_iphone_2.png" rel="lightbox[1870]"><img class="alignnone size-full wp-image-1872" title="resultats_iphone_2" src="http://www.tutomobile.fr/wp-content/uploads/2011/05/resultats_iphone_2.png" alt="" width="161" height="190" /></a></p>
<p style="text-align: justify;">C&#8217;est à dire :</p>
<ul style="text-align: justify;">
<li>6 : <strong>Loic </strong>- Commence dans le développement iphone, cette formation serait vraiment cool.</li>
<li>26 : <strong>benoit </strong>- C’est vraiment bien de mettre en jeu se genre de formation, ca permet de se former <img src="http://www.tutomobile.fr/wp-includes/images/smilies/icon_smile.gif" alt=":)" /></li>
</ul>
<p style="text-align: justify;">Félicitation aux gagnants, je vais prendre contact avec eux pour qu&#8217;il puisse bénéficier de leur cadeau !</p>
<p style="text-align: justify;">
<img src="http://feeds.feedburner.com/~r/TutoMobileTutorielsIphone/~4/nnVTTaFVsp8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.tutomobile.fr/concours-resultats-du-concours-pour-gagner-4-formation-video2brain-iphone/29/05/2011/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.tutomobile.fr/concours-resultats-du-concours-pour-gagner-4-formation-video2brain-iphone/29/05/2011/</feedburner:origLink></item>
		<item>
		<title>[Concours] Gagner 4 formations vidéos pour apprendre le développement iPhone</title>
		<link>http://feedproxy.google.com/~r/TutoMobileTutorielsIphone/~3/CynKdqdtuhU/</link>
		<comments>http://www.tutomobile.fr/concours-gagner-4-formations-videos-pour-apprendre-le-developpement-iphone/23/05/2011/#comments</comments>
		<pubDate>Mon, 23 May 2011 08:12:55 +0000</pubDate>
		<dc:creator>Axon</dc:creator>
				<category><![CDATA[Tutoriels iPhone]]></category>
		<category><![CDATA[concours]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[video2brain]]></category>

		<guid isPermaLink="false">http://www.tutomobile.fr/?p=1859</guid>
		<description><![CDATA[Il y a deux semaines,]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Il y a deux semaines, nous faisions <a href="http://www.tutomobile.fr/concours-resultats-du-concours-pour-gagner-4-formation-video2brain-android/14/05/2011/" target="_blank">gagner quatre formations video2brain aux développeurs Android</a>, alors cette semaine c&#8217;est au tour des développeurs iPhone de gagner une des quatres formations <a href="https://www.video2brain.com/" target="_blank">video2brain</a> iPhone mis en jeu !<span id="more-1859"></span></p>
<p style="text-align: justify;"><a href="https://www.video2brain.com/" target="_blank">video2brain</a> propose plusieurs formations vidéos iPhone et notamment celle-ci <a href="https://partner.video2brain.com/tutomobile/courses.htm#/?c=631&amp;t=1" target="_blank">Développement d&#8217;applications pour l&#8217;iPhone</a>. D&#8217;une valeur de 49.80€ pour 6h de formation intensive, cette formation vous permettra d&#8217;avoir de bonnes bases dans le développement iPhone. C&#8217;est donc cette formation que nous allons vous faire gagner. Et comme vous êtes nombreux à nous suivre, nous avons décider de mettre en jeu quatre formations, soit un total de lot de <strong>199.20€</strong> !</p>
<p>Vous avez jusqu’au vendredi 27 mai 23h59 pour tenter votre chance. Pour cela il y a deux moyens de gagner :</p>
<ul>
<li>(<strong>2 gagnants</strong>) le premier sera de laisser un commentaire sur cette article (tirage au sort grace à <a href="http://www.random.org/" target="_blank">random.org</a>, une seule participation par personne est autorisée)</li>
<li>(<strong>2 gagnants</strong>) le second, vous devrez poster sur Twitter le tweet qui suit (et part la même occasion vous pouvez nous suivre mais ce n’est pas obligatoire <a href="http://twitter.com/TutoMobile" target="_blank">@TutoMobile</a>) :</li>
</ul>
<p style="text-align: center;"><strong><em>Gagner une formation de développement #iPhone (http://goo.gl/MuuJe) grâce à @TutoMobile et @video2brainfr #concours &#8211; http://sct.me/9DB</em></strong></p>
<p style="text-align: center;"><strong><span style="color: #ff0000;">Attention : vérifier bien que  les http soit présent dans les liens de votre tweet sinon votre candidature n’est pas pris en compte ! (MEME SI VOUS FAITES UN COPIER/COLLER, VERIFIEZ ! )</span></strong></p>
<p style="text-align: justify;">N’hésitez pas à partager ce concours sur Twitter, Facebook, mail ou autre pour nous remercier, se sera notre plus belle récompense ! <img src="http://www.tutomobile.fr/wp-includes/images/smilies/icon_wink.gif" alt=";)" /></p>
<p style="text-align: center;"><strong>BONNE CHANCE A TOUS !</strong></p>
<img src="http://feeds.feedburner.com/~r/TutoMobileTutorielsIphone/~4/CynKdqdtuhU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.tutomobile.fr/concours-gagner-4-formations-videos-pour-apprendre-le-developpement-iphone/23/05/2011/feed/</wfw:commentRss>
		<slash:comments>51</slash:comments>
		<feedburner:origLink>http://www.tutomobile.fr/concours-gagner-4-formations-videos-pour-apprendre-le-developpement-iphone/23/05/2011/</feedburner:origLink></item>
		<item>
		<title>Intégrer et utiliser le partage Facebook [Tutoriel iPhone n°11]</title>
		<link>http://feedproxy.google.com/~r/TutoMobileTutorielsIphone/~3/Zg2BAx5OZFg/</link>
		<comments>http://www.tutomobile.fr/integrer-et-utiliser-le-partage-facebook-tutoriel-iphone-n%c2%b011/22/05/2011/#comments</comments>
		<pubDate>Sun, 22 May 2011 11:45:33 +0000</pubDate>
		<dc:creator>Axon</dc:creator>
				<category><![CDATA[Tutoriels iPhone]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[video2brain]]></category>

		<guid isPermaLink="false">http://www.tutomobile.fr/?p=1831</guid>
		<description><![CDATA[Voici un seconde tuto iPhone]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Voici un seconde tuto iPhone vidéo, j&#8217;espère qu&#8217;il vous plaira autant que le premier ! <img src='http://www.tutomobile.fr/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  Nous allons voir dans ce tuto fourni par <a href="http://www.video2brain.com/fr/" target="_blank">video2brain</a>, comment intégrer le partage Facebook dans vos applications.<span id="more-1831"></span></p>
<p style="text-align: justify;">Par la même occasion je vous annonce que lundi nous mettrons en place un petit concours pour <a href="http://www.tutomobile.fr/concours-gagner-4-formations-videos-pour-apprendre-le-developpement-iphone/23/05/2011/">gagner quatre formations video2brain iPhone</a>, alors soyez au rendez-vous!</p>
<p style="text-align: center;"><a href="http://www.tutomobile.fr/concours-gagner-4-formations-videos-pour-apprendre-le-developpement-iphone/23/05/2011/"><strong>TENTEZ VOTRE CHANCE ICI !!!</strong></a></p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="600" height="338" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://vimeo.com/moogaloop.swf?clip_id=23948685&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00adef&amp;fullscreen=1&amp;autoplay=0&amp;loop=0" /><embed type="application/x-shockwave-flash" width="600" height="338" src="http://vimeo.com/moogaloop.swf?clip_id=23948685&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=00adef&amp;fullscreen=1&amp;autoplay=0&amp;loop=0" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<img src="http://feeds.feedburner.com/~r/TutoMobileTutorielsIphone/~4/Zg2BAx5OZFg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.tutomobile.fr/integrer-et-utiliser-le-partage-facebook-tutoriel-iphone-n%c2%b011/22/05/2011/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<enclosure url="http://vimeo.com/moogaloop.swf?clip_id=23948685&amp;amp;server=vimeo.com&amp;amp;show_title=0&amp;amp;show_byline=0&amp;amp;show_portrait=0&amp;amp;color=00adef&amp;amp;fullscreen=1&amp;amp;autoplay=0&amp;amp;loop=0" length="-1" type="application/x-shockwave-flash" /><media:content url="http://vimeo.com/moogaloop.swf?clip_id=23948685&amp;amp;server=vimeo.com&amp;amp;show_title=0&amp;amp;show_byline=0&amp;amp;show_portrait=0&amp;amp;color=00adef&amp;amp;fullscreen=1&amp;amp;autoplay=0&amp;amp;loop=0" type="application/x-shockwave-flash" /><itunes:explicit>no</itunes:explicit><itunes:subtitle>Voici un seconde tuto iPhone</itunes:subtitle><itunes:author>Axon</itunes:author><itunes:summary>Voici un seconde tuto iPhone</itunes:summary><itunes:keywords>Tutoriels iPhone, iPhone, video2brain</itunes:keywords><feedburner:origLink>http://www.tutomobile.fr/integrer-et-utiliser-le-partage-facebook-tutoriel-iphone-n%c2%b011/22/05/2011/</feedburner:origLink></item>
		<item>
		<title>Application Multivues avec XCode 4 (partie 1) [Tutoriel iPhone n°10]</title>
		<link>http://feedproxy.google.com/~r/TutoMobileTutorielsIphone/~3/ChFaGFUc_uM/</link>
		<comments>http://www.tutomobile.fr/application-multivue-avec-xcode-4-partie-1-tutoriel-iphone-n%c2%b010/01/04/2011/#comments</comments>
		<pubDate>Fri, 01 Apr 2011 12:11:47 +0000</pubDate>
		<dc:creator>Axon</dc:creator>
				<category><![CDATA[Tutoriels iPhone]]></category>
		<category><![CDATA[Disclosure]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[UINavigationController]]></category>
		<category><![CDATA[UITableCell]]></category>
		<category><![CDATA[UITableView]]></category>
		<category><![CDATA[UITableViewDataSource]]></category>
		<category><![CDATA[UIViewController]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://www.tutomobile.fr/?p=1714</guid>
		<description><![CDATA[Ce premier tutoriel vidéo iPhone]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Ce premier tutoriel vidéo iPhone fait référence au <a href="http://www.tutomobile.fr/category/tutorial-iphone/" target="_blank">tutoriel iPhone</a> n°8 : <a title="Permanent Link to Liste et détails : créer une application multivues (1/3) [Tutoriel iPhone n°8]" rel="bookmark" href="http://www.tutomobile.fr/liste-et-details-creer-une-application-multivues-12-tutoriel-iphone-n%c2%b08/09/11/2010/" target="_blank">Liste et détails : créer une application multivues (1/3)</a>. Il s&#8217;agit du même tuto mais adapter pour XCode 4. Je remercie énormément Ich&#8217; d&#8217;avoir pris le temps de faire ce tuto de 50 min. pour Tuto Mobile.<span id="more-1714"></span></p>
<p style="text-align: center;"><strong>Pensez à activer la HD pour un rendu optimal, sinon l&#8217;image est flou.</strong></p>
<p><center><br />
<iframe src="http://player.vimeo.com/video/21796864" width="550" height="412" frameborder="0"></iframe>
<p><a href="http://vimeo.com/21796864">[Tuto iPhone] Application Multivue avec XCode 4 (partie 1)</a> from <a href="http://vimeo.com/user6542229">Axon TutoMobile</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p>.</center></p>
<img src="http://feeds.feedburner.com/~r/TutoMobileTutorielsIphone/~4/ChFaGFUc_uM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.tutomobile.fr/application-multivue-avec-xcode-4-partie-1-tutoriel-iphone-n%c2%b010/01/04/2011/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		<feedburner:origLink>http://www.tutomobile.fr/application-multivue-avec-xcode-4-partie-1-tutoriel-iphone-n%c2%b010/01/04/2011/</feedburner:origLink></item>
		<item>
		<title>Liste et détails : créer une application multivues (2/3) [Tutoriel iPhone n°9]</title>
		<link>http://feedproxy.google.com/~r/TutoMobileTutorielsIphone/~3/HfHsXtpZlJY/</link>
		<comments>http://www.tutomobile.fr/liste-et-details-creer-une-application-multivues-23-tutoriel-iphone-n%c2%b09/17/02/2011/#comments</comments>
		<pubDate>Thu, 17 Feb 2011 22:32:44 +0000</pubDate>
		<dc:creator>Axon</dc:creator>
				<category><![CDATA[Tutoriels iPhone]]></category>
		<category><![CDATA[BarButtonItem]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[Modal]]></category>
		<category><![CDATA[NSMutableArray]]></category>
		<category><![CDATA[UINavigationController]]></category>
		<category><![CDATA[UITextField]]></category>
		<category><![CDATA[viewWillAppear]]></category>
		<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://www.tutomobile.fr/?p=1474</guid>
		<description><![CDATA[Ce tutoriel est la suite]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Ce tutoriel est la suite du <a href="http://www.tutomobile.fr/liste-et-details-creer-une-application-multivues-12-tutoriel-iphone-n°8/09/11/2010/" target="_self">tutoriel iPhone n°8</a>. Il reprend là où nous avions laissé le travail précédemment : nous avons fait une liste d&#8217;éléments issus d&#8217;un fichier .plist et, en touchant un élément de la liste, nous avions accès à une fenêtre de détails. Il s&#8217;agit ici d&#8217;apporter des modifications à la liste et plus précisément d&#8217;ajouter des éléments : l&#8217;occasion ici de découvrir les <strong><em>fenêtres modales</em></strong> sur iOS.</p>
<p><span id="more-1474"></span></p>
<p style="text-align: justify;">
<h3>Préambule : NSArray et NSMutableArray</h3>
<p style="text-align: justify;">Avant de commencer à ajouter nos fonctionnalités, il est nécessaire de s&#8217;arrêter sur un point concernant les objets proposés par UIKit. Pour stocker notre liste de sites Internet, nous avons utilisé un objet de type NSArray :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>tabWebSites;</pre></div></div>

<p style="text-align: justify;">Il s&#8217;agit d&#8217;un tableau d&#8217;objets qui permet toutes sortes d&#8217;actions comme &laquo;&nbsp;compter le nombre d&#8217;éléments&nbsp;&raquo;, &laquo;&nbsp;récupérer un élément du tableau&nbsp;&raquo;, etc. NSArray a une limitation, et elle est de taille : il ne peut pas être modifié. Du coup cela va nous poser problème dans ce tutoriel : si je souhaite ajouter ou supprimer des éléments, il va bien falloir que je modifie mon tableau de sites.</p>
<p style="text-align: justify;">Pour implémenter un tableau que l&#8217;on peut modifier de la sorte, on utile la classe <strong><em>NSMutableArray</em></strong>. Cette classe propose les mêmes méthodes que NSArray qu&#8217;elle surcharge et fonctionne donc de manière identique. Elle propose, en outre plusieurs méthodes nouvelles :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">insertObject<span style="color: #002200;">:</span>atIndex<span style="color: #002200;">:</span>
removeObjectAtIndex<span style="color: #002200;">:</span>
addObject<span style="color: #002200;">:</span>
removeLastObject
replaceObjectAtIndex<span style="color: #002200;">:</span>withObject<span style="color: #002200;">:</span></pre></div></div>

<p style="text-align: justify;">Alors pourquoi avoir utilisé NSArray dans le tutoriel n°8 ? Il faut évidemment considérer les performances et l&#8217;impact mémoire dans le choix de vos objets dans UIKit. NSArray est bien plus économe que NSMutableArray et, quand on développe pour iPhone (comme pour tout développement embarqué), cette question est fondamentale.</p>
<p style="text-align: justify;">Voici donc un petit exercice nécessaire avant de continuer : transformer le tableau NSArray *tabWebSites en NSMutableArray. Il s&#8217;agit de faire les modifications dans le RootViewController (.m et .h). Faîtes cette modification (il y a plusieurs endroits à modifier) et recompilez votre application pour vérifier que tout fonctionne encore avant de passer à la suite !</p>
<h3>Ajouter le bouton sur la vue pour ajouter un site</h3>
<p style="text-align: justify;">Nous souhaitons proposer aux utilisateurs d&#8217;ajouter le ou les sites de leur choix à la liste. Nous allons donc ajouter, dans la barre de navigation un bouton &laquo;&nbsp;Ajouter&nbsp;&raquo; qui permettra d&#8217;ouvrir un dialogue d&#8217;ajout.</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2011/02/Capture-d’écran-2011-02-09-à-22.59.50.png" rel="lightbox[1474]"><img class="aligncenter size-full wp-image-1611" title="Résultat sur iPhone 4" src="http://www.tutomobile.fr/wp-content/uploads/2011/02/Capture-d’écran-2011-02-09-à-22.59.50.png" alt="" width="396" height="414" /></a></p>
<p style="text-align: justify;">Dans XCode, nous allons ajouter un IBOutlet appelé &laquo;&nbsp;addButtonItem&nbsp;&raquo; qui représentera le bouton, et une IBAction qui sera appelée quand l&#8217;utilisateur appuiera sur ce bouton. Dans RootViewController.h, nous ajoutons donc ces deux éléments :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> RootViewController <span style="color: #002200;">:</span> UITableViewController <span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>websites;
	IBOutlet UIBarButtonItem <span style="color: #002200;">*</span>addButtonItem;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>tabWebSites;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> UIBarButtonItem <span style="color: #002200;">*</span>addButtonItem;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> addButtonPressed<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender;</pre></div></div>

<p style="text-align: justify;">Dans le fichier RootViewController.m, ajouter le @synthetise et n&#8217;oubliez pas le &laquo;&nbsp;release&nbsp;&raquo; dans la méthode &laquo;&nbsp;dealloc&nbsp;&raquo;. Vous devez implémenter la méthode addButtonPressed. Ne mettons qu&#8217;un simple log pour le moment :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> addButtonPressed<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender <span style="color: #002200;">&#123;</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;On ajoute !&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Maintenant il s&#8217;agit de créer effectivement l&#8217;objet. Pour ce faire, nous pouvons le faire dans le code (via XCode) ou beaucoup plus simplement à l&#8217;aide d&#8217;InterfaceBuilder. Voyons comment mettre en place cette deuxième méthode.</p>
<p style="text-align: justify;">Ouvrez le fichier RootViewController.xib, et faîtes apparaître la fenêtre &laquo;&nbsp;Library&nbsp;&raquo;. Faîtes glisser un élément &laquo;&nbsp;Bar Button Item&nbsp;&raquo; dans la fenêtre principale.</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2011/02/UIBarButton-Item.png" rel="lightbox[1474]"><img class="aligncenter size-full wp-image-1596" title="UIBarButton Item" src="http://www.tutomobile.fr/wp-content/uploads/2011/02/UIBarButton-Item.png" alt="Ajouter un UIBar Button Item" width="517" height="377" /></a></p>
<p style="text-align: justify;">Ouvrez la fenêtre d&#8217;inspecteur et modifiez le champ &laquo;&nbsp;Identifier&nbsp;&raquo; de bouton pour &laquo;&nbsp;Add&nbsp;&raquo; (c&#8217;est le bouton avec le &laquo;&nbsp;+&nbsp;&raquo;). Profitez en pour vous familiariser avec les différents types de boutons proposés. Vous reconnaîtrez ceux que vous avez l&#8217;habitude de voir sur la plupart des applications iPhone (Bookmarks, Search, Camera&#8230;).</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2011/02/Type-de-UIButtonItem.png" rel="lightbox[1474]"><img class="aligncenter size-full wp-image-1597" title="Type de UIButtonItem" src="http://www.tutomobile.fr/wp-content/uploads/2011/02/Type-de-UIButtonItem.png" alt="Changer le type du UIButtonItem" width="299" height="246" /></a></p>
<p style="text-align: justify;">Enfin, il s&#8217;agit de relier l&#8217;IBOutlet et l&#8217;IBAction que nous avons créés précédemment avec ce nouvel élément. Cliquez-droit sur &laquo;&nbsp;File&#8217;s owner&nbsp;&raquo; (dans notre cas, il s&#8217;agit d&#8217;un RootViewController) et faîtes glisser le IBOutlet &laquo;&nbsp;addButtonItem&nbsp;&raquo; vers le bouton nouvellement créé. Pour l&#8217;action, c&#8217;est presque pareil, cliquez droit sur le bouton dans la liste et faîtes glisser &laquo;&nbsp;selector&nbsp;&raquo; vers File&#8217;s Owner. Choisissez notre méthode addButtonPressed.</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2011/02/IBOutlet-et-IBAction.png" rel="lightbox[1474]"><img class="aligncenter size-full wp-image-1598" title="IBOutlet et IBAction" src="http://www.tutomobile.fr/wp-content/uploads/2011/02/IBOutlet-et-IBAction.png" alt="Relier l'action " width="546" height="282" /></a></p>
<p style="text-align: justify;">Sauvegardez et revenez à XCode.</p>
<p style="text-align: justify;">Il reste une dernière formalité et pas des moindres. Il faut présenter ce nouveau bouton. Il nous faut l&#8217;ajouter dans la barre de navigation, dans la partie de gauche. Pour ce faire, rendez-vous dans la méthode viewDidLoad pour ajouter la ligne suivante :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">self.navigationItem.leftBarButtonItem <span style="color: #002200;">=</span> self.addButtonItem;</pre></div></div>

<p style="text-align: justify;">Sauvegardez et compilez. Le bouton apparaît alors dans la barre de navigation supérieure. Quand vous cliquez dessus, la ligne de log est imprimée. Parfait !</p>
<h3>Créer la vue qui permettra de saisir les informations du nouveau site</h3>
<p style="text-align: justify;">Si vous avez suivi la première partie de ce tutoriel, il n&#8217;y a rien, ici, de bien nouveau. Je vais donc passer rapidement là dessus en vous précisant les points essentiels :</p>
<ul>
<li>Dans XCode, créez une nouvelle classe, héritée de UIViewController, appelez-la <strong><em>ModifViewController</em></strong> et n’oubliez pas d’associer un fichier .h et un fichier .xib</li>
<li>Ajoutez trois attributs et autant de propriétés dans le fichier .h</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import </span>
&nbsp;
<span style="color: #a61390;">@interface</span> ModifViewController <span style="color: #002200;">:</span> UIViewController <span style="color: #002200;">&#123;</span>
	IBOutlet UITextField <span style="color: #002200;">*</span>titleTextField;
	IBOutlet UITextField <span style="color: #002200;">*</span>descriptionTextField;
	IBOutlet UITextField <span style="color: #002200;">*</span>urlTextField;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span> UITextField <span style="color: #002200;">*</span>titleTextField;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span> UITextField <span style="color: #002200;">*</span>descriptionTextField;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span> UITextField <span style="color: #002200;">*</span>urlTextField;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<ul>
<li>Synthétisez les dans le .m et n&#8217;oubliez pas les release dans la méthode dealloc</li>
<li>Dans InterfaceBuilder, Ajoutez trois zones de textes et des labels pour obtenir le rendu suivant (on laisse de la place pour le clavier qui va apparaître par la suite) :</li>
</ul>
<p><a href="http://www.tutomobile.fr/wp-content/uploads/2011/02/3-UITextFields.png" rel="lightbox[1474]"><img class="aligncenter size-full wp-image-1613" title="3 UITextFields" src="http://www.tutomobile.fr/wp-content/uploads/2011/02/3-UITextFields.png" alt="" width="400" height="582" /></a></p>
<ul>
<li>Attachez les outlets avec les éléments que vous venez de déposer dans la vue.</li>
<li>Revenez à XCode, compilez pour voir si tout va bien (inutile de tester : on a pas appelé la vue).</li>
</ul>
<h3>Appeler la vue quand on appuie sur le bouton &laquo;&nbsp;+&nbsp;&raquo;</h3>
<p style="text-align: justify;">Vous avez un bouton &laquo;&nbsp;+&nbsp;&raquo;, vous avez une vue à appeler : il ne reste qu&#8217;à lier les deux ! Pour afficher des formulaires d&#8217;ajout et de modification, il convient d&#8217;utiliser une autre métaphore que celle employée pour la navigation (qui enchaîne les écrans en poussant l&#8217;ancien écran vers la gauche quand le nouveau arrive par la droite). Il s&#8217;agit des &laquo;&nbsp;<strong><em>Modal View</em></strong>&laquo;&nbsp;. Un exemple, sur votre iPhone, allez dans l&#8217;application &laquo;&nbsp;Téléphone&nbsp;&raquo; ou contacts et appuyez sur le &laquo;&nbsp;+&nbsp;&raquo; pour ajouter un contact. L&#8217;écran d&#8217;ajout arrive par le bas pour se superposer sur l&#8217;écran actuel. L&#8217;utilisateur sait que l&#8217;écran précédent est derrière cette nouvelle vue et s&#8217;attend à la retrouver une fois qu&#8217;il aura appuyé sur un bouton OK (ou Annuler, le cas échéant).</p>
<p style="text-align: justify;">En terme de code, c&#8217;est sensiblement la même chose que la navigation dans un UINavigationController : on crée la vue, on la pousse, et on release. La seule différence, c&#8217;est que c&#8217;est la vue actuelle qui &laquo;&nbsp;supporte&nbsp;&raquo; la fenêtre modale, et non un UINavigationController. Rendez-vous dans méthode <strong><em>addButtonPressed</em></strong> et modifiez la comme suit :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> addButtonPressed<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender <span style="color: #002200;">&#123;</span>
	ModifViewController <span style="color: #002200;">*</span>addView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ModifViewController alloc<span style="color: #002200;">&#93;</span> initWithNibName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ModifViewController&quot;</span> bundle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self presentModalViewController<span style="color: #002200;">:</span>addView animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>addView release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Compilez, testez&#8230; La fenêtre modale apparaît comme demandée&#8230; et&#8230; GAME OVER : vous ne pouvez plus faire autre chose que quitter l&#8217;application : aucun élément ne permet de revenir.</p>
<h3>Euh chef ?! On fait quoi maintenant ?</h3>
<p style="text-align: justify;">Super on affiche notre vue pour ajouter, mais on ne peut pas en partir sans devoir quitter l&#8217;application. Il nous faut ajouter deux boutons &laquo;&nbsp;Annuler&nbsp;&raquo; et &laquo;&nbsp;OK&nbsp;&raquo; et enregistrer le nouveau site. Généralement, ces boutons sont dans la partie supérieure de l&#8217;écran, dans une barre qui fait curieusement penser à la barre bleue qu&#8217;on a fait apparaître avec les <strong><em>UINavigationController</em></strong>. Commençons donc par mettre ce navigationController autour de notre vue :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> addButtonPressed<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender <span style="color: #002200;">&#123;</span>
	ModifViewController <span style="color: #002200;">*</span>addView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ModifViewController alloc<span style="color: #002200;">&#93;</span> initWithNibName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ModifViewController&quot;</span> bundle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	UINavigationController <span style="color: #002200;">*</span>navc <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UINavigationController alloc<span style="color: #002200;">&#93;</span> initWithRootViewController<span style="color: #002200;">:</span>addView<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self presentModalViewController<span style="color: #002200;">:</span>navc animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>addView release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>navc release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Ajoutons deux boutons : un &laquo;&nbsp;Enregistrer&nbsp;&raquo; et un &laquo;&nbsp;Annuler&nbsp;&raquo;. Pour cela, on procède un peu de la même façon que pour notre bouton &laquo;&nbsp;+&nbsp;&raquo; précédent. Ouvrez ModifViewController.m et modifiez la méthode <strong><em>ViewDidLoad</em></strong> comme suit :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
    self.navigationItem.leftBarButtonItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIBarButtonItem alloc<span style="color: #002200;">&#93;</span>
		initWithBarButtonSystemItem<span style="color: #002200;">:</span>UIBarButtonSystemItemCancel
		target<span style="color: #002200;">:</span>self
		action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>cancel<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>
	autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Dans ce code, on ajoute donc nos deux boutons qu&#8217;on associe avec des méthodes (@selector) : le bouton &laquo;&nbsp;Annuler&nbsp;&raquo; est associé à la méthode &laquo;&nbsp;cancel:&nbsp;&raquo; et le bouton &laquo;&nbsp;Enregistrer&nbsp;&raquo; est associé à &laquo;&nbsp;save&nbsp;&raquo;.</p>
<p style="text-align: justify;">Commençons par implémenter la méthode &laquo;&nbsp;cancel&nbsp;&raquo;. Cette méthode doit masquer la vue pour revenir à l&#8217;écran précédent. Dans <em><strong>ModifViewController.h</strong></em>, ajoutez la déclaration de la méthode :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> cancel<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender;</pre></div></div>

<p style="text-align: justify;">Et implémentez-la dans ModifViewController.m :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> cancel<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>self dismissModalViewControllerAnimated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Vous pouvez compiler et tester. Cette fois, ouf ! nous ne sommes plus bloqués. Par contre, il est temps de se concentrer sur la sauvegarde.</p>
<h3>Procéder à l&#8217;enregistrement du nouveau site</h3>
<p style="text-align: justify;">Petite exercice pour commencer. Ajoutez un bouton <strong><em>UIBarButtonSystemItemSave</em></strong> dans la partie droite (rightBarButtonItem) qui appelle une nouvelle méthode appelée save:. Pour le moment, dans cette méthode, fermez simplement la fenêtre modale. Si vous séchez, jetez juste un oeil un peu plus haut, nous venons de le faire.</p>
<p style="text-align: justify;">Il s&#8217;agit maintenant de sauvegarder notre nouveau site dans notre liste de sites.</p>
<p style="text-align: justify;">Avant de commencer, il nous faut faire un import de la classe WebSite pour qu&#8217;on puisse créer un objet de ce type. En haut du fichier ModifViewController.m, ajoutez la ligne :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;Website.h&quot;</span></pre></div></div>

<p style="text-align: justify;">Puis nous allons créer un nouvel objet Website avec les valeurs du formulaire. Nous faisons ça au moment de sauvegarder : dans notre nouvelle méthode save:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> save<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Récupérer un NSUrl à partir de la valeur du champ texte</span>
	<span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>urlSite <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> alloc<span style="color: #002200;">&#93;</span> initWithString<span style="color: #002200;">:</span>urlTextField.text<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Créer un nouveau site et affecter les valeurs</span>
	Website <span style="color: #002200;">*</span>newSite <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Website alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
	newSite.webSiteTitle <span style="color: #002200;">=</span> titleTextField.text;
	newSite.webSiteURL <span style="color: #002200;">=</span> urlSite;
	newSite.webSiteDescription <span style="color: #002200;">=</span> descriptionTextField.text;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Ne pas oublier de libérer le NSUrl qu'on vient de créer</span>
	<span style="color: #002200;">&#91;</span>urlSite release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self dismissModalViewControllerAnimated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Il s&#8217;agit maintenant de rajouter notre nouveau site dans la liste des sites, mais comment ? La classe  ModifViewController ne connaît pas le tableau des sites qui est stocké dans RootViewController ! Qu&#8217;à cela ne tienne, nous allons passer ce tableau à notre classe ModifViewController qui pourra ainsi l&#8217;enrichir.</p>
<p style="text-align: justify;">Dans ModifViewController.h ajoutez un NSMutableArray *tabWebSites :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>tabWebSites;</pre></div></div>

<p style="text-align: justify;">Ajoutez la propriété pour y accéder :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>tabWebSites;</pre></div></div>

<p style="text-align: justify;">Synthétisez la propriété dans ModifViewController.m et n&#8217;oubliez pas le release.</p>
<p style="text-align: justify;">Dans la méthode <strong><em>addButtonPressed:</em></strong> de RootViewController.m, nous allons affecter notre tableau tabWebSites, comme ceci :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span> addButtonPressed<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> sender <span style="color: #002200;">&#123;</span>
	ModifViewController <span style="color: #002200;">*</span>addView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ModifViewController alloc<span style="color: #002200;">&#93;</span> initWithNibName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;ModifViewController&quot;</span> bundle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// On passe le tableau de sites pour que la nouvelle vue puisse le modifier</span>
	addView.tabWebSites <span style="color: #002200;">=</span> self.tabWebSites;
&nbsp;
	UINavigationController <span style="color: #002200;">*</span>navc <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UINavigationController alloc<span style="color: #002200;">&#93;</span> initWithRootViewController<span style="color: #002200;">:</span>addView<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self presentModalViewController<span style="color: #002200;">:</span>navc animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>addView release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>navc release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Enfin, il est maintenant temps d&#8217;ajouter notre site dans notre tableau. Retournons à la méthode <strong><em>save:</em></strong> et ajoutons les deux lignes suivantes :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">	<span style="color: #11740a; font-style: italic;">/*   ...*/</span>	
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Ajouter le site web</span>
	<span style="color: #002200;">&#91;</span>tabWebSites addObject<span style="color: #002200;">:</span>newSite<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>newSite release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self dismissModalViewControllerAnimated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Compilez, testez&#8230; Et&#8230; Ca ne plante pas, mais pas de nouveau site à l&#8217;horizon. <img src='http://www.tutomobile.fr/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </p>
<h3>Le site n&#8217;apparaît pas dans la liste, alors que je viens de l&#8217;ajouter</h3>
<p style="text-align: justify;">Il reste en effet une dernière petite chose à faire pour voir apparaître les fuits de votre labeur. Et c&#8217;est assez simple vous allez voir. Dans le paragraphe précédent, nous avons ajouté un objet dans notre tableau webSites, puis nous avons masqué la vue modal. Jamais nous n&#8217;avons averti la UITableView que son contenu avait changé. C&#8217;est pourtant une étape nécessaire : iOS ne va pas passer son temps à vérifier que quelque chose à été modifié, au risque de transformer votre iPhone en brouette. Nous allons ajouter une petite ligne de code dans la méthode <strong><em>viewWillAppear</em></strong>, qui, comme son nom l&#8217;indique, est appelée au moment où la vue va être affichée : quand elle est affichée suite au passage dans un UINavigationController, par exemple, ou quand une fenêtre modale s&#8217;efface pour laisser réapparaître la vue.</p>
<p style="text-align: justify;">Dans le code proposé par XCode à la création du projet, la méthode viewWillAppear est déjà codée, mais mise en commentaires. Enlevez les commentaires et modifiez la comme suit :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewWillAppear<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>animated <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>super viewWillAppear<span style="color: #002200;">:</span>animated<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self.tableView reloadData<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Cette fois c&#8217;est la bonne. Compilez et essayez d&#8217;ajouter un site : il vient s&#8217;ajouter en bas de la liste et on peut en voir un aperçu en touchant la ligne correspondante dans la liste.</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2011/02/Ajouter.png" rel="lightbox[1474]"><img class="aligncenter size-full wp-image-1661" title="Ajouter" src="http://www.tutomobile.fr/wp-content/uploads/2011/02/Ajouter.png" alt="" width="495" height="403" /></a></p>
<p style="text-align: justify;">C&#8217;est ainsi que s&#8217;achève cette seconde partie. La troisième et dernière partie arrive très vite. Nous verrons ensemble comment, très facilement, mettre en place le &laquo;&nbsp;<a href="http://www.apple.com/fr/iphone/tips/" target="_blank">Balayer pour supprimer</a>&laquo;&nbsp;, et surtout comment faire en sorte de sauvegarder vos nouveaux sites ou les modifications dans la mémoire de l&#8217;iPhone (parce pour le moment, si vous quittez, et bien vous avez tout perdu). Donc à très vite pour la suite <img src='http://www.tutomobile.fr/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<img src="http://feeds.feedburner.com/~r/TutoMobileTutorielsIphone/~4/HfHsXtpZlJY" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.tutomobile.fr/liste-et-details-creer-une-application-multivues-23-tutoriel-iphone-n%c2%b09/17/02/2011/feed/</wfw:commentRss>
		<slash:comments>32</slash:comments>
		<feedburner:origLink>http://www.tutomobile.fr/liste-et-details-creer-une-application-multivues-23-tutoriel-iphone-n%c2%b09/17/02/2011/</feedburner:origLink></item>
		<item>
		<title>Liste et détails : créer une application multivues (1/3) [Tutoriel iPhone n°8] [XCode 4]</title>
		<link>http://feedproxy.google.com/~r/TutoMobileTutorielsIphone/~3/1Fd6NspD8tM/</link>
		<comments>http://www.tutomobile.fr/liste-et-details-creer-une-application-multivues-12-tutoriel-iphone-n%c2%b08/09/11/2010/#comments</comments>
		<pubDate>Tue, 09 Nov 2010 23:01:56 +0000</pubDate>
		<dc:creator>Axon</dc:creator>
				<category><![CDATA[Tutoriels iPhone]]></category>
		<category><![CDATA[Disclosure]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[UINavigationController]]></category>
		<category><![CDATA[UITableCell]]></category>
		<category><![CDATA[UITableView]]></category>
		<category><![CDATA[UITableViewDataSource]]></category>
		<category><![CDATA[UIViewController]]></category>
		<category><![CDATA[Xcode]]></category>
		<category><![CDATA[XCode 4]]></category>

		<guid isPermaLink="false">http://www.tutomobile.fr/?p=1235</guid>
		<description><![CDATA[Nouveau (mai 2011) : Ce]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;"><strong><em><span style="color: #ff6600;">Nouveau (mai 2011) : Ce tutoriel a été mis à jour et été testé pour XCode 4.</span></em></strong></p>
<p style="text-align: justify;">Tous les utilisateurs d&#8217;iPhone sont habitués à naviguer dans des listes, à toucher un élément pour afficher le détail d&#8217;un objet, et utiliser la barre de navigation supérieure pour revenir à l&#8217;écran principal. Ce fonctionnement est homogène dans toutes les applications proposées en standard par Apple sur les appareils exécutant iOS (iPhone, iPod Touch, iPad), mais pas seulement. En effet, Cocoa Touch facilite considérablement la vie du développeur qui souhaite programmer une App dans cette idée, ce qui explique l&#8217;omniprésence d&#8217;une telle approche.</p>
<p><span id="more-1235"></span></p>
<p style="text-align: justify;">Ce tutoriel sera séparée en trois parties. La première vous proposera un pas à pas pour construire une liste à partir d&#8217;un fichier à plat, puis détaillera le fonctionnement pour ouvrir une fenêtre de détails. Dans la seconde, nous verrons comment enrichir le contenu de l&#8217;application et aborderons les fenêtres modales. Enfin dans la troisième et dernière, nous verrons l&#8217;enchaînement d&#8217;objet à mettre en place pour enregistrer les modifications dans la mémoire de l&#8217;iPhone et verrons comment, en quelques clics, nous pouvons utiliser les comportements avancés des tableaux.</p>
<h3>L&#8217;objectif</h3>
<p>Avant de commencer, je souhaiterais vous montrer où nous souhaitons arriver à la fin de ce tutoriel. Notre application a pour ambition de présenter des sites internet de référence. Cette liste de sites sera stockée dans un fichier et présentée à l&#8217;utilisateur sous forme d&#8217;une table avec le titre du site et le début de la description. En touchant une ligne, l&#8217;utilisateur accèdera à une vue de détails présentant la description complète et une webview du site en question.</p>
<p><a href="http://www.tutomobile.fr/wp-content/uploads/2010/11/Capture-d’écran-2010-11-09-à-23.24.58.png" rel="lightbox[1235]"><img class="aligncenter size-full wp-image-1287" title="Capture d’écran 2010-11-09 à 23.24.58" src="http://www.tutomobile.fr/wp-content/uploads/2010/11/Capture-d’écran-2010-11-09-à-23.24.58.png" alt="" width="600" height="410" /></a></p>
<h3>Créer le projet</h3>
<p style="text-align: justify;">On est parti ! Lancez XCode, démarrez la création d&#8217;un nouveau projet et choisissez une &laquo;&nbsp;<strong><em>Navigation-based application&nbsp;&raquo;</em><span style="font-weight: normal;">. </span></strong>Ce template, nous ne l&#8217;avons pas utilisé dans les tutoriels précédents parce qu&#8217;il va préparer pas mal de choses pour vous :</p>
<ul>
<li style="text-align: justify;">une première classe <strong>RootViewController</strong> (un .m, un .h et un fichier InterfaceBuilder .xib)</li>
<li style="text-align: justify;">cette classe sera instanciée dans un <strong>UINavigationController</strong> (nous l&#8217;avions créé nous-mêmes dans le <a href="http://www.tutomobile.fr/gerer-la-navigation-entre-plusieurs-vues-uinavigationcontroller-tutoriel-iphone-n°5/14/08/2010/" target="_self">tutoriel n°5</a>)</li>
<li style="text-align: justify;">de plus cette classe RootViewController est une sous-classe de <strong>UITableViewController</strong>, ce qui signifie qu&#8217;elle propose, par défaut, une liste d&#8217;éléments.</li>
</ul>
<p style="text-align: justify;">Appelez ce projet &laquo;&nbsp;MyWebSites&nbsp;&raquo;. Vous pouvez le compiler tout de suite. Vous verrez alors apparaître un écran présentant la barre bleue de navigation en haut et une liste, vide, mais qui réagit déjà au toucher (vous pouvez &laquo;&nbsp;scroller&nbsp;&raquo; dedans).</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/11/Première-compilation.png" rel="lightbox[1235]"><img class="aligncenter size-medium wp-image-1285" title="Première compilation" src="http://www.tutomobile.fr/wp-content/uploads/2010/11/Première-compilation-161x300.png" alt="" width="161" height="300" /></a></p>
<p style="text-align: justify;">Personnalisons cela rapidement. Ouvrez MainWindow.xib, accédez au &laquo;&nbsp;Navigation Controller&nbsp;&raquo; dans la barre de gauche. Vous remarquez que la vue qui sera affichée dans un premier temps est gérée par <strong><em>RootViewController</em></strong>. Double-cliquez dans la barre bleue pour mettre le titre en surbrillance et le modifier selon votre choix. Ceci revient à modifier la propriété title, comme nous l&#8217;avions vu dans le tutoriel précédent, et comme nous n&#8217;allons pas tarder à le faire pour la vue de détails.</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/11/Changer-le-titre-de-la-vue.png" rel="lightbox[1235]"><img class="aligncenter size-full wp-image-1837" title="Changer le titre de la vue" src="http://www.tutomobile.fr/wp-content/uploads/2010/11/Changer-le-titre-de-la-vue.png" alt="" width="250" height="302" /></a></p>
<h3>Ajouter un fichier .plist contenant les informations de sites</h3>
<p style="text-align: justify;">Cette liste est bien vide, il est temps de trouver des données à y présenter. Apple propose plusieurs façons de stocker des données, comme des fichiers textes, des accès à une base de données SQLite, les mettre sur un serveur Internet et les récupérer, etc&#8230; Mais il y a mieux. Les <em><strong>fichiers de propriétés</strong></em> (les fameux .plist) sont des fichiers XML qui présentent de nombreux avantages dont celui d&#8217;être supportés nativement par les objets du framework (NSArray, NSDictionnary, &#8230;) ; cela signifie que nous allons pouvoir récupérer avec une simple ligne de code des objets entiers depuis un fichier .plist. La seconde option est <em><strong>CoreData</strong></em>, que nous aborderons en détails dans un prochain tutoriel.</p>
<p style="text-align: justify;">Assez parlé ! Pour créer un fichier .plist qui contiendra les données de notre liste de sites Internet favoris, utilisez la commande &laquo;&nbsp;New File&#8230;&nbsp;&raquo; du menu File, et choisissez &laquo;&nbsp;Property List&nbsp;&raquo; qui se trouve dans la rubrique &laquo;&nbsp;Ressource&nbsp;&raquo;. Donnez-lui un nom (j&#8217;ai appelé le mien &laquo;&nbsp;websites.plist&nbsp;&raquo;) et retrouvez-le dans l&#8217;arborescence de XCode (il est en fin de projet).</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/11/Nouveau-plist1.png" rel="lightbox[1235]"><img class="aligncenter size-full wp-image-1839" title="Nouveau plist" src="http://www.tutomobile.fr/wp-content/uploads/2010/11/Nouveau-plist1.png" alt="" width="400" height="269" /></a></p>
<p style="text-align: justify;">Ici, nul besoin de connaître le moindre début de XML, XCode s&#8217;occupe de tout et vous présente le contenu du fichier dans une interface super simple et efficace à utiliser. Notre objectif, ici : obtenir un tableau d&#8217;objets (NSArray), ces objets étant des dictionnaires clé-valeur (NSDictionnary) permettant, pour chaque site, de préciser : le titre du site, son URL et une description à afficher.</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/11/schéma.png" rel="lightbox[1235]"><img class="aligncenter size-full wp-image-1241" title="schéma du fichier plist" src="http://www.tutomobile.fr/wp-content/uploads/2010/11/schéma.png" alt="" width="602" height="510" /></a></p>
<p style="text-align: justify;">Bon je m&#8217;épargne de vous expliquer par le menu comment fonctionne l&#8217;interface (petite astuce : clic-droit pour ajouter les premières lignes, après, vous avez droit à des icônes &laquo;&nbsp;plus&nbsp;&raquo; et &laquo;&nbsp;moins&nbsp;&raquo;. Voici le résultat final : un NSArray contenant des objets de types NSDictionnary ayant tous la même structure : trois NSStrings title, url et description. Courage !</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/11/Plist-contenu.png" rel="lightbox[1235]"><img class="aligncenter size-medium wp-image-1242" title="Contenu du fichier .plist" src="http://www.tutomobile.fr/wp-content/uploads/2010/11/Plist-contenu-300x166.png" alt="" width="300" height="166" /></a></p>
<p style="text-align: justify;">(Cliquez sur l&#8217;image pour voir le résultat attendu)</p>
<p style="text-align: justify;">
<h3>Créer une classe modèle</h3>
<p style="text-align: justify;">Il est temps de coder un peu et de nous attaquer à la partie &laquo;&nbsp;modèle&nbsp;&raquo; de notre <a href="http://www.tutomobile.fr/model-view-controller/17/10/2010/" target="_self">fameux MVC</a>. Nous allons faire une &laquo;&nbsp;classe métier&nbsp;&raquo; pour représenter chacun des sites. Cette classe a pour objectifs :</p>
<ul>
<li>de représenter un site (avec son titre, son URL, sa description)</li>
<li>de prendre en charge les actions à mener sur un site</li>
</ul>
<p style="text-align: justify;">Nous aurons, par la suite, besoin de créer un objet métier pour chacun des NSDictionnary détaillé dans notre fichier .plist. Il faut donc créer pour cette classe :</p>
<ul>
<li>Trois attributs (titre, URL et description)</li>
<li>Trois propriétés pour pouvoir accéder aux valeurs des attributs</li>
<li>Une méthode d&#8217;initialisation à partir d&#8217;un NSDictionnary</li>
</ul>
<p style="text-align: justify;">Pour créer la nouvelle classe, choisissez &laquo;&nbsp;New File&#8230;&nbsp;&raquo; dans le menu &laquo;&nbsp;File&nbsp;&raquo; et choisissez une &laquo;&nbsp;Objective-C class&nbsp;&raquo; simple. Appelez-la Website, XCode se chargera de créer les deux fichier .h (en-tête) et .m (implémentation).</p>
<p style="text-align: justify;">Dans le fichier .h, nous allons créer les trois attributs, déclarer les propriétés ainsi que la méthode d&#8217;initialisation :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import UIKit/UIKit.h</span>
&nbsp;
<span style="color: #a61390;">@interface</span> Website <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>webSiteTitle;
    <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>webSiteURL;
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>webSiteDescription;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>webSiteTitle;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>webSiteURL;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>webSiteDescription;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initWithDictionaryFromPlist<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> dictionnary;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p style="text-align: justify;">Dans le fichier .m, nous allons synthétiser les trois propriétés (sans oublier de faire les release correspondants dans la méthode dealloc) et implémenter la méthode initWithDictionaryFromPlist :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;Website.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> Website
&nbsp;
<span style="color: #a61390;">@synthesize</span> webSiteTitle;
<span style="color: #a61390;">@synthesize</span> webSiteURL;
<span style="color: #a61390;">@synthesize</span> webSiteDescription;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span> initWithDictionaryFromPlist<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> dictionnary <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>self init<span style="color: #002200;">&#93;</span>;
&nbsp;
    self.webSiteTitle <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dictionnary objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;title&quot;</span><span style="color: #002200;">&#93;</span>;
    self.webSiteURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>dictionnary objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;url&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
    self.webSiteDescription <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dictionnary objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;description&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>webSiteTitle release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>webSiteURL release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>webSiteDescription release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p style="text-align: justify;">Vous pouvez essayer de compiler, mais pour le moment rien n&#8217;a changé, ceci permettra simplement de corriger les éventuelles petites fautes de frappe.</p>
<h3>Charger le contenu dans la liste</h3>
<p style="text-align: justify;">Il est largement temps de faire en sorte que cette liste se remplisse avec les sites que nous avons saisis dans notre fichier .plist. Retournons sur la classe RootViewController, et plus précisément dans le fichier header (.h) pour ajouter un attribut : <strong><em>tabWebSites</em></strong> qui contiendra tous les objets que nous allons charger de notre fichier .plist</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import UIKit/UIKit.h</span>
&nbsp;
<span style="color: #a61390;">@interface</span> RootViewController <span style="color: #002200;">:</span> UITableViewController <span style="color: #002200;">&#123;</span>
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>tabWebSites;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>tabWebSites;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p style="text-align: justify;">Dans le fichier .m, ajoutez les éléments suivants (ça doit commencer à devenir un reflexe quand vous déclarez une propriété) :</p>
<ul>
<li>le @synthetise pour la propriété</li>
<li>le message release dans la méthode dealloc de la classe</li>
</ul>
<p style="text-align: justify;">(il y a un morceau de code similaire un peu plus haut dans le tutoriel).</p>
<p style="text-align: justify;">Intéressons-nous maintenant à la méthode <strong><em>viewDidLoad</em></strong> de RootViewController.m. Cette méthode est appelée une fois que la vue est chargée, c&#8217;est à dire, dans notre cas, une fois que la liste est préparée en mémoire pour que nous puissions y ajouter des éléments. Elle est déjà pré-codée par XCode lors de la création du projet. Repérez-la : elle est en général quasi en haut du fichier.</p>
<p style="text-align: justify;">C&#8217;est dans cette méthode que nous allons :</p>
<ul>
<li>Charger le fichier .plist et le parcourir</li>
<li>Créer un objet Website à partir de chaque NSDictionnary extrait du fichier .plist</li>
<li>Enregistrer ces objets Website dans le tableau tabWebSites que nous venons de créer</li>
</ul>
<p>Voici le code à implémenter, j&#8217;ai détaillé les trois points :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;RootViewController.h&quot;</span>
<span style="color: #6e371a;">#import &quot;Website.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> RootViewController
<span style="color: #a61390;">@synthesize</span> tabWebSites;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Charger le fichier .plist dans un tableau que l'on appelera  arrayFromFile</span>
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>path <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> pathForResource<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;websites&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;plist&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>dictFromFile <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> alloc<span style="color: #002200;">&#93;</span> initWithContentsOfFile<span style="color: #002200;">:</span>path<span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>arrayFromFile <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>dictFromFile objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Root&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Créons un tableau temporaire que nous allons remplir avec un objet Website par NSDictionnary contenu dans le fichier .plist</span>
    <span style="color: #11740a; font-style: italic;">// Notez l'utilisation de NSEnumerator pour parcourir un tableau</span>
    <span style="color: #400080;">NSMutableArray</span> <span style="color: #002200;">*</span>websitesToAdd <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableArray</span> alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSEnumerator</span> <span style="color: #002200;">*</span>enumerator <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>arrayFromFile objectEnumerator<span style="color: #002200;">&#93;</span>;
    <span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>anObject;
    <span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>anObject <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>enumerator nextObject<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        Website <span style="color: #002200;">*</span>ws <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>Website alloc<span style="color: #002200;">&#93;</span> initWithDictionaryFromPlist<span style="color: #002200;">:</span> anObject<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>websitesToAdd addObject<span style="color: #002200;">:</span> ws<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>ws release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Remplir la propriété tabWebSites avec le contenu du NSMutableArray précédent</span>
    self.tabWebSites <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithArray<span style="color: #002200;">:</span>websitesToAdd<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Gestion de la mémoire : pour chaque alloc, n'oubliez pas le release qui va avec !</span>
    <span style="color: #002200;">&#91;</span>websitesToAdd release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>arrayFromFile release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// [...]</span></pre></div></div>

<p>Compilez, et tadaaa ! Non toujours rien ? Bon, c&#8217;est normal : on a chargé notre tableau, mais on a rien demandé à afficher.</p>
<h3>En savoir plus sur UITableView</h3>
<p style="text-align: justify;">XCode a déjà préparé pas mal de méthodes dans le fichier RootViewController.m qui n&#8217;attendent qu&#8217;à être activée et personnalisées. Avant de faire en sorte que nos sites apparaissent vraiment dans la liste, laissez-moi vous parler un peu plus de UITableView (cette magnifique liste).</p>
<p style="text-align: justify;">Le composant UITableView propose une des façons les plus simples et les plus rapides pour traiter les données : le tableau, et plus précisément la liste. Un <strong><em>UITableView</em></strong> propose en fait un ensemble de cellules (les UITableViewCell) qui sont organisées en sections :</p>
<p><a href="http://www.tutomobile.fr/wp-content/uploads/2010/11/Design-UITableView.png" rel="lightbox[1235]"><img class="aligncenter size-full wp-image-1273" title="Design UITableView" src="http://www.tutomobile.fr/wp-content/uploads/2010/11/Design-UITableView.png" alt="" width="613" height="407" /></a></p>
<p style="text-align: justify;">Pour présenter des données, il ne s&#8217;agit pas de &laquo;&nbsp;charger&nbsp;&raquo; un UITableView en mettant des données dedans (comme dans un tableau NSArray, par exemple). Un UITableView a besoin d&#8217;un objet qui l&#8217;accompagne : sa source de données : un <strong><em>DataSource</em></strong>. N&#8217;importe quel objet peut être un DataSource, il suffit pour cela qu&#8217;il implémente le protocole : <a href="http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableViewDataSource_Protocol/Reference/Reference.html" target="_blank">UITableViewDataSource</a>. Lorsqu&#8217;il sera temps d&#8217;afficher le contenu d&#8217;une cellule, par exemple celle de la troisième ligne de la première section, le UITableView demandera alors à son DataSource : &laquo;&nbsp;Qu&#8217;est-ce que je dois afficher dans la ligne 3 de la section 1 ?&nbsp;&raquo;.</p>
<p style="text-align: justify;">XCode a bien fait les choses quand il a préparé notre projet. Le DataSource de notre UITableView est déjà déclaré : c&#8217;est notre classe <strong><em>RootViewController</em></strong>. Pour s&#8217;en convaincre, il suffit d&#8217;aller dans RootViewController.h, constater que cette classe étend la classe UITableViewController. Si vous appuyez sur Commande (Pomme) et cliquez sur &laquo;&nbsp;UITableViewController&nbsp;&raquo;, l&#8217;écran présentera la déclaration de cette classe, et vous verrez qu&#8217;elle implémente bien le protocole UITableViewDataSource.</p>
<h3>Afficher les données dans la liste</h3>
<p style="text-align: justify;">Les méthodes du protocole UITableViewDataSource que nous avons à implémenter pour afficher les informations sont numberOfSectionsInTableView, numberOfRowsInSection et cellForRowAtIndexPath, elles sont déjà préparées dans RootViewController.m. Voici le code, commenté pour détailler les actions mises en place :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Customize the number of sections in the table view.</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>numberOfSectionsInTableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// On n'a besoin que d'une section pour nos sites Internet</span>
    <span style="color: #a61390;">return</span> <span style="color: #2400d9;">1</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>section <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Nous ne tenons pas compte du numéro de section puisqu'il n'y en a qu'une</span>
    <span style="color: #11740a; font-style: italic;">// Dans cette unique section il y a tous les éléments du tableau, on retourne donc le nombre</span>
    <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>self.tabWebSites count<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Customize the appearance of table view cells.</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
&nbsp;
    <span style="color: #a61390;">static</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>CellIdentifier <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Cell&quot;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Les cellules sont mises en cache pour accélérer le traitement, sous l'identifiant &quot;Cell&quot;,</span>
    <span style="color: #11740a; font-style: italic;">// on essaie récupère ce modèle de cellule s'il est déjà en cache</span>
    UITableViewCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Si on n'a pas réussi à sortir une cellule du cache, on crée un nouveau modèle de cellule</span>
    <span style="color: #11740a; font-style: italic;">// et on l'enregistre dans le cache</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>cell <span style="color: #002200;">==</span> <span style="color: #a61390;">nil</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithStyle<span style="color: #002200;">:</span>UITableViewCellStyleSubtitle reuseIdentifier<span style="color: #002200;">:</span>CellIdentifier<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// On récupère l'objet Website qui correspon à la ligne que l'on souhaite afficher</span>
    Website <span style="color: #002200;">*</span>ws <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.tabWebSites objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// On configure la cellule avec le titre du site et sa description</span>
    cell.textLabel.text <span style="color: #002200;">=</span> ws.webSiteTitle;
    cell.detailTextLabel.text <span style="color: #002200;">=</span> ws.webSiteDescription;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// On renvoie la cellule configurée pour l'affichage</span>
    <span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Il est temps de compiler tout cela, et de faire notre premier test. Si tout va bien, voilà le résultat affiché :</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/11/Première-vue-sans-détails.png" rel="lightbox[1235]"><img class="aligncenter size-medium wp-image-1275" title="Première vue sans détails" src="http://www.tutomobile.fr/wp-content/uploads/2010/11/Première-vue-sans-détails-161x300.png" alt="" width="161" height="300" /></a></p>
<h3>Créer une vue de détail</h3>
<p style="text-align: justify;">Nous allons maintenant créer la vue qui sera affichée quand on touchera un élément de la liste. Il s&#8217;agit d&#8217;un UIViewController que nous allons pousser, plus tard, dans le UINavigationController. On va aller un peut plus vite pour cette partie, parce que, si vous vous êtes déjà frottés <a href="http://www.tutomobile.fr/category/tutorial-iphone/" target="_blank">aux tutoriels précédents</a>, ça devrait être une partie de plaisirs.</p>
<p style="text-align: justify;">Créez une nouvelle classe, héritée de UIViewController, appelez-la <strong><em>DetailViewController</em></strong> et n&#8217;oubliez pas d&#8217;associer un fichier .h et un fichier .xib. Dans DetailViewController.h, créez trois attributs et autant de propriétés :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import</span>
<span style="color: #6e371a;">#import &quot;Website.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> DetailViewController <span style="color: #002200;">:</span> UIViewController <span style="color: #002200;">&#123;</span>
	IBOutlet UITextView <span style="color: #002200;">*</span>descriptionTextView;
	IBOutlet UIWebView <span style="color: #002200;">*</span>webView;
	Website <span style="color: #002200;">*</span>ws;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span> UIWebView <span style="color: #002200;">*</span>webView;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span> UITextView <span style="color: #002200;">*</span>descriptionTextView;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic,retain<span style="color: #002200;">&#41;</span> Website <span style="color: #002200;">*</span>ws;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p style="text-align: justify;">Dans le fichier .m, synthétisez les trois propriétés, n&#8217;oubliez pas les release de rigueur dans la méthode dealloc et personnalisez la méthode viewDidLoad pour personnaliser l&#8217;affichage de la vue et charger la page dans la webview :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;DetailViewController.h&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> DetailViewController
<span style="color: #a61390;">@synthesize</span> descriptionTextView, ws, webView;
&nbsp;
<span style="color: #11740a; font-style: italic;">/*...*/</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>webView release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>descriptionTextView release<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>ws release<span style="color: #002200;">&#93;</span>;	
&nbsp;
    <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*...*/</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Titre du navigation controller</span>
    self.title <span style="color: #002200;">=</span> ws.webSiteTitle;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Afficher le contenu de la page web</span>
    <span style="color: #400080;">NSURLRequest</span> <span style="color: #002200;">*</span>requestObject <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLRequest</span> requestWithURL<span style="color: #002200;">:</span> ws.webSiteURL<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>webView loadRequest<span style="color: #002200;">:</span>requestObject<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Affecter le champ de description avec la description du site</span>
    descriptionTextView.text <span style="color: #002200;">=</span> ws.webSiteDescription;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p style="text-align: justify;">Enfin, ouvrez le fichier .xib : ajoutez, un <strong><em>UIWebView</em></strong> en bas de la vue, ainsi qu&#8217;un <strong><em>UITextView</em></strong> qui présentera la description, au dessus de la vue Web.</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/11/Bibliothèque-déléments-à-ajouter.png" rel="lightbox[1235]"><img class="aligncenter size-full wp-image-1840" title="Bibliothèque d'éléments à ajouter" src="http://www.tutomobile.fr/wp-content/uploads/2010/11/Bibliothèque-déléments-à-ajouter.png" alt="" width="303" height="391" /></a></p>
<p style="text-align: justify;">Cliquez-droit sur l&#8217;objet &laquo;&nbsp;File&#8217;s owner&nbsp;&raquo; dans la colonne de gauche pour ouvrir l&#8217;outil vous permettant de faire les connexions. Connectez la propriété <strong><em>descriptionTextView</em></strong> :</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/11/Associer-la-TextView.png" rel="lightbox[1235]"><img class="aligncenter size-full wp-image-1841" title="Associer la TextView" src="http://www.tutomobile.fr/wp-content/uploads/2010/11/Associer-la-TextView.png" alt="" width="400" height="191" /></a></p>
<p style="text-align: justify;">Ainsi que l&#8217;attribut <strong><em>webview</em></strong> de notre objet :</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/11/Associer-la-webView.png" rel="lightbox[1235]"><img class="aligncenter size-full wp-image-1842" title="Associer la webView" src="http://www.tutomobile.fr/wp-content/uploads/2010/11/Associer-la-webView.png" alt="" width="400" height="289" /></a></p>
<p style="text-align: justify;">A l&#8217;aide l&#8217;inspecteur de la colonne de droite, personnalisez l&#8217;affichage des deux éléments (n&#8217;oubliez pas, par exemple, de cocher &laquo;&nbsp;Scales Page To Fit&nbsp;&raquo; pour la WebView). Sauvegardez le tout avant de continuer.</p>
<h3>Afficher les bonnes données dans la vue de détail</h3>
<p style="text-align: justify;">Quand l&#8217;utilisateur touche un site de la liste, il faut que la popup de détails soit poussée dans le UINavigationController. Nous avons vu ce mode de fonctionnement dans le <a href="http://www.tutomobile.fr/gerer-la-navigation-entre-plusieurs-vues-uinavigationcontroller-tutoriel-iphone-n°5/14/08/2010/">tutoriel numéro 5</a> : s&#8217;agit d&#8217;instancier une classe <strong><em>UIViewController</em></strong> et d&#8217;utiliser la méthode <strong><em>pushViewController</em></strong>. Retournez donc sur le code de RootViewController.m.</p>
<p style="text-align: justify;">Commencez par créer un lien vers la nouvelle classe avec la commande #import :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;DetailViewController.h&quot;</span></pre></div></div>

<p style="text-align: justify;">Puis repérez la méthode <strong><em>didSelectRowAtIndexPath</em></strong> qui est appelée quand l&#8217;utilisateur touche une cellule du UITableView. Cette méthode prend en paramètre un objet <strong><em>NSIndexPath</em></strong> qui contient, dans sa propriété row, le numéro de la ligne qui a été touchée (et qui correspond, de fait à la position dans notre tableau tabWebSites). Modifiez la comme suit :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView didSelectRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath <span style="color: #002200;">&#123;</span>
	DetailViewController <span style="color: #002200;">*</span>detailVC <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>DetailViewController alloc<span style="color: #002200;">&#93;</span> initWithNibName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;DetailViewController&quot;</span> bundle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
	detailVC.ws <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.tabWebSites objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self.navigationController pushViewController<span style="color: #002200;">:</span>detailVC animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>detailVC release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Il est presque temps de compiler et d&#8217;admirer le résultat ! Mais juste avant, une petite remarque d&#8217;importance : si par hasard, vous souhaitez mettre cette application à disposition sur l&#8217;AppStore, il y a de fortes chances qu&#8217;elle soit refusée parce que qu&#8217;elle n&#8217;est pas conforme aux HIG : les fameuses et strictes iPhone Human Interface Guidelines. Je vous laisse trouver toute cette documentation dans XCode, et j&#8217;en profite vous parler des <strong><em>disclosure indicators elements</em></strong>. En effet, quand vous souhaitez indiquer à l&#8217;utilisateur que toucher une ligne va le faire avancer dans la hiérarchie des vues, vous devez le lui indiquer avec un Disclosure Indicator.</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/11/Disclosure.png" rel="lightbox[1235]"><img class="aligncenter size-full wp-image-1288" title="Disclosure" src="http://www.tutomobile.fr/wp-content/uploads/2010/11/Disclosure.png" alt="" width="354" height="70" /></a></p>
<p style="text-align: justify;">Pour ajouter ce petit point de détail (mais qui a son importance), il nous faut ajouter une dernière ligne dans la méthode <strong>cellForRowAtIndexPath</strong> de notre <strong>RootViewController</strong> (juste avant le return, par exemple) :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">cell.accessoryType <span style="color: #002200;">=</span> UITableViewCellAccessoryDisclosureIndicator;</pre></div></div>

<p style="text-align: justify;">Cette fois c&#8217;est terminé ! Vous pouvez compiler et admirer le résultat ! <img src='http://www.tutomobile.fr/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p style="text-align: justify;">
<p style="text-align: justify;">Dans la seconde partie, nous parlerons de la modification des éléments de la liste. Vous verrez que ce n&#8217;est pas beaucoup plus difficile et qu&#8217;il suffit juste d&#8217;être un peu rigoureux pour ne pas perdre des modifications en route. Vous apprendrez, en outre, à gérer le magnifique glissé du doigt pour supprimer une ligne ! En attendant, n&#8217;hésitez pas à utiliser les commentaires si vous rencontrez des difficultés, et surtout, accrochez-vous : en général, la solution n&#8217;est pas très loin ! <img src='http://www.tutomobile.fr/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p style="text-align: justify;">Merci pour vos commentaires et encouragements ! A très vite !</p>
<img src="http://feeds.feedburner.com/~r/TutoMobileTutorielsIphone/~4/1Fd6NspD8tM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.tutomobile.fr/liste-et-details-creer-une-application-multivues-12-tutoriel-iphone-n%c2%b08/09/11/2010/feed/</wfw:commentRss>
		<slash:comments>46</slash:comments>
		<feedburner:origLink>http://www.tutomobile.fr/liste-et-details-creer-une-application-multivues-12-tutoriel-iphone-n%c2%b08/09/11/2010/</feedburner:origLink></item>
		<item>
		<title>Model-View-Controller : le design pattern de référence du dev. iPhone</title>
		<link>http://feedproxy.google.com/~r/TutoMobileTutorielsIphone/~3/SVivU3rnEoE/</link>
		<comments>http://www.tutomobile.fr/model-view-controller/17/10/2010/#comments</comments>
		<pubDate>Sun, 17 Oct 2010 19:00:41 +0000</pubDate>
		<dc:creator>Axon</dc:creator>
				<category><![CDATA[Tutoriels iPhone]]></category>
		<category><![CDATA[Controler]]></category>
		<category><![CDATA[Design Pattern]]></category>
		<category><![CDATA[Développement]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[Model]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[View]]></category>

		<guid isPermaLink="false">http://www.tutomobile.fr/?p=1185</guid>
		<description><![CDATA[Si vous vous promenez sur]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Si vous vous promenez sur les documentations Apple, sur les autres sites dédiés au développement iPhone ou Mac, ou encore en regardant simplement les codes sources des applis, vous repérerez vite trois termes qui, répétés tels des <a href="http://fr.wikipedia.org/wiki/Mantra" target="_blank">Mantras</a> de l&#8217;informatique moderne, semblent tout aussi puissants que mystérieux : <em><strong>Model</strong></em><strong> </strong>, <strong><em>View</em></strong> et <strong><em>Controller</em></strong>.<br />
Après 7 tutoriels iPhone, il est grand temps de faire une petite pause un peu plus théorique dans nos développements et essayer de prendre la mesure de ce design pattern à la base de tout développement sur iOS.</p>
<p><span id="more-1185"></span></p>
<h3>Qu&#8217;est-ce qu&#8217;un design pattern ?</h3>
<p style="text-align: justify;">La définition la plus courante d&#8217;un pattern vous laissera probablement sur votre faim :<br />
<em><strong>Un pattern est une solution à un problème dans un contexte.</strong></em></p>
<p style="text-align: justify;">Soyons plus précis : dans une situation donnée, vous, développeur, avez un but défini que vous souhaitez atteindre. Mais beaucoup d&#8217;autres développeurs ont déjà eu cette même problématique. Le design pattern est une conception générique que tout le monde peut appliquer pour atteindre ce but, dans ce contexte. Un design pattern est donc mieux qu&#8217;une &laquo;&nbsp;best practice&nbsp;&raquo;, c&#8217;est une  conception idéale, testée et approuvée par vos pairs.</p>
<p style="text-align: justify;">Il existe en fait une petite vingtaine de patterns principaux (Stratégie, Singleton, Commande, Patron de méthode, Proxy &#8230;), que nous n&#8217;allons pas détailler ici mais qui sont extrêmement utiles et font partie de la boîte à outil quotidienne du parfait analyste programmeur, chef de projet technique ou architecte logiciel. Une biblio est disponible à la fin de cet article si vous souhaitez creuser la question.</p>
<p style="text-align: justify;">Retenons donc : un design pattern est un <strong><em>modèle de conception éprouvé</em></strong> pour un problème dans un contexte donné. Son utilisation permet de garantir l&#8217;indépendance et la réutilisabilité des objets, mais aussi leur bonne évolution.</p>
<p style="text-align: justify;">
<p style="text-align: justify;">
<h3>Je vous présente, MVC : pas un design pattern, LE design pattern</h3>
<p style="text-align: justify;">
<p style="text-align: justify;">
<p style="text-align: justify;">Le design pattern MVC pour Modèle-Vue-Contrôleur est un pattern composé : c&#8217;est à dire qu&#8217;il met en oeuvre trois patterns plus simples (<a href="http://fr.wikipedia.org/wiki/Stratégie_(patron_de_conception)" target="_blank">Stratégie</a>, <a href="http://fr.wikipedia.org/wiki/Observateur_(patron_de_conception)" target="_blank">Observateur</a> et <a href="http://fr.wikipedia.org/wiki/Objet_composite" target="_blank">Composite</a>) mais que nous ne détaillerons pas ici. A vrai dire c&#8217;est le roi des patterns composés : mis au point en 1979 dans les laboratoires de recherche Xerox PARC, il décrit une architecture qui organise les interfaces homme machine en trois parties indépendantes et réutilisables :</p>
<ul>
<li>le <strong><em>modèle</em></strong> : le modèle de données, la logique de l&#8217;application</li>
<li>la <strong><em>vue</em></strong> : présentation des données et interactions avec l&#8217;utilisateur</li>
<li>le <strong><em>contrôleur</em></strong> : logique de contrôle, gestion des événements, mise à jour du modèle et/ou de la vue</li>
</ul>
<p style="text-align: justify;">MVC est utilisé partout dans Cocoa Touch. De façon plus générale, il est au coeur de Cocoa, comme il était au coeur de NextStep et du langage SmallTalk. C&#8217;est même devenu un tube, la preuve : <a href="http://www.youtube.com/watch?v=YYvOGPMLVDo" target="_blank">http://www.youtube.com/watch?v=YYvOGPMLVDo</a></p>
<h3>La vue d&#8217;un côté, le modèle de l&#8217;autre : le contrôleur fait le lien</h3>
<p style="text-align: justify;">Pour une réutilisabilité maximum et garantir l&#8217;évolution des objets, il faut des frontières claires. C&#8217;est ce qu&#8217;impose le MVC. L&#8217;objet contrôleur devient alors le ciment et le lien pour afficher dans la vue les données du modèle. Un peu comme ça :</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/10/Macaron-MVC.png" rel="lightbox[1185]"><img class="aligncenter size-full wp-image-1191" title="Macaron MVC" src="http://www.tutomobile.fr/wp-content/uploads/2010/10/Macaron-MVC.png" alt="" width="387" height="364" /></a></p>
<p style="text-align: justify;">En formalisant les choses de manière moins gloutonne plus professionnelle :</p>
<p><a href="http://www.tutomobile.fr/wp-content/uploads/2010/10/MVC-Pro.png" rel="lightbox[1185]"><img class="aligncenter size-full wp-image-1192" title="MVC Pro" src="http://www.tutomobile.fr/wp-content/uploads/2010/10/MVC-Pro.png" alt="" width="515" height="274" /></a></p>
<ol>
<li style="text-align: justify;">L&#8217;utilisateur interagit avec la vue, en saisissant une valeur ou en cliquant sur un bouton, par exemple</li>
<li style="text-align: justify;">Le contrôleur reçoit les actions de l&#8217;utilisateur, les interprète et demande au modèle de modifier son état</li>
<li style="text-align: justify;">De même, suite à cette action, le contrôleur peut avoir besoin de demander à la vue de changer en conséquence (activer ou désactiver un champ, etc.)</li>
<li style="text-align: justify;">Le modèle informe la vue de son changement d&#8217;état, suite à une modification liée à un clic de bouton ou même à une modification interne (fin d&#8217;un chargement, etc.)</li>
<li style="text-align: justify;">La vue demande son état au modèle, que ce soit suite à une information du modèle ou du contrôleur</li>
</ol>
<h3>Un exemple plus concret pour fixer les idées</h3>
<p style="text-align: justify;">C&#8217;est encore un peut abstrait ? Prenons l&#8217;exemple d&#8217;un lecteur MP3, comme celui-ci :</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/10/Lecteur-MP3-Julie-Lenne.png" rel="lightbox[1185]"><img class="aligncenter size-full wp-image-1194" title="Lecteur MP3 - Julie Lenne" src="http://www.tutomobile.fr/wp-content/uploads/2010/10/Lecteur-MP3-Julie-Lenne.png" alt="" width="459" height="143" /></a></p>
<p style="text-align: justify;">Identifions les acteurs dans le MVC :</p>
<ul>
<li style="text-align: justify;">La vue c&#8217;est l&#8217;écran ci-dessus avec l&#8217;affichage du nom de l&#8217;artiste, les boutons Lecture, suivant et Précédent, le contrôle du volume</li>
<li style="text-align: justify;">Le modèle est un objet qui a la charge de lire le fichier MP3 et d&#8217;envoyer les informations au système de son</li>
<li style="text-align: justify;">Le contrôleur est un objet qui va faire le lien entre les deux</li>
</ul>
<p>Avec notre schéma précédent, cela nous donne :</p>
<ol>
<li style="text-align: justify;">L&#8217;utilisateur a très envie d&#8217;écouter de la musique, il appuie sur le bouton &laquo;&nbsp;Lecture&nbsp;&raquo;</li>
<li style="text-align: justify;">Le contrôleur reçoit l&#8217;information &laquo;&nbsp;le bouton Lecture a été cliqué&nbsp;&raquo;, il demande au modèle de commencer la lecture. La musique démarre.</li>
<li style="text-align: justify;">Le contrôleur demande à la vue de transformer le bouton &laquo;&nbsp;Lecture&nbsp;&raquo; en un bouton &laquo;&nbsp;Pause&nbsp;&raquo; et d&#8217;afficher les informations du morceau en lecture.</li>
<li style="text-align: justify;">Le morceau a été lu en entier, le modèle informe la vue qu&#8217;il a commencé la lecture du morceau suivant</li>
<li style="text-align: justify;">La vue a été informée du changement de morceau et demande au modèle les information du nouveau morceau en lecture.</li>
</ol>
<h3>Le développement iPhone adopte le modèle MVC</h3>
<p style="text-align: justify;">Nous avons déjà eu affaire avec le MVC dans les précédents tutoriels iPhone, nous ne cesserons pas de l&#8217;utiliser par la suite.</p>
<ul>
<li style="text-align: justify;">La vue est, dans l&#8217;immense majorité des cas, prise en charge par Interface Builder, qui nous permet de tout designer sans une ligne de code : ajouter des éléments, gérer leur positionnement.</li>
<li style="text-align: justify;">Nous n&#8217;avons pas encore vu de modèle à proprement parler, mais nous aurons l&#8217;occasion d&#8217;en créer, et ce dès le tutoriel n°8. Nous verrons aussi que les données peuvent être prises en charge par CoreData, moteur de données extrêmement puissant.</li>
<li style="text-align: justify;">Les contrôleurs, ça on en a déjà vu : UIViewController, UINavigationController, UITabBarController, &#8230; C&#8217;est pour l&#8217;instant là où nous avons codé le plus, puisque nous avons essentiellement des applications qui réagissent à l&#8217;utilisateur.</li>
</ul>
<h3>Deux questions qu&#8217;il vaut mieux poser tout de suite</h3>
<p style="text-align: justify;"><em>1ère question : Dans le </em><a href="http://www.tutomobile.fr/faire-une-application-calculette-tutoriel-iphone-n°4/06/08/2010/" target="_self"><em>tutoriel n°4 &#8211; l&#8217;application calculette</em></a><em>, nous n&#8217;avons pas utilisé de modèle, pourquoi ? Un modèle n&#8217;est pas utile dans ce cas ?</em></p>
<p style="text-align: justify;">C&#8217;est un choix plus éditorial que de bonne conception que de ne pas utiliser de modèle dans ce tutoriel. L&#8217;objectif était de faire le plus simple possible, quitte à créer des lourdeurs et à sacrifier complètement la réutilisabilité du code mis en place. Par exemple, il est impossible d&#8217;intégrer cette calculette dans une autre appli ou même dans une autre vue de cette application sans tout recoder.</p>
<p style="text-align: justify;"><em>2nde question : Au final le contrôleur n&#8217;est qu&#8217;un simple passe-plat, non ? Pourquoi ne pas garder qu&#8217;un modèle et une vue et se séparer des intermédiaires ?</em></p>
<p style="text-align: justify;">Le contrôleur ne fait pas que passer les plats : il est chargé d&#8217;interpréter les actions de l&#8217;utilisateur dans la vue pour manipuler le modèle en conséquence. Deux raisons expliquent qu&#8217;il n&#8217;est pas souhaitable de confier à la vue le soin de manipuler le modèle. La première est que cela imposerait à la vue d&#8217;avoir deux boulots : gérer l&#8217;interface et manipuler le modèle. La seconde est que cela imposerait un couplage fort entre la vue et le modèle, il serait alors impossible de modifier le modèle sans modifier la vue, ou l&#8217;inverse. Un couplage faible via un contrôleur vous assure une évolution plus souple des éléments par la suite. En gros : pas besoin de tout recoder parce que j&#8217;ai ajouté une fonctionnalité.</p>
<h3>Envisager le MVC dans une application avec un enchaînement d&#8217;écrans</h3>
<p style="text-align: justify;">Une application iPhone est souvent composée de différents écran enchaînés entre eux. Un exemple : je consulte une liste d&#8217;éléments, et quand je sélectionne un événement, je consulte le détail de l&#8217;élément. Dans cet exemple, le MVC joue pleinement son rôle de patron de conception :</p>
<ul>
<li style="text-align: justify;">Ma liste présente une série d&#8217;éléments. Un contrôleur récupère un tableau d&#8217;objets modèles représentant des éléments. Il trie ces objets (par nom, etc.) et les envoie à la vue qui sera en charge d&#8217;afficher.</li>
<li style="text-align: justify;">Quand l&#8217;utilisateur sélectionne un élément dans la vue, le contrôleur se charge de pousser le contrôleur de détail en lui passant l&#8217;objet modèle à détailler</li>
<li style="text-align: justify;">le contrôleur de détail instancie une vue et présente les éléments du modèle qui lui a été passé. Il peut aussi prendre en charge des modifications, il charge l&#8217;objet modèle de se transformer en conséquence (la vue propose un champ pour changer le nom de l&#8217;élément, le contrôleur récupère la nouvelle valeur de la vue et modifie le modèle en conséquence)</li>
<li style="text-align: justify;">Quand l&#8217;utilisateur appuie sur le bouton &laquo;&nbsp;Précédent&nbsp;&raquo; pour revenir à la liste, le contrôleur de liste reprend la main et rafraîchit les données. Le modèle modifié envoie son nouveau nom et la liste présentée sera à jour des modifications.</li>
</ul>
<p>Si ce n&#8217;est pas encore tout à fait clair, un exemple sera abordé dès le tutoriel suivant.</p>
<h3>Aller plus loin</h3>
<p style="text-align: justify;">Ce tutoriel n&#8217;a pas la prétention d&#8217;être exhaustif et de tout vous apprendre sur le MVC mais bien de vous initier à cette conception que vous retrouverez dans la plupart de vos développements. Si vous souhaitez aller (beaucoup) plus loin, je vous recommande les ouvrages suivants :</p>
<ul>
<li style="text-align: justify;"><a href="http://librairie.immateriel.fr/fr/ebook/9782815000031/design-patterns-tête-la-première" target="_blank">Tête la première Design Patterns</a> &#8211; Eric Freeman &amp; Elisabeth Freeman avec Kathy Sierra et Bert Bates &#8211; le livre qui vous dévoilera les secrets du maître des patterns et vous propose d&#8217;injecter directement dans votre cerveau les principaux patterns (cette collection est excellente, et ce livre en particulier est la principale source de cet article)</li>
<li><a href="http://www.amazon.fr/patterns-Catalogue-modèles-conception-réutilisables/dp/2711786447/" target="_blank">Design Patterns</a> &#8211; par &laquo;&nbsp;the Gang of Four&nbsp;&raquo;</li>
<li><a href="http://c2.com/ppr/" target="_blank">Portland Pattern Repository</a> &#8211; un wiki dédié à tout ce qui est lié aux patterns</li>
<li>Le <a href="http://hillside.net/patterns" target="_blank">Hillside Group</a> centralise les ressources sur les patterns</li>
<li><a href="http://fr.wikipedia.org/wiki/Modèle-Vue-Contrôleur" target="_blank">Wikipedia</a> est toujours une source d&#8217;information à ne pas négliger</li>
<li>Et évidemment, <a href="http://developer.apple.com/technologies/mac/cocoa.html" target="_blank">la section d&#8217;Apple à propos de Cocoa</a></li>
</ul>
<p style="text-align: justify;">Pour conclure, adopter le Model-View-Controller, apporte de nombreux avantages pour structurer votre architecture de code, et sa maîtrise vous assurera de concevoir des applications, plus réutilisables, plus faciles à faire évoluer. Un dernier avantages des design patterns est qu&#8217;ils représentent un vocabulaire commun  pour échanger avec les autres concepteurs. Les commentaires ci-dessous sont l&#8217;occasion de faire la preuve de cette affirmation <img src='http://www.tutomobile.fr/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Merci !</p>
<img src="http://feeds.feedburner.com/~r/TutoMobileTutorielsIphone/~4/SVivU3rnEoE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.tutomobile.fr/model-view-controller/17/10/2010/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.tutomobile.fr/model-view-controller/17/10/2010/</feedburner:origLink></item>
		<item>
		<title>Faire un navigateur web [Tutoriel iPhone n°7] [XCode 4]</title>
		<link>http://feedproxy.google.com/~r/TutoMobileTutorielsIphone/~3/fa1Bk4oYDnM/</link>
		<comments>http://www.tutomobile.fr/faire-un-navigateur-web-tutoriel-iphone-n%c2%b07/20/09/2010/#comments</comments>
		<pubDate>Mon, 20 Sep 2010 22:35:18 +0000</pubDate>
		<dc:creator>Axon</dc:creator>
				<category><![CDATA[Tutoriels iPhone]]></category>
		<category><![CDATA[Clavier]]></category>
		<category><![CDATA[Delegate]]></category>
		<category><![CDATA[FirstResponder]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[NSURL]]></category>
		<category><![CDATA[Text Input Traits]]></category>
		<category><![CDATA[UITextField]]></category>
		<category><![CDATA[UITextFieldDelegate]]></category>
		<category><![CDATA[UIWebView]]></category>
		<category><![CDATA[XCode 4]]></category>

		<guid isPermaLink="false">http://www.tutomobile.fr/?p=1139</guid>
		<description><![CDATA[Nouveau (avril 2011) : Ce]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;"><strong><em><span style="color: #ff6600;">Nouveau (avril 2011) : Ce tutoriel a été mis à jour et été testé pour XCode 4.</span></em></strong></p>
<p style="text-align: justify;">Le SDK de l&#8217;iPhone propose de nombreux composants permettants de gagner un temps précieux lors du développement. <strong><em>UIVebView</em></strong> est de ceux-ci puisqu&#8217;il permet d&#8217;intégrer une vue WebKit dans votre application. A titre d&#8217;exemple, ce tutoriel vous propose de construire un premier navigateur web, et vous verrez que le point plus délicat à bien comprendre n&#8217;est pas forcément là où on l&#8217;attend !</p>
<p style="text-align: justify;">Ce tutoriel sera en effet l&#8217;occasion d&#8217;en savoir plus sur le comportement de UITextField, quand il s&#8217;agira de créer la barre d&#8217;adresse et d&#8217;interagir avec le clavier virtuel de l&#8217;iPhone.</p>
<p><span id="more-1139"></span></p>
<h3>Créer le projet et y ajouter une UIWebView</h3>
<p style="text-align: justify;">Démarrez XCode et créez un nouveau projet de type &laquo;&nbsp;View based application&nbsp;&raquo;. Nous l&#8217;appellerons &laquo;&nbsp;MyWebBrowser&nbsp;&raquo;.</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/09/Xcode-nouveau-View-based-application.png" rel="lightbox[1139]"><img class="aligncenter size-full wp-image-1755" title="Xcode - nouveau View-based application" src="http://www.tutomobile.fr/wp-content/uploads/2010/09/Xcode-nouveau-View-based-application.png" alt="" width="500" height="334" /></a></p>
<p style="text-align: justify;">
<p style="text-align: justify;">Dans <strong><em>MyWebBrowserViewController.h</em></strong>, nous allons ajouter un nouvel Outlet de type UIWebView, ainsi que la propriété associée.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import</span>
<span style="color: #a61390;">@interface</span> MyWebBrowserViewController <span style="color: #002200;">:</span> UIViewController <span style="color: #002200;">&#123;</span>
	IBOutlet UIWebView <span style="color: #002200;">*</span>webView;
<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> UIWebView <span style="color: #002200;">*</span>webView;
<span style="color: #a61390;">@end</span></pre></div></div>

<p style="text-align: justify;">L&#8217;objet UIWebView, est, comme son nom l&#8217;indique bien, une vue dans laquelle va pouvoir être affiché le contenu d&#8217;une page web. Ce contenu va interagir avec l&#8217;utilisateur de la même manière qu&#8217;une page web standard : zoom, navigation, suivre les liens, remplir et envoyer les formulaires, etc. Cette instance &laquo;&nbsp;webView&nbsp;&raquo; sera donc l&#8217;objet central de notre navigateur web.</p>
<p style="text-align: justify;">N&#8217;oubliez pas de synthétiser la propriété dans <strong><em>MyWebBrowserViewController.m</em></strong> et de faire un release dans la méthode dealloc :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> MyWebBrowserViewController
<span style="color: #a61390;">@synthesize</span> webView;
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>dealloc <span style="color: #002200;">&#123;</span>
      <span style="color: #002200;">&#91;</span>webView release<span style="color: #002200;">&#93;</span>;
      <span style="color: #002200;">&#91;</span>super dealloc<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Sauvegardez les fichiers .h et .m, puis ouvrez le fichier &laquo;&nbsp;<strong><em>MyWebBrowserViewController.xib</em></strong>&laquo;&nbsp;. A l&#8217;aide de la bibliothèque d&#8217;objets de la colonne de droite, faîtes glisser un objet UIWebView sur la vue.</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/09/Infos-sur-la-webView.png" rel="lightbox[1139]"><img class="aligncenter size-full wp-image-1756" title="Infos sur la webView" src="http://www.tutomobile.fr/wp-content/uploads/2010/09/Infos-sur-la-webView.png" alt="" width="480" height="245" /></a></p>
<p style="text-align: justify;">Vous allez voir que XCode vous propose que la UIWebView prenne toute la place disponible : c&#8217;est parfait ! Inutile de le contrarier outre mesure pour le moment <img src='http://www.tutomobile.fr/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> . Associer l&#8217;outlet &laquo;&nbsp;webView&nbsp;&raquo; de l&#8217;objet File&#8217;s Owner (clic droit sur File&#8217;sOwner et tracez un trait depuis l&#8217;élément webView de la fenêtre noire vers la vue nouvellement créée).</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/09/Connecter-la-WebView.png" rel="lightbox[1139]"><img class="aligncenter size-full wp-image-1757" title="Connecter la WebView" src="http://www.tutomobile.fr/wp-content/uploads/2010/09/Connecter-la-WebView.png" alt="" width="400" height="190" /></a></p>
<p style="text-align: justify;">Sauvegardez ; il ne reste plus qu&#8217;à compiler et à lancer l&#8217;application.</p>
<h3>Premier problème : il n&#8217;y a rien dans le navigateur&#8230;</h3>
<p style="text-align: justify;">Oui, mais en même temps, nous n&#8217;avons pas précisé d&#8217;adresse en particulier ! C&#8217;est l&#8217;occasion de parler rapidement du fonctionnement de UIWebView. Les données dans UIWebView sont chargées à partir d&#8217;une requête Internet : une <strong><em>NSURLRequest</em></strong>. Cette requête est un objet créé à partir d&#8217;une adresse URL : un objet <strong><em>NSURL</em></strong>. On peut expliquer cela comme on veut, le code est encore ce qu&#8217;il y a de plus clair à comprendre. Repérez la méthode viewDidLoad qui doit être en commentaire dans le fichier MyWebBrowserViewController.m, et transformez la ainsi :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidLoad <span style="color: #002200;">&#123;</span>
	<span style="color: #002200;">&#91;</span>super viewDidLoad<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Créer une URL</span>
	<span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>urlAddress <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://www.google.com&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Faire une requête sur cette URL</span>
	<span style="color: #400080;">NSURLRequest</span> <span style="color: #002200;">*</span>requestObject <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLRequest</span> requestWithURL<span style="color: #002200;">:</span>urlAddress<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Charger la requête dans la UIWebView</span>
	<span style="color: #002200;">&#91;</span>webView loadRequest<span style="color: #002200;">:</span>requestObject<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Sauvegardez et compilez. C&#8217;est mieux ! Vous voici sur le moteur de recherche. Touchez le champ Google et saisissez l&#8217;intitulé de votre recherche, touchez les liens, tout fonctionne !</p>
<h3>Second problème : l&#8217;affichage des sites n&#8217;est pas adapté à la taille du téléphone</h3>
<p style="text-align: justify;">Google affiche correctement le moteur de recherche ainsi que les résultats, mais dès qu&#8217;on essaie d&#8217;aller un peu plus loin, ça se corse : les sites n&#8217;apparaissent pas adaptés à la taille du téléphone, à l&#8217;inverse de Safari mobile où tout est zoomé pile poil comme il faut.</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/09/Comparatif.png" rel="lightbox[1139]"><img class="aligncenter size-full wp-image-1156" title="Comparatif" src="http://www.tutomobile.fr/wp-content/uploads/2010/09/Comparatif.png" alt="" width="500" height="445" /></a></p>
<p style="text-align: justify;">Il s&#8217;agit d&#8217;une simple propriété à affecter à notre UIWebView. Ouvrez à nouveau <strong><em>&laquo;&nbsp;MyWebBrowserViewController.xib&nbsp;&raquo;</em></strong>, sélectionnez la UIWebView et dans l&#8217;onglet &laquo;&nbsp;Attributes Inspector&nbsp;&raquo; de la colonne de droite, cochez la case &laquo;&nbsp;Scale Page To Fit&nbsp;&raquo;.</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/09/Inspecteur-scale-page-to-fit.png" rel="lightbox[1139]"><img class="aligncenter size-full wp-image-1758" title="Inspecteur - scale page to fit" src="http://www.tutomobile.fr/wp-content/uploads/2010/09/Inspecteur-scale-page-to-fit.png" alt="" width="313" height="211" /></a></p>
<p style="text-align: justify;">Sauvegardez, et recompilez. Cette fois, c&#8217;est bon.</p>
<h3>Un navigateur a besoin d&#8217;une barre d&#8217;adresse !</h3>
<p style="text-align: justify;">Et oui ! S&#8217;il est vrai que dans cet état notre navigateur fonctionne, ce serait quand même plus pratique avec une barre d&#8217;adresse. Réouvrez le fichier MyWebBrowserViewController.h et ajoutez un nouvel outlet de type <strong><em>UITextField</em></strong> que nous appellerons <strong><em>adressField</em></strong>. Ajoutez la propriété correspondante, synthétisez la dans MyWebBrowserViewController.m et faites un release dans la méthode dealloc. C&#8217;est le même fonctionnement que précédemment !</p>
<p style="text-align: justify;">Retournons modifier MyWebBrowserViewController.xib : réduisez la taille de la UIWebView, et ajoutez un objet UITextField qui prendra toute la largeur.</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/09/Ajouter-un-UITextField1.png" rel="lightbox[1139]"><img class="aligncenter size-full wp-image-1759" title="Ajouter un UITextField" src="http://www.tutomobile.fr/wp-content/uploads/2010/09/Ajouter-un-UITextField1.png" alt="" width="338" height="213" /></a></p>
<p style="text-align: justify;">Connectez ce UITextField à l&#8217;outlet que nous venons de créer (exactement de la même façon que la UIWebView) et sauvegardez.</p>
<h3>Comment configurer le clavier virtuel ?</h3>
<p style="text-align: justify;">Si vous compilez et lancez votre application à ce moment du projet, le toucher du champ texte va faire apparaître le clavier virtuel. Mais comment faire pour configurer ce clavier ? En effet, dans Safari Mobile, quand il s&#8217;agit de saisir l&#8217;URL, les caractères &laquo;&nbsp;.&nbsp;&raquo;, &laquo;&nbsp;/&nbsp;&raquo; ou encore l&#8217;extension sont en accès rapide. De plus, le bouton en bas à droite propose généralement un bouton &laquo;&nbsp;Aller&nbsp;&raquo;.</p>
<p><a href="http://www.tutomobile.fr/wp-content/uploads/2010/09/Claviers.png" rel="lightbox[1139]"><img class="aligncenter size-full wp-image-1162" title="Claviers" src="http://www.tutomobile.fr/wp-content/uploads/2010/09/Claviers.png" alt="" width="400" height="185" /></a></p>
<p style="text-align: justify;">S&#8217;il est une chose qu&#8217;il est important de bien comprendre à ce stade, c&#8217;est que vous n&#8217;avez pas la main directement pour configurer le clavier virtuel : vous ne pouvez pas l&#8217;invoquer directement ou de lui envoyer des messages. C&#8217;est au moment de l&#8217;exécution que iOS &laquo;&nbsp;attache&nbsp;&raquo; le clavier virtuel au composant qui l&#8217;a appelé. C&#8217;est donc ce composant qui va savoir quel type de clavier virtuel il souhaite se voir proposé. Cette configuration est appelée : <strong><em>Text Input Traits</em></strong>.</p>
<p style="text-align: justify;">Dans l&#8217;interface de modification de MyWebBrowserViewController.xib, sélectionnez le UITextField et affichez l&#8217;inspecteur dans la colonne de droite. Dans l&#8217; &laquo;&nbsp;Attributes Inspector&nbsp;&raquo;, vous remarquerez une section relative à ces &laquo;&nbsp;Text Input Traits&nbsp;&raquo;.</p>
<p><a href="http://www.tutomobile.fr/wp-content/uploads/2010/09/Text-Input-Traits.png" rel="lightbox[1139]"><img class="aligncenter size-full wp-image-1760" title="Text Input Traits" src="http://www.tutomobile.fr/wp-content/uploads/2010/09/Text-Input-Traits.png" alt="" width="311" height="158" /></a></p>
<p>De nombreuses options sont configurables :</p>
<ul>
<li>le type de clavier (par défaut, URL, numéro de téléphone, email&#8230;)</li>
<li>si c&#8217;est un clavier de mot de passe ou non (l&#8217;option Secure)</li>
<li>la mise en place de l&#8217;auto-correction (vous pouvez la désactiver, mais n&#8217;oubliez pas que l&#8217;utilisateur choisit, dans les préférences, son mode de fonctionnement : il n&#8217;est pas forcément du meilleur goût que de passer outre ce choix)</li>
<li>le bouton de validation que vous souhaitez voir apparaître</li>
<li>des options d&#8217;apparence</li>
<li>etc.</li>
</ul>
<p>Choisissez un clavier URL et le bouton &laquo;&nbsp;Go&nbsp;&raquo; pour la validation.</p>
<h3>Récupérer le Go lancé par le clavier</h3>
<p style="text-align: justify;">Si vous compilez et lancez votre application maintenant, il va se passer quelque chose d&#8217;assez désagréable. Vous touchez le champ d&#8217;adresse, tapez <a href="http://www.tutomobile.fr">http://www.tutomobile.fr</a> et maintenant vous pourrez écraser vigoureusement le bouton Go, il ne faut pas rêver : il ne va rien se passer du tout. Toucher ce bouton doit faire deux choses :</p>
<ul>
<li>mettre à jour la UIWebView avec la nouvelle adresse</li>
<li>masquer ce clavier qui n&#8217;est plus nécessaire jusqu&#8217;à ce qu&#8217;on retouche le champ d&#8217;adresse</li>
</ul>
<p style="text-align: justify;">Le premier réflexe serait de créer une (IBAction) dans notre MyWebBrowserViewController et de relier un événement à cette action. Une manière de faire qui pourrait ressembler à celle que nous avons employée pour les UIButton dans le <a href="http://www.tutomobile.fr/application-hello-world-tutoriel-iphone-n°3/03/08/2010/">Tutoriel n°3</a>, par exemple. Mais là : problème ! Il n&#8217;y a pas vraiment l&#8217;événement qui correspond à ce Go.</p>
<p style="text-align: justify;">Il y a une autre voie. UITextField peut (et souvent doit) être accompagné d&#8217;un <strong><em>Delegate</em></strong> (mais si, rappelez-vous le <a href="http://www.tutomobile.fr/bien-commencer-avec-uitabbarcontroller-tutoriel-iphone-n°6/09/09/2010/">Tutoriel n°6</a> où nous parlions des AppDelegate) ; c&#8217;est à dire un &laquo;&nbsp;représentant&nbsp;&raquo; : un objet qui vient en soutien. Nous allons dire à notre UITextField que notre classe MyWebBrowserViewController est son représentant.</p>
<p style="text-align: justify;">Ouvrez le fichier MyWebBrowserViewController.h et modifiez la déclaration de la classe qui devient :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyWebBrowserViewController <span style="color: #002200;">:</span> UIViewController <span style="color: #002200;">&amp;</span>lt;UITextFieldDelegate<span style="color: #002200;">&amp;</span>gt; <span style="color: #002200;">&#123;</span></pre></div></div>

<p style="text-align: justify;">Ceci signifie que MyWebBrowserViewController implémente désormais le protocole <strong><em>UITextFieldDelegate</em></strong>. En d&#8217;autre termes, votre classe lance un cri du coeur : &laquo;&nbsp;oui ! UITextField, je suis digne d&#8217;être ton delegate !&nbsp;&raquo;. Pour savoir ce qu&#8217;il est nécessaire ou possible d&#8217;implémenter comme méthode dans le cadre de ce protocole, appuyez sur la touche &laquo;&nbsp;Commande&nbsp;&raquo; du clavier (euh&#8230; pomme, quoi) et double-cliquez sur &lt;UITextFieldDelegate&gt; pour consulter la déclaration. Toutes les méthodes sont optionnelles, celle qui nous intéresse s&#8217;appelle <strong><em>textFieldShouldReturn</em>.</strong></p>
<p style="text-align: justify;">Retournez dans le code de MyWebBrowserViewController.m et implémentez comme suit :</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span> textFieldShouldReturn<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITextField <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>textField <span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>urlAddress <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span> textField.text<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Ouvrir l'URL</span>
	<span style="color: #400080;">NSURLRequest</span> <span style="color: #002200;">*</span>requestObject <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLRequest</span> requestWithURL<span style="color: #002200;">:</span>urlAddress<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">//Load the request in the UIWebView.</span>
	<span style="color: #002200;">&#91;</span>webView loadRequest<span style="color: #002200;">:</span>requestObject<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Réduire le clavier</span>
	<span style="color: #002200;">&#91;</span>textField resignFirstResponder<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Pour afficher l&#8217;adresse URL saisie dans la UIWebView, vous remarquerez que le code est sensiblement le même que précédemment. Pour réduire le clavier, c&#8217;est extrêmement simple : lorsque vous touchez le UITextField, le clavier apparaît et s&#8217;inscrit comme étant le &laquo;&nbsp;First Responder&nbsp;&raquo; de l&#8217;objet. Si vous lancez le message <strong><em>resignFirstResponder</em></strong>, vous révoquez le clavier : il disparaît.</p>
<p style="text-align: justify;">Il reste une dernière toute petite chose que nous n&#8217;avons pas faîte, assigner au UITextField son nouveau delegate. Retournez modifier MyWebBrowserViewController.xib, cliquez droit sur le UITextField, et faîtes glisser depuis &laquo;&nbsp;delegate&nbsp;&raquo; vers l&#8217;objet &laquo;&nbsp;File&#8217;s Owner&nbsp;&raquo;. Sauvegardez, compilez et testez.</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/09/Assigner-le-delegate.png" rel="lightbox[1139]"><img class="aligncenter size-full wp-image-1761" title="Assigner le delegate" src="http://www.tutomobile.fr/wp-content/uploads/2010/09/Assigner-le-delegate.png" alt="" width="500" height="272" /></a></p>
<p style="text-align: justify;">Attention, dans cette version, il est nécessaire de préciser &laquo;&nbsp;http://&nbsp;&raquo; devant les URLs que vous tapez pour que cela fonctionne. Voilà un petit exercice rapide à réaliser : ajouter automatiquement &laquo;&nbsp;http://&nbsp;&raquo; si ce n&#8217;est pas précisé par l&#8217;utilisateur !</p>
<p style="text-align: justify;">
<p style="text-align: justify;">C&#8217;est ainsi que s&#8217;achève ce tutoriel iPhone n°7. Vous l&#8217;aviez compris, le navigateur web était un prétexte pour vous faire aussi découvrir quelques subtilités des UITextFields, que vous retrouverez dans la plupart de vos applications. Cependant, s&#8217;il vous reste quelques minutes, je vous conseille d&#8217;aller jeter un oeil sur le <a href="http://www.tutomobile.fr/faire-un-navigateur-web-tutoriel-android-n°9/11/07/2010/">tutoriel Android n°9</a> pour voir les différences qui peuvent séparer les deux plateformes sur des objets comme une vue Web.</p>
<img src="http://feeds.feedburner.com/~r/TutoMobileTutorielsIphone/~4/fa1Bk4oYDnM" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.tutomobile.fr/faire-un-navigateur-web-tutoriel-iphone-n%c2%b07/20/09/2010/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		<feedburner:origLink>http://www.tutomobile.fr/faire-un-navigateur-web-tutoriel-iphone-n%c2%b07/20/09/2010/</feedburner:origLink></item>
		<item>
		<title>Les onglets avec UITabBarController [Tutoriel iPhone n°6] [XCode 4]</title>
		<link>http://feedproxy.google.com/~r/TutoMobileTutorielsIphone/~3/Gz6NNUPdHYI/</link>
		<comments>http://www.tutomobile.fr/bien-commencer-avec-uitabbarcontroller-tutoriel-iphone-n%c2%b06/09/09/2010/#comments</comments>
		<pubDate>Thu, 09 Sep 2010 22:43:39 +0000</pubDate>
		<dc:creator>Axon</dc:creator>
				<category><![CDATA[Tutoriels iPhone]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[UITabBarController]]></category>
		<category><![CDATA[UIViewController]]></category>
		<category><![CDATA[XCode 4]]></category>

		<guid isPermaLink="false">http://www.tutomobile.fr/?p=1110</guid>
		<description><![CDATA[Nouveau (avril 2011) : Ce]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;"><strong><em><span style="color: #ff6600;">Nouveau (avril 2011) : Ce tutoriel a été mis à jour et été testé pour XCode 4.</span></em></strong></p>
<p style="text-align: justify;">Si <strong><em><a href="http://www.tutomobile.fr/gerer-la-navigation-entre-plusieurs-vues-uinavigationcontroller-tutoriel-iphone-n°5/14/08/2010/" target="_self">UINavigationController</a></em></strong> permet d&#8217;organiser les vues de façon hiérarchique et de permettre à l&#8217;utilisateur d&#8217;obtenir des détails sur un élément de son choix, il est souvent nécessaire de proposer plusieurs fonctions ou modes de recherche de manière parallèle. Citons par exemple l&#8217;application iPod qui permet de rechercher un morceau de musique par artiste, par album, par genre, etc. Cette navigation par onglets positionnés en bas de l&#8217;écran est gérée par une classe CocoaTouch : <em><strong>UITabBarController</strong></em>. Dans ce tutoriel, vous verrez comment articuler plusieurs vues à l&#8217;aide de UITabBarController et comment vous pourrez personnaliser l&#8217;apparence de ce dernier.</p>
<h3><span id="more-1110"></span>Créer deux UIViews à afficher</h3>
<p style="text-align: justify;">Pour commencer, ouvrez XCode et créez un projet de type &laquo;&nbsp;Window Based Application&nbsp;&raquo;.</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/09/Xcode-Window-based-Application.png" rel="lightbox[1110]"><img class="aligncenter size-full wp-image-1741" title="Xcode - Window-based Application" src="http://www.tutomobile.fr/wp-content/uploads/2010/09/Xcode-Window-based-Application.png" alt="" width="145" height="110" /></a></p>
<p style="text-align: justify;">Nous allons maintenant créer deux vues que nous enregistrerons par la suite dans le UITabBarController. Pour créer la première vue, choisissez &laquo;&nbsp;New File&#8230;&nbsp;&raquo; dans le menu &laquo;&nbsp;File&nbsp;&raquo; de XCode, puis choisissez le type <em><strong>UIViewController Subclass</strong></em>. N&#8217;oubliez pas de cocher la case &laquo;&nbsp;With XIB for user interface&nbsp;&raquo; pour que le fichier Interface Builder soit automatiquement créé. Appelez cette classe <em>TabLabelViewController</em>.</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/09/Xcode-New-UIViewController-subclass.png" rel="lightbox[1110]"><img class="aligncenter size-full wp-image-1742" title="Xcode - New UIViewController subclass" src="http://www.tutomobile.fr/wp-content/uploads/2010/09/Xcode-New-UIViewController-subclass.png" alt="" width="500" height="323" /></a></p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/09/Xcode-New-UIViewController-subclass.png" rel="lightbox[1110]"></a><a href="http://www.tutomobile.fr/wp-content/uploads/2010/09/Xcode-with-XIB.png" rel="lightbox[1110]"><img class="aligncenter size-full wp-image-1743" title="Xcode - with XIB" src="http://www.tutomobile.fr/wp-content/uploads/2010/09/Xcode-with-XIB.png" alt="" width="500" height="96" /></a></p>
<p style="text-align: justify;">Ouvrez le fichier TabLabelViewController.xib et ajoutez un UILabel au centre présentant le texte de votre choix. Sauvegarder avant de continuer.</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/09/Insérer-un-label.png" rel="lightbox[1110]"><img class="aligncenter size-full wp-image-1744" title="Insérer un label" src="http://www.tutomobile.fr/wp-content/uploads/2010/09/Insérer-un-label.png" alt="" width="422" height="272" /></a></p>
<p style="text-align: justify;">Pour la seconde vue, je vous propose quelque chose d&#8217;un peu plus sympa qu&#8217;un bête texte : nous allons afficher un logo de votre choix. Commençons d&#8217;abord par ajouter l&#8217;image dans le projet. Pour ce faire, localisez le fichier sur votre disque dur et faîtes le glisser dans la colonne de gauche de XCode, dans le répertoire de votre projet. N&#8217;oubliez pas de cocher la case &laquo;&nbsp;Copy items into destination group&#8217;s folder (if needed)&nbsp;&raquo; dans la fenêtre qui s&#8217;ouvre, pour que l&#8217;image soit dupliquée dans votre projet (et éviter de perdre vos petits par la suite).</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/09/Ajouter-une-image-au-projet.png" rel="lightbox[1110]"><img class="aligncenter size-full wp-image-1745" title="Ajouter une image au projet" src="http://www.tutomobile.fr/wp-content/uploads/2010/09/Ajouter-une-image-au-projet.png" alt="" width="400" height="322" /></a></p>
<p style="text-align: justify;">Créons maintenant une nouvelle classe que nous appellerons <em>TabImageViewController</em>. A nouveau ouvrez le fichier .xib, et faîtes-y glisser, depuis la bibliothèque, un élément <strong><em>Image View</em></strong>, il doit occuper toute la place disponible dans la vue principale.</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/09/Xcode-Ajouter-une-image.png" rel="lightbox[1110]"><img class="aligncenter size-full wp-image-1746" title="Xcode - Ajouter une image" src="http://www.tutomobile.fr/wp-content/uploads/2010/09/Xcode-Ajouter-une-image.png" alt="" width="298" height="281" /></a></p>
<p style="text-align: justify;">Dans la partie supérieure de la zone de droite, choisissez, si ce n&#8217;est pas le cas l&#8217;onglet &laquo;&nbsp;Attributes inspector&nbsp;&raquo; (matérialisé par un petit curseur). Dans le champ &laquo;&nbsp;Image&nbsp;&raquo;, choisissez l&#8217;image que nous avons ajoutée précédemment. Avant de sauvegarder, modifier le mode de positionnement pour trouver celui qui correspondra le mieux à votre image.</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/09/Xcode-propriétés-de-limage.png" rel="lightbox[1110]"><img class="aligncenter size-full wp-image-1747" title="Xcode - propriétés de l'image" src="http://www.tutomobile.fr/wp-content/uploads/2010/09/Xcode-propriétés-de-limage.png" alt="" width="275" height="235" /></a></p>
<h3>Créer l&#8217;objet UITabBarController et insérer les deux vues</h3>
<p style="text-align: justify;">Nous avons maintenant deux objets ViewController. Il s&#8217;agit de les associer à un UITabBarController et ainsi naviguer entre les deux. Pour ce faire, ouvrez le fichier appelé <strong><em>xxxAppDelegate.h</em></strong> (où &laquo;&nbsp;xxx&nbsp;&raquo; est à remplacer par le nom de votre projet). Ajoutez-y la déclaration d&#8217;un nouvel objet UITabBarController.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> TabBarTutorialAppDelegate <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&amp;</span>lt;UIApplicationDelegate<span style="color: #002200;">&amp;</span>gt; <span style="color: #002200;">&#123;</span>
    UITabBarController <span style="color: #002200;">*</span>tabBarController;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Ouvrez le fichier<em><strong> xxxAppDelegate.m</strong></em> pour modifier l&#8217;implémentation. Dans d&#8217;en-tête du fichier, importez les deux vues que nous venons de créer.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;TabLabelViewController.h&quot;</span>
<span style="color: #6e371a;">#import &quot;TabImageViewController.h&quot;</span></pre></div></div>

<p style="text-align: justify;">Repérez à présent la méthode <strong><em>- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions</em></strong>. Cette méthode est appelée (comme son nom l&#8217;indique) dès que l&#8217;application a fini de se charger. Pour être très complet, la classe xxxAppDelegate est une classe qui vient en soutient de chaque application. Vous avez la possibilité d&#8217;y implémenter des méthodes qui seront utiles tout au long de la vie de l&#8217;application. Une traduction possible de &laquo;&nbsp;Delegate&nbsp;&raquo; est &laquo;&nbsp;Représentant&nbsp;&raquo; ce qui correspond bien à l&#8217;usage que nous avons des AppDelegate : ce sont les représentants de l&#8217;application ; nous leur apprenons à réagir convenablement quand il arrive quelque chose à l&#8217;application (démarrage, arrêt, mise en veille ou même problème de mémoire).</p>
<p style="text-align: justify;">Mais je m&#8217;égare et philosophe sur les AppDelegate. Repérez donc cette méthode &laquo;&nbsp;didFinishLaunching&nbsp;&raquo; qui, pour le moment se charge d&#8217;afficher une fenêtre. Nous devons faire trois choses dans cette méthode :</p>
<ol>
<li>Instancier le UITabBarController</li>
<li>Créer un TabLabelViewController et l&#8217;enregistrer dans le UITabBarController</li>
<li>Créer un TabImageViewController et l&#8217;enregistrer</li>
</ol>
<p style="text-align: justify;">Pour enregistrer les vues dans le UITabBarController, nous utiliserons sa propriété <strong><em>viewControllers</em></strong> qui est en fait un tableau de vues.</p>
<p style="text-align: justify;">Voici donc le code à implémenter.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>application<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application didFinishLaunchingWithOptions<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>launchOptions <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Override point for customization after application launch.</span>
	<span style="color: #11740a; font-style: italic;">// Créer un tabBarViewController</span>
	tabBarController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITabBarController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Créer une vue 1 et affecter</span>
	TabLabelViewController <span style="color: #002200;">*</span>v1 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>TabLabelViewController alloc<span style="color: #002200;">&#93;</span> initWithNibName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;TabLabelViewController&quot;</span> bundle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Créer une vue image et affecter</span>
	TabImageViewController <span style="color: #002200;">*</span>v2 <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>TabImageViewController alloc<span style="color: #002200;">&#93;</span> initWithNibName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;TabImageViewController&quot;</span> bundle<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	tabBarController.viewControllers <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span>v1, v2, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>v1 release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>v2 release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #002200;">&#91;</span>self.window addSubview<span style="color: #002200;">:</span>tabBarController.view<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>self.window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Notez qu&#8217;une fois les deux UIViewController créés et ajoutés dans le NSArray, nous leur passons un signal release : ils sont en effet retenus par le UITabBarController.</p>
<p style="text-align: justify;">Vous pouvez compiler et faire un premier essai de votre application. Vous obtiendrez bien une barre d&#8217;onglets en bas de l&#8217;écran permettant de naviguer entre les deux vues.</p>
<h3>Personnaliser l&#8217;apparence du UITabBarController</h3>
<p style="text-align: justify;">Il y a quand même un petit problème : les onglets sont vides ! Il devraient être associés à une image et à un intitulé, au minimum. Avant de commencer, il est nécessaire de bien comprendre le fonctionnement de UITabBarController : pour savoir ce qu&#8217;il doit afficher dans l&#8217;onglet correspondant à un ViewController, il va le lui demander ! C&#8217;est le ViewController qui définit son apparence dans la barre d&#8217;onglets. Ceci se fait à l&#8217;aide d&#8217;un objet <strong><em>UITabBarItem</em><span style="font-weight: normal;"> qui est enregistré dans la propriété </span><em>tabBarItem</em></strong> du UIViewController.</p>
<p style="text-align: justify;">Le UITabBarItem permet de préciser :</p>
<ul>
<li>le titre qui s&#8217;affiche en bas</li>
<li>une image. Cette image sera au format PNG en noir et transparent. Les pixels noirs seront automatiquement colorés par iOS avec cet effet bleuté pour indiquer que l&#8217;onglet est actif. Taille 30px par 30px</li>
<li>un tag : un nombre qui pourra être affichés sous forme d&#8217;une petite pastille rouge.</li>
</ul>
<p style="text-align: justify;">Pour le TabLabelViewController, nous allons créer une image PNG de notre cru (ici un petit crayon) et choisir le titre &laquo;&nbsp;Label&nbsp;&raquo;.</p>
<p style="text-align: justify;"><a href="http://www.tutomobile.fr/wp-content/uploads/2010/09/crayonnoir.png" rel="lightbox[1110]"><img class="aligncenter size-full wp-image-1749" title="crayonnoir" src="http://www.tutomobile.fr/wp-content/uploads/2010/09/crayonnoir.png" alt="" width="30" height="30" /></a></p>
<p style="text-align: justify;">Pour ce faire, créez l&#8217;image avec votre logiciel préféré (on a qu&#8217;à dire GIMP) ou téléchargez-la en faisant un clic-droit sur l&#8217;image ci-dessus, et ajoutez-la au projet en le faisant glisser vers la colonne de gauche de XCode. Ouvrez le fichier TabLabelViewController.m, repérez la méthode <strong><em>initWithNibName</em><span style="font-weight: normal;">, et complétez-la avec le code suivant :</span></strong></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithNibName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>nibNameOrNil bundle<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSBundle</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>nibBundleOrNil
<span style="color: #002200;">&#123;</span>
    self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super initWithNibName<span style="color: #002200;">:</span>nibNameOrNil bundle<span style="color: #002200;">:</span>nibBundleOrNil<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        self.tabBarItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITabBarItem alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Label&quot;</span> image<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;crayonnoir.png&quot;</span><span style="color: #002200;">&#93;</span> tag<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><a href="http://www.tutomobile.fr/wp-content/uploads/2010/09/Capture-d’écran-2010-09-10-à-00.13.22.png" rel="lightbox[1110]"><img class="aligncenter size-full wp-image-1123" title="TabBar personnalisé" src="http://www.tutomobile.fr/wp-content/uploads/2010/09/Capture-d’écran-2010-09-10-à-00.13.22.png" alt="" width="389" height="380" /></a></p>
<p style="text-align: justify;"><strong><span style="font-weight: normal;">Pour le TabImageViewController, encore plus rapide, nous allons utiliser un élément standard du système. Il en existe quelques un : une étoile pour les favoris, un livre ouvert pour les favoris, des utilisateurs, un loupe pour la recherche&#8230; Nous allons utiliser </span><em>UITabBarSystemItemFeatured</em></strong> qui est représenté par deux spots vers le haut. Cette fois, même plus besoin de choisir de titre : tout est fait automatiquement (et traduit).</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithNibName<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>nibNameOrNil bundle<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSBundle</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>nibBundleOrNil
<span style="color: #002200;">&#123;</span>
    self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super initWithNibName<span style="color: #002200;">:</span>nibNameOrNil bundle<span style="color: #002200;">:</span>nibBundleOrNil<span style="color: #002200;">&#93;</span>;
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
        self.tabBarItem <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITabBarItem alloc<span style="color: #002200;">&#93;</span> initWithTabBarSystemItem<span style="color: #002200;">:</span>UITabBarSystemItemFeatured tag<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#125;</span>
    <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><a href="http://www.tutomobile.fr/wp-content/uploads/2010/09/Capture-d’écran-2010-09-10-à-00.21.17.png" rel="lightbox[1110]"><img class="aligncenter size-full wp-image-1124" title="TabBarSystem" src="http://www.tutomobile.fr/wp-content/uploads/2010/09/Capture-d’écran-2010-09-10-à-00.21.17.png" alt="" width="370" height="370" /></a></p>
<p style="text-align: justify;">Attention cependant, l&#8217;usage des icônes UITabBarSystemItem est très encadré par Apple pour maintenir une cohérence de la signification de ces onglets dans tout le système. Référez vous au guide<em> &laquo;&nbsp;Human Interface guidelines&nbsp;&raquo;</em> pour l&#8217;utilisation de ces icônes.</p>
<p style="text-align: justify;">Et voilà un bon aperçu de tout ce que vous pouvez faire avec <strong><em>UITabBarController</em></strong>. Une dernière chose quand même : vous pouvez ajouter autant de ViewControllers dans votre TabBar : s&#8217;il y en a trop pour tous les afficher en une seule fois, iOS proposera de lui-même un bouton &laquo;&nbsp;More&nbsp;&raquo; et permettra à l&#8217;utilisateur de paramétrer son affichage et choisir l&#8217;ordre des onglets. Vous trouverez ce fonctionnement dans l&#8217;application iPod, par exemple.</p>
<p style="text-align: justify;">Ce tutoriel touche à sa fin, merci d&#8217;être arrivé jusqu&#8217;au bout <img src='http://www.tutomobile.fr/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  N&#8217;hésitez pas à réagir dans les commentaires, ni à poser vos questions : nous essaierons de vous répondre et de vous aider dans votre dev. pour iPhone !</p>
<img src="http://feeds.feedburner.com/~r/TutoMobileTutorielsIphone/~4/Gz6NNUPdHYI" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.tutomobile.fr/bien-commencer-avec-uitabbarcontroller-tutoriel-iphone-n%c2%b06/09/09/2010/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		<feedburner:origLink>http://www.tutomobile.fr/bien-commencer-avec-uitabbarcontroller-tutoriel-iphone-n%c2%b06/09/09/2010/</feedburner:origLink></item>
	<copyright>Tuto Mobile</copyright><media:credit role="author">Axon</media:credit><media:rating>nonadult</media:rating></channel>
</rss>

