<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Learning Iphone</title>
	<atom:link href="http://www.learningiphone.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learningiphone.com</link>
	<description>Iphone Development musings focusing on: Visual Programming / User Interaction / Game Development</description>
	<lastBuildDate>Sat, 19 Mar 2011 05:33:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Combining Cocos2D with UIKit and UIToolbar</title>
		<link>http://www.learningiphone.com/2011/03/combining-cocos2d-with-uikit-and-uitoolbar/</link>
		<comments>http://www.learningiphone.com/2011/03/combining-cocos2d-with-uikit-and-uitoolbar/#comments</comments>
		<pubDate>Sat, 19 Mar 2011 04:05:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[cocos2d]]></category>
		<category><![CDATA[uikit]]></category>

		<guid isPermaLink="false">http://www.learningiphone.com/?p=350</guid>
		<description><![CDATA[Cocos2D is an awesome 2D iphone game engine, it&#8217;s just really really good and the code inside is nicely organized. However, to a beginner using Cocos2D it would seem that it does not work with UIKit based applications, this impression is largely Cocos2D&#8217;s fault. I recently started a new game, and my plan is to [...]]]></description>
			<content:encoded><![CDATA[<p>Cocos2D is an awesome 2D iphone game engine, it&#8217;s just really really good and the code inside is nicely organized.<br />
However, to a beginner using Cocos2D it would seem that it does not work with UIKit based applications, this impression is largely Cocos2D&#8217;s fault.</p>
<p>I recently started a new game, and my plan is to use UIKit for some of the level-creation and imo there&#8217;s no need to use Cocos2D to simulate UIKit when UIKit is already an amazing UIKit simulator and readily available.</p>
<p>Last time I posted a similar idea, a lot of people found it useful, I started doing a write-up but to be honest it involves many steps that need explaining, and i&#8217;m not great at it.</p>
<p>So this time i&#8217;m instead providing it as a xcode project which you can open and compile and just start using!</p>
<h1>Download / Repository</h1>
<p><a href="https://github.com/onedayitwillmake/Cocos2D-UIToolbar">https://github.com/onedayitwillmake/Cocos2D-UIToolbar</a></p>
<h1>Preview</h1>
<p><a href="http://www.learningiphone.com/wp-content/uploads/2011/03/Screen-shot-2011-03-19-at-12.25.42-AM.png"><img title="Screen shot 2011-03-19 at 12.25.42 AM" src="http://www.learningiphone.com/wp-content/uploads/2011/03/Screen-shot-2011-03-19-at-12.25.42-AM.png" alt="" width="580" height="298" /></a></p>
<h1><a href="http://www.learningiphone.com/wp-content/uploads/2011/03/Screen-shot-2011-03-19-at-1.30.33-AM.png"><img class="alignnone size-full wp-image-359" title="Screen shot 2011-03-19 at 1.30.33 AM" src="http://www.learningiphone.com/wp-content/uploads/2011/03/Screen-shot-2011-03-19-at-1.30.33-AM.png" alt="" width="580" height="298" /></a></h1>
<h1>Slight detail</h1>
<ul>
<li>Cocos2D lives in side a EAGLView, and EAGLView is just a view that you can insert into other views!</li>
<li>Especially in recent versions of iOS it plays very very nicely with other UIView&#8217;s occupying the screen at the same time</li>
<li>I started off with Cocos2D Box2D template, put the default logic in RootViewController inside of a new UIViewController and made sure Cocos2D was not attaching itself to the window.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.learningiphone.com/2011/03/combining-cocos2d-with-uikit-and-uitoolbar/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Cocos2D Forced Kerning</title>
		<link>http://www.learningiphone.com/2011/01/cocos2d-forced-kerning/</link>
		<comments>http://www.learningiphone.com/2011/01/cocos2d-forced-kerning/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 00:28:09 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[categories]]></category>
		<category><![CDATA[cocos2d]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[obj-c-runtime]]></category>

		<guid isPermaLink="false">http://www.learningiphone.com/?p=338</guid>
		<description><![CDATA[In Cocos2D you display text, any number of ways. A popular method for displaying text is to use BitmapFont, that is &#8211; a font created in a program which outputs two files. A bitmap (say a png for example), and a plain text &#8216;.fnt&#8217; file, containing information regarding where each character is in that file. [...]]]></description>
			<content:encoded><![CDATA[<p>In Cocos2D you display text, any number of ways. A popular method for displaying text is to use BitmapFont, that is &#8211; a font created in a program which outputs two files. A bitmap (say a png for example), and a plain text &#8216;.fnt&#8217; file, containing information regarding where each character is in that file.</p>
<p>There are many programs that do this well, I use Heiro which is a free java based bitmap font creator.<br />
A benefit of these programs as well, is that can &#8216;bake&#8217; in certain effects such as an inset/glow/drop shadow &#8211; on to the text so it doesn&#8217;t have to be calculated every frame.</p>
<p>The problem with that however, is that it requires a lot of &#8216;padding&#8217; on the font so the glow from the letter &#8216;A&#8217; doesn&#8217;t spill to the letter &#8216;B&#8217;.</p>
<h2>So you end up with something that looks like this</h2>
<p><a href="http://www.learningiphone.com/wp-content/uploads/2011/01/GothamBoldOrangeWithGlowAt20pts1.png"><img class="alignnone size-full wp-image-342" title="GothamBoldOrangeWithGlowAt20pts" src="http://www.learningiphone.com/wp-content/uploads/2011/01/GothamBoldOrangeWithGlowAt20pts1.png" alt="" width="256" height="256" /></a><a href="http://www.learningiphone.com/wp-content/uploads/2011/01/Screen-shot-2011-01-30-at-7.13.28-PM.png"><img class="alignnone size-medium wp-image-343" title="Screen shot 2011-01-30 at 7.13.28 PM" src="http://www.learningiphone.com/wp-content/uploads/2011/01/Screen-shot-2011-01-30-at-7.13.28-PM-300x200.png" alt="" width="300" height="200" /></a></p>
<h2>However what you would really like is something like this:</h2>
<p><a href="http://www.learningiphone.com/wp-content/uploads/2011/01/Screen-shot-2011-01-30-at-7.15.20-PM.png"><img class="alignnone size-medium wp-image-344" title="Screen shot 2011-01-30 at 7.15.20 PM" src="http://www.learningiphone.com/wp-content/uploads/2011/01/Screen-shot-2011-01-30-at-7.15.20-PM-300x203.png" alt="" width="300" height="203" /></a></p>
<p>&lt;hr&gt;</p>
<p>Much better!</p>
<p>So how do we get that, well my method was to implement a iVar &#8216;forcedKerning&#8217; variable into the bitmap font class &#8211; CCLabelBMFont .</p>
<p>This left me with a problem, when i upgraded Cocos2D, i over wrote my changes, and also for a while before that i was afraid to upgrade Cocos2D library version because of what i would break. So i started trying to figure out a different way to implement it using Categories.</p>
<h1>Storing a variable using categories:</h1>
<p>﻿The Main problem, was that I needed to store the variable &#8216;forcedKerning&#8217; for each instance of the class, however I did not want to create a global dictionary or the like, and you cannot create iVars in a category.</p>
<p>Since iOS 3.1, and OSX 10.6 there is something called &#8216;<strong>objc_getAssociatedObject</strong>&#8216; and &#8216;<strong>objc_setAssociatedObject</strong>&#8216;<br />
Which allows you to, essentially create a dynamic property to an existing class.</p>
<p>The &#8216;get&#8217; takes two parameters, the object itself (the class instance), and a void <em>UNIQUE</em> pointer. Because that is used as the key. So the other bit of trickiery was figuring out a pointer, i can use that i can then access in a different method. Namely the setter.</p>
<p>The trick was to use @selector(setMyKey) as the pointer to the property!</p>
<h2>Putting it together, how to force extra kerning with Cocos2D Bitmap font!</h2>
<p>// Create a file called CCLabelBMFont+Kerning.m</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  CCLabelBMFont+Kerning.m</span>
<span style="color: #11740a; font-style: italic;">//  Junny</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #11740a; font-style: italic;">//  Created by Mario Gonzalez on 1/30/11.</span>
<span style="color: #11740a; font-style: italic;">//  Copyright 2011 Whale Island Games. All rights reserved.</span>
<span style="color: #11740a; font-style: italic;">//</span>
<span style="color: #6e371a;">#import &lt;objc/runtime.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> CCLabelBMFont <span style="color: #002200;">&#40;</span>kerning<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> createFontChars;
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>nonatomic, assign<span style="color: #002200;">&#41;</span> <span style="color: #a61390;">float</span> forcedKerning;
<span style="color: #a61390;">@end</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> CCLabelBMFont <span style="color: #002200;">&#40;</span>kerning<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span> forcedKerning <span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span>objc_getAssociatedObject<span style="color: #002200;">&#40;</span>self, <span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>forcedKerning<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> floatValue<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> setForcedKerning<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span>aValue <span style="color: #002200;">&#123;</span>
	<span style="color: #400080;">NSNumber</span> <span style="color: #002200;">*</span>aNumber <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNumber</span> numberWithFloat<span style="color: #002200;">:</span>aValue<span style="color: #002200;">&#93;</span>;
	objc_setAssociatedObject<span style="color: #002200;">&#40;</span>self, <span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>forcedKerning<span style="color: #002200;">&#41;</span>, aNumber, OBJC_ASSOCIATION_RETAIN_NONATOMIC<span style="color: #002200;">&#41;</span>;
	<span style="color: #002200;">&#91;</span>self createFontChars<span style="color: #002200;">&#93;</span>;
<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> createFontChars
<span style="color: #002200;">&#123;</span>
	<span style="color: #a61390;">int</span> nextFontPositionX <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">int</span> nextFontPositionY <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	unichar prev <span style="color: #002200;">=</span> <span style="color: #002200;">-</span><span style="color: #2400d9;">1</span>;
	<span style="color: #a61390;">int</span> kerningAmount <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
	CGSize tmpSize <span style="color: #002200;">=</span> CGSizeZero;
&nbsp;
	<span style="color: #a61390;">int</span> longestLine <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
	<span style="color: #a61390;">int</span> totalHeight <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
	<span style="color: #a61390;">int</span> quantityOfLines <span style="color: #002200;">=</span> <span style="color: #2400d9;">1</span>;
	<span style="color: #a61390;">float</span> localForcedKerning <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self forcedKerning<span style="color: #002200;">&#93;</span>;
	NSUInteger stringLen <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>string_ length<span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span> stringLen <span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">return</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// quantity of lines NEEDS to be calculated before parsing the lines,</span>
	<span style="color: #11740a; font-style: italic;">// since the Y position needs to be calcualted before hand</span>
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span>NSUInteger i<span style="color: #002200;">=</span><span style="color: #2400d9;">0</span>; i &lt; stringLen<span style="color: #002200;">-</span><span style="color: #2400d9;">1</span>;i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		unichar c <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>string_ characterAtIndex<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> c<span style="color: #002200;">==</span><span style="color: #bf1d1a;">'<span style="color: #2400d9;">\n</span>'</span><span style="color: #002200;">&#41;</span>
			quantityOfLines<span style="color: #002200;">++</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	totalHeight <span style="color: #002200;">=</span> configuration_<span style="color: #002200;">-</span>&gt;commonHeight_ <span style="color: #002200;">*</span> quantityOfLines;
	nextFontPositionY <span style="color: #002200;">=</span> <span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span>configuration_<span style="color: #002200;">-</span>&gt;commonHeight_ <span style="color: #002200;">-</span> configuration_<span style="color: #002200;">-</span>&gt;commonHeight_<span style="color: #002200;">*</span>quantityOfLines<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span>NSUInteger i<span style="color: #002200;">=</span><span style="color: #2400d9;">0</span>; i&lt;stringLen; i<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		unichar c <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>string_ characterAtIndex<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span>;
		NSAssert<span style="color: #002200;">&#40;</span> c &lt; kCCBMFontMaxChars, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;BitmapFontAtlas: character outside bounds&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>c <span style="color: #002200;">==</span> <span style="color: #bf1d1a;">'<span style="color: #2400d9;">\n</span>'</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			nextFontPositionX <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
			nextFontPositionY <span style="color: #002200;">-=</span> configuration_<span style="color: #002200;">-</span>&gt;commonHeight_;
			<span style="color: #a61390;">continue</span>;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		kerningAmount <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#91;</span>self kerningAmountForFirst<span style="color: #002200;">:</span>prev second<span style="color: #002200;">:</span>c<span style="color: #002200;">&#93;</span>;
&nbsp;
		ccBMFontDef fontDef <span style="color: #002200;">=</span> configuration_<span style="color: #002200;">-</span>&gt;BMFontArray_<span style="color: #002200;">&#91;</span>c<span style="color: #002200;">&#93;</span>;
&nbsp;
		CGRect rect <span style="color: #002200;">=</span> fontDef.rect;
&nbsp;
		CCSprite <span style="color: #002200;">*</span>fontChar;
&nbsp;
		fontChar <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span>CCSprite<span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#91;</span>self getChildByTag<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> <span style="color: #002200;">!</span> fontChar <span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			fontChar <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CCSprite alloc<span style="color: #002200;">&#93;</span> initWithBatchNode<span style="color: #002200;">:</span>self rectInPixels<span style="color: #002200;">:</span>rect<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>self addChild<span style="color: #002200;">:</span>fontChar z<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span> tag<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span>;
			<span style="color: #002200;">&#91;</span>fontChar release<span style="color: #002200;">&#93;</span>;
		<span style="color: #002200;">&#125;</span>
		<span style="color: #a61390;">else</span> <span style="color: #002200;">&#123;</span>
			<span style="color: #11740a; font-style: italic;">// reusing fonts</span>
			<span style="color: #002200;">&#91;</span>fontChar setTextureRectInPixels<span style="color: #002200;">:</span>rect rotated<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span> untrimmedSize<span style="color: #002200;">:</span>rect.size<span style="color: #002200;">&#93;</span>;
&nbsp;
			<span style="color: #11740a; font-style: italic;">// restore to default in case they were modified</span>
			fontChar.visible <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;
			fontChar.opacity <span style="color: #002200;">=</span> <span style="color: #2400d9;">255</span>;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #a61390;">float</span> yOffset <span style="color: #002200;">=</span> configuration_<span style="color: #002200;">-</span>&gt;commonHeight_ <span style="color: #002200;">-</span> fontDef.yOffset;
		fontChar.positionInPixels <span style="color: #002200;">=</span> ccp<span style="color: #002200;">&#40;</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span>nextFontPositionX <span style="color: #002200;">+</span> fontDef.xOffset <span style="color: #002200;">+</span> fontDef.rect.size.width<span style="color: #002200;">*</span>0.5f <span style="color: #002200;">+</span> kerningAmount,
										<span style="color: #002200;">&#40;</span><span style="color: #a61390;">float</span><span style="color: #002200;">&#41;</span>nextFontPositionY <span style="color: #002200;">+</span> yOffset <span style="color: #002200;">-</span> rect.size.height<span style="color: #002200;">*</span>0.5f <span style="color: #002200;">&#41;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// update kerning</span>
&nbsp;
		nextFontPositionX <span style="color: #002200;">+=</span> configuration_<span style="color: #002200;">-</span>&gt;BMFontArray_<span style="color: #002200;">&#91;</span>c<span style="color: #002200;">&#93;</span>.xAdvance <span style="color: #002200;">+</span> kerningAmount;
		nextFontPositionX <span style="color: #002200;">+=</span> localForcedKerning;
&nbsp;
		prev <span style="color: #002200;">=</span> c;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// Apply label properties</span>
		<span style="color: #002200;">&#91;</span>fontChar setOpacityModifyRGB<span style="color: #002200;">:</span>opacityModifyRGB_<span style="color: #002200;">&#93;</span>;
		<span style="color: #11740a; font-style: italic;">// Color MUST be set before opacity, since opacity might change color if OpacityModifyRGB is on</span>
		<span style="color: #002200;">&#91;</span>fontChar setColor<span style="color: #002200;">:</span>color_<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #11740a; font-style: italic;">// only apply opacity if it is different than 255 )</span>
		<span style="color: #11740a; font-style: italic;">// to prevent modifying the color too (issue #610)</span>
		<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span> opacity_ <span style="color: #002200;">!=</span> <span style="color: #2400d9;">255</span> <span style="color: #002200;">&#41;</span>
			<span style="color: #002200;">&#91;</span>fontChar setOpacity<span style="color: #002200;">:</span> opacity_<span style="color: #002200;">&#93;</span>;
&nbsp;
		<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>longestLine &lt; nextFontPositionX<span style="color: #002200;">&#41;</span>
			longestLine <span style="color: #002200;">=</span> nextFontPositionX;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	tmpSize.width <span style="color: #002200;">=</span> longestLine;
	tmpSize.height <span style="color: #002200;">=</span> totalHeight;
&nbsp;
	<span style="color: #002200;">&#91;</span>self setContentSizeInPixels<span style="color: #002200;">:</span>tmpSize<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">@end</span></pre></div></div>

<p>For now i&#8217;m duplicating the original createFontChars method, via copy paste, this is a big nono &#8211; but im looking to undo that, and it&#8217;s still much better than modifying the Libraries class file and being stuck to it forever. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.learningiphone.com/2011/01/cocos2d-forced-kerning/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>New Stanford course is online</title>
		<link>http://www.learningiphone.com/2011/01/new-stanford-course-is-online/</link>
		<comments>http://www.learningiphone.com/2011/01/new-stanford-course-is-online/#comments</comments>
		<pubDate>Thu, 27 Jan 2011 14:02:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.learningiphone.com/?p=333</guid>
		<description><![CDATA[I learned a lot by watching the original course, nothing like having someone explain in lecture form, after reading about these concepts for a while. And now it looks like, they&#8217;ve updated the course. The new instructor has a more calm style, seems like it will be good. http://itunes.apple.com/us/itunes-u/developing-apps-for-ios-sd/id395631522 This is from the first few minutes [...]]]></description>
			<content:encoded><![CDATA[<p>I learned a lot by watching the original course, nothing like having someone explain in lecture form, after reading about these concepts for a while. And now it looks like, they&#8217;ve updated the course.</p>
<p>The new instructor has a more calm style, seems like it will be good.<br />
<a href="http://itunes.apple.com/us/itunes-u/developing-apps-for-ios-sd/id395631522">http://itunes.apple.com/us/itunes-u/developing-apps-for-ios-sd/id395631522</a></p>
<h3>This is from the first few minutes of the first lecture.</h3>
<p><a href="http://www.learningiphone.com/wp-content/uploads/2011/01/Screen-shot-2011-01-27-at-8.50.56-AM.png"><img class="alignnone size-full wp-image-334" title="Screen shot 2011-01-27 at 8.50.56 AM" src="http://www.learningiphone.com/wp-content/uploads/2011/01/Screen-shot-2011-01-27-at-8.50.56-AM.png" alt="" width="380" height="223" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.learningiphone.com/2011/01/new-stanford-course-is-online/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Better NSLog &#8211; Adding function name / line number to NSLog</title>
		<link>http://www.learningiphone.com/2010/10/better-nslog-adding-function-name-line-number-to-nslog/</link>
		<comments>http://www.learningiphone.com/2010/10/better-nslog-adding-function-name-line-number-to-nslog/#comments</comments>
		<pubDate>Sat, 30 Oct 2010 22:55:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.learningiphone.com/?p=301</guid>
		<description><![CDATA[When tracking down a bug, the most useful way to start checking things is to NSLog something. I&#8217;m a heavy user of breakpoints and the debugger. I can&#8217;t live without it, and it&#8217;s saved me countless hours. However I usually see if i can NSLog something first perhaps the problem was an obvious one. A [...]]]></description>
			<content:encoded><![CDATA[<p>When tracking down a bug, the most useful way to start checking things is to NSLog something. I&#8217;m a heavy user of breakpoints and the debugger. I can&#8217;t live without it, and it&#8217;s saved me countless hours. However I usually see if i can NSLog something first perhaps the problem was an obvious one. </p>
<p>A problem that frequently occurs is that when trying to track down a bug, you end up with many NSLog statements in the console and it&#8217;s difficult to know which is which. Often you&#8217;ll see coders start adding things like &#8220;>>>&#8221; or &#8220;#&#8221;, to differentiate this NEW NSLog statement from the ones already showing up.</p>
<p>An easy solution for this, is to use a better logging function one that can tell you the function from which it was called, and the line number.<br />
The compiler has certain special variables you can use that give you this information. With that knowledge, creating a macro for a better log function becomes very simple.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define VERBOSE_DEBUG_ENABLED_DEBUG 1 // Set to 0 to turn off</span>
<span style="color: #6e371a;">#if !defined(VERBOSE_DEBUG_ENABLED) || VERBOSE_DEBUG_ENABLED == 0 </span>
<span style="color: #6e371a;">#define LOGVERBOSE(...) do{}while(0)</span>
<span style="color: #6e371a;">#else</span>
<span style="color: #6e371a;">#define LOGVERBOSE(__FORMAT__, ...) NSLog(@&quot;%s-Line: %d-&gt;%@&quot;, __func__, __LINE__, [NSString stringWithFormat:__FORMAT__, __VA_ARGS__])</span>
<span style="color: #6e371a;">#endif</span></pre></div></div>

<p>Here we are using the special compiler directives (i&#8217;m not sure what their proper terms are), __func__, and __LINE__ to create a new NSLog statement that then uses, stringWithFormat: to wrap the user&#8217;s log call so that it behaves exactly like NSLog.</p>
<p>In practice it is used like this</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#include SOME_HEADER_FILE_WITH_ABOVE_CODE</span>
...
LOGVERBOSE<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;someVar: %0.2f&quot;</span>, someVar<span style="color: #002200;">&#41;</span>;
<span style="color: #11740a; font-style: italic;">// Outputs</span>
<span style="color: #11740a; font-style: italic;">// -[SomeViewControllerSubclass myFunction:]-Line:32-&gt;someVar: 4.34</span></pre></div></div>

<p>That&#8217;s all there is to it. Very useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learningiphone.com/2010/10/better-nslog-adding-function-name-line-number-to-nslog/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The best form submission class (asi-http-request)</title>
		<link>http://www.learningiphone.com/2010/10/the-best-form-submission-class-asi-http-request/</link>
		<comments>http://www.learningiphone.com/2010/10/the-best-form-submission-class-asi-http-request/#comments</comments>
		<pubDate>Tue, 26 Oct 2010 15:10:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.learningiphone.com/?p=289</guid>
		<description><![CDATA[Small but quick snippet here. Sending multi-part form with image data: NSString *imagePath = NSString *imagePath = &#91;NSString stringWithFormat:@&#34;%@/%@&#34;, &#91;NSSearchPathForDirectoriesInDomains&#40;NSDocumentDirectory, NSUserDomainMask, YES&#41; lastObject&#93;, @&#34;junny.png&#34;&#93;; &#160; UIImage *image = &#91;UIImage imageWithContentsOfFile:imagePath&#93;; NSData * imageData = UIImageJPEGRepresentation&#40;image, 0.75&#41; &#160; ASIFormDataRequest *request = &#91;ASIFormDataRequest requestWithURL:&#91;NSURL URLWithString:http://example.com/form.php]]; &#91;request setPostValue:@&#34;I like brains!&#34; forKey:@&#34;title&#34;&#93;; &#91;request setPostValue:&#91;NSString stringWithFormat:@&#34;%@&#34;, &#91;NSDate date&#93;&#93; forKey:@&#34;session&#34;&#93;;l &#91;request [...]]]></description>
			<content:encoded><![CDATA[<p>Small but quick snippet here.</p>
<p>Sending multi-part form with image data:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">        <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>imagePath <span style="color: #002200;">=</span> 	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>imagePath <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@/%@&quot;</span>,
						   <span style="color: #002200;">&#91;</span>NSSearchPathForDirectoriesInDomains<span style="color: #002200;">&#40;</span>NSDocumentDirectory, NSUserDomainMask, <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span> lastObject<span style="color: #002200;">&#93;</span>,
						   <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;junny.png&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
        UIImage <span style="color: #002200;">*</span>image <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIImage imageWithContentsOfFile<span style="color: #002200;">:</span>imagePath<span style="color: #002200;">&#93;</span>;
        <span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span> imageData <span style="color: #002200;">=</span> UIImageJPEGRepresentation<span style="color: #002200;">&#40;</span>image, <span style="color: #2400d9;">0.75</span><span style="color: #002200;">&#41;</span>
&nbsp;
	ASIFormDataRequest <span style="color: #002200;">*</span>request <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>ASIFormDataRequest requestWithURL<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span>http<span style="color: #002200;">:</span><span style="color: #11740a; font-style: italic;">//example.com/form.php]];</span>
	<span style="color: #002200;">&#91;</span>request setPostValue<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;I like brains!&quot;</span> forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;title&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>request setPostValue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%@&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;session&quot;</span><span style="color: #002200;">&#93;</span>;l
	<span style="color: #002200;">&#91;</span>request setPostValue<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;image.png&quot;</span> forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Filename&quot;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>request setData<span style="color: #002200;">:</span>imageData forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Filedata&quot;</span><span style="color: #002200;">&#93;</span>; <span style="color: #11740a; font-style: italic;">// Get image, convert to JPG, get data</span>
	<span style="color: #002200;">&#91;</span>request setDelegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>request startAsynchronous<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>The <a href="http://github.com/pokeb/asi-http-request">ASIFormDataRequest</a>, is the BEST class for handling POST/GET/Multi-Part http request. It has a lot of classes, and its a little bulkier because it does what it does amazingly well. I&#8217;ve tried a few other implementations and this thing saved my life at 4am when I was about to start crying on this project</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learningiphone.com/2010/10/the-best-form-submission-class-asi-http-request/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Parsing Delimited String from XMLNode on iOS using GDataXMLNode</title>
		<link>http://www.learningiphone.com/2010/10/280/</link>
		<comments>http://www.learningiphone.com/2010/10/280/#comments</comments>
		<pubDate>Thu, 14 Oct 2010 16:32:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.learningiphone.com/?p=280</guid>
		<description><![CDATA[Often when you work with XML, you need to put a bunch of values into a node, maybe positioning information for example. The best way to do that is to create a &#8220;Delimited String&#8221;, that is &#8220;ABC&#124;123&#124;XYZ&#124;987&#8243;, is delimited by the &#124; character. Here&#8217;s how you get that back in iOS into an array, that [...]]]></description>
			<content:encoded><![CDATA[<p>Often when you work with XML, you need to put a bunch of values into a node, maybe positioning information for example.<br />
The best way to do that is to create a &#8220;Delimited String&#8221;, that is &#8220;ABC|123|XYZ|987&#8243;, is delimited by the | character.</p>
<p>Here&#8217;s how you get that back in iOS into an array, that you can then loop through and make use of:</p>
<p>Btw I&#8217;m using <a href="http://code.google.com/p/gdata-objectivec-client/source/browse/trunk/Source/XMLSupport/">GDataXML</a>, which is googles Objective-C xml reading/writing library. Needless to say it&#8217;s very good, and very fast.</p>
<p>Ok, so let&#8217;s turn this into an array:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pos<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>297.00,30.00<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pos<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Broken into multiple lines to fit, it's a long one!</span>
<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>myPositionList <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>xmltree elementsForName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;pos&quot;</span> <span style="color: #002200;">&#93;</span> objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> stringValue<span style="color: #002200;">&#93;</span>
     stringByTrimmingCharactersInSet<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSCharacterSet</span> whitespaceAndNewlineCharacterSet<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> 
           componentsSeparatedByString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;,&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.learningiphone.com/2010/10/280/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reading / Creating XML in iOS</title>
		<link>http://www.learningiphone.com/2010/10/reading-and-writing-xml-data-in-ios/</link>
		<comments>http://www.learningiphone.com/2010/10/reading-and-writing-xml-data-in-ios/#comments</comments>
		<pubDate>Mon, 11 Oct 2010 04:21:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[cross-post]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[uikit]]></category>

		<guid isPermaLink="false">http://www.learningiphone.com/?p=274</guid>
		<description><![CDATA[As I&#8217;ve mentioned previously, this blog serves as a kind of social-bookmarker, and exist somewhat in order for me to chronologically log my learning experience with iOS programming. So not everything I post is always a how-to or a tip, but sometimes I will share something which I read that I found useful that people [...]]]></description>
			<content:encoded><![CDATA[<p>As I&#8217;ve mentioned previously, this blog serves as a kind of social-bookmarker, and exist somewhat in order for me to chronologically log my learning experience with iOS programming. So not everything I post is always a how-to or a tip, but sometimes I will share something which I read that I found useful that people learning as I learn might find useful, not to mention I often reference my own blog to remember stuff.</p>
<p>Anyway, this is a great write up on using GDataXML for reading/creating XML in iOS.<br />
<a href="http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml ">http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml </a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.learningiphone.com/2010/10/reading-and-writing-xml-data-in-ios/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Post-A-Day #10 UIViewController&#8217;s views</title>
		<link>http://www.learningiphone.com/2010/10/post-a-day-10-uiviewcontrollers-views/</link>
		<comments>http://www.learningiphone.com/2010/10/post-a-day-10-uiviewcontrollers-views/#comments</comments>
		<pubDate>Thu, 07 Oct 2010 00:34:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.learningiphone.com/?p=272</guid>
		<description><![CDATA[-&#40;void&#41; someEventDidOccur EditOptionsViewController *anEditOptionsViewController = &#91;modalViewControllers_ objectForKey:@&#34;someName&#34;&#93;; &#160; // A little note about retain/release // views are not aware of their UIViewController // When they don't have one, and .view is requested UIViewController will simply create a new one. // In the code below, if the order were reversed, &#34;anEditOptionsViewController&#34; would be dealloced // // [...]]]></description>
			<content:encoded><![CDATA[
<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> someEventDidOccur
EditOptionsViewController <span style="color: #002200;">*</span>anEditOptionsViewController <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>modalViewControllers_ objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;someName&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// A little note about retain/release</span>
	<span style="color: #11740a; font-style: italic;">//	views are not aware of their UIViewController</span>
	<span style="color: #11740a; font-style: italic;">//		When they don't have one, and .view is requested UIViewController will simply create a new one.</span>
	<span style="color: #11740a; font-style: italic;">//	In the code below, if the order were reversed, &quot;anEditOptionsViewController&quot; would be dealloced</span>
	<span style="color: #11740a; font-style: italic;">//</span>
	<span style="color: #11740a; font-style: italic;">//	After wich the subsequent .view call, or '[anEditOptionsViewController view]' would create a new view, Havoc ensues</span>
	<span style="color: #002200;">&#91;</span>anEditOptionsViewController.view removeFromSuperview<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>modalViewControllers_ removeObjectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;someName&quot;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.learningiphone.com/2010/10/post-a-day-10-uiviewcontrollers-views/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Post-A-Day #9 &#8211; Blocks Part 1</title>
		<link>http://www.learningiphone.com/2010/10/post-a-day-9-blocks-part-1/</link>
		<comments>http://www.learningiphone.com/2010/10/post-a-day-9-blocks-part-1/#comments</comments>
		<pubDate>Wed, 06 Oct 2010 00:59:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.learningiphone.com/?p=268</guid>
		<description><![CDATA[In objective-C a Block is a type of closure. A closure, is a method that contains state information, using free variables. It binds the variables for you on the stack, so in that sense it &#8220;closes&#8221; the expression. To give an example in javascript you could call setTimeout this way: function callLater&#40;first, second, third&#41; &#123; [...]]]></description>
			<content:encoded><![CDATA[<p>In objective-C a <code>Block</code> is a type of closure.<br />
A closure, is a method that contains state information, using free variables. It binds the variables for you on the stack, so in that sense it &#8220;closes&#8221; the expression.</p>
<p>To give an example in javascript you could call <code>setTimeout</code> this way:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> callLater<span style="color: #009900;">&#40;</span>first<span style="color: #339933;">,</span> second<span style="color: #339933;">,</span> third<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>
		<span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #006600; font-style: italic;">/* This inner function is to be executed with - setTimeout
			   - and when it is executed it can read, and act upon, the
			   parameters passed to the outer function:-
			*/</span>
			total <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
			<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> third<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span>
			<span style="color: #009900;">&#123;</span>
				total <span style="color: #339933;">+=</span> first <span style="color: #339933;">*</span> second<span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;">return</span> total<span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
a <span style="color: #339933;">=</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">;</span>
b <span style="color: #339933;">=</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span>
c <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
functionReference <span style="color: #339933;">=</span> callLater<span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> b<span style="color: #339933;">,</span> c<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
setTimeout<span style="color: #009900;">&#40;</span>functionReference<span style="color: #339933;">,</span> <span style="color: #CC0000;">2500</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>A closure allows you to bind free variables, it &#8220;closes&#8221; the encapsultation.<br />
Pretty useful stuff&#8230;</p>
<p>In Objective-C this can be accomplished using a language feature called Blocks, this is what one looks like</p>
<p><a href="http://www.learningiphone.com/wp-content/uploads/2010/10/Untitled-1.png"><img src="http://www.learningiphone.com/wp-content/uploads/2010/10/Untitled-1.png" alt="" title="Blocks #1" width="700" height="274" class="alignnone size-full wp-image-269" /></a></p>
<p>Objective-C and the Foundation framework make heavy usage of Blocks, for things like sorting arrays &#8211; performing things on threads, etc.</p>
<p>I&#8217;ll talk more about that later, but with the above you can at least create a Block and get started, in understanding their somewhat cryptic looking syntax.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learningiphone.com/2010/10/post-a-day-9-blocks-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Post-A-Day #8 &#8211; Allow superview to recieve events caught by subview.</title>
		<link>http://www.learningiphone.com/2010/10/post-a-day-8-allow-superview-to-recieve-events-caught-by-subview-2/</link>
		<comments>http://www.learningiphone.com/2010/10/post-a-day-8-allow-superview-to-recieve-events-caught-by-subview-2/#comments</comments>
		<pubDate>Mon, 04 Oct 2010 00:27:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.learningiphone.com/?p=259</guid>
		<description><![CDATA[I had a problem in my current project related to this, and it was a good opportunity, to learn a little bit more about how touches are handled in iOS. Enter UIResponder UIResponder is the Class any objects that which to receive events must Subclass, if they wish to receive (among other types) TouchEvents from [...]]]></description>
			<content:encoded><![CDATA[<p>I had a problem in my current project related to this, and it was a good opportunity, to learn a little bit more about how touches are handled in iOS.</p>
<h1>Enter UIResponder</h1>
<p><code>UIResponder</code> is the Class any objects that which to receive events must Subclass, if they wish to receive (among other types) TouchEvents from the application.<br />
<code>UIView</code> and <code>UIViewController</code> are two distinct  Subclasses of UIResponder. However keep in mind that UIViewController is not a subclass of <code>UIView</code>, nor is <code>UIView</code> a subclass of <code>UIViewController</code>. Each is distinct.</p>
<p>For the purposes of this discussion, just know that it implements these four methods.<br />
<code><br />
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;<br />
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;<br />
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;<br />
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;<br />
</code></p>
<h1>The responder chain</h1>
<p>All active <code>UIResponders</code> are actually part of the &#8216;<code>ResponderChain</code>&#8216; . In other words; each one knows about the next one in the <code>ResponderChain</code>.  This is probably done this way to to run as fast as possible during<code> RunTime</code>. </p>
<p>The tail of the responder chain, for applications running on iOS is the <code>UIWindow</code>.<br />
When a user taps the screen, the device figures out who to call &#8216;<code>touchesBegan</code>&#8216; on by calling  <code>–hitTest:withEvent:</code> on all active responders. This function in turn calls, <code>–pointInside:withEvent:</code> on of that views subviews. </p>
<p>So if you had objectA, inside of objectB, which is inside of objectC. Actually let&#8217;s use a more concrete example: imagine you had Person, inside of a House, inside of Block.<br />
In our pseudo real world example the responder chain might look like this: Person->House->Block->&#8230;->Universe.<br />
When a user touches the screen, PersonA is the head of the responder chain, he checks if the tap happened on him by checking &#8216;pointInside:withEvent:&#8217;, if the response is NO. The next object in the ResponderChain, in our case House does the same check. Eventually if no one caught the TouchEvent, the universe receives it and can do something with it or nothing.</p>
<p>The way apple has implemented the ResponderChain actually makes it much simpler to &#8216;bubble up&#8217; a touch event however.<br />
To bubble up a touch event from a subView to it&#8217;s parentView, you simply call <code>-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;</code> you simply call on super. For example:</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>touchesBegan<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSSet</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>touches withEvent<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIEvent <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>event
<span style="color: #002200;">&#123;</span>
     <span style="color: #11740a; font-style: italic;">// Forward this message up the responder chain</span>
     <span style="color: #002200;">&#91;</span>super touchesBegan<span style="color: #002200;">:</span>touches withEvent<span style="color: #002200;">:</span> event<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.learningiphone.com/2010/10/post-a-day-8-allow-superview-to-recieve-events-caught-by-subview-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
