<?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>Raphael Wichmann &#187; notifications</title>
	<atom:link href="http://www.rwichmann.com/tag/notifications/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rwichmann.com</link>
	<description>ActionScript and other computer related stuff.</description>
	<lastBuildDate>Tue, 30 Aug 2011 10:58:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Custom Events in Objective-C</title>
		<link>http://www.rwichmann.com/2009/03/02/custom-events-in-objective-c/</link>
		<comments>http://www.rwichmann.com/2009/03/02/custom-events-in-objective-c/#comments</comments>
		<pubDate>Mon, 02 Mar 2009 19:59:38 +0000</pubDate>
		<dc:creator>Raphael</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[custom]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[notifications]]></category>

		<guid isPermaLink="false">http://www.rwichmann.com/?p=87</guid>
		<description><![CDATA[If you are living in the ActionScript world and you are looking for something like an event handling system, you might won&#8217;t find any. I googled events and custom events, callbacks and finally figured out that they call it &#8220;Notifications&#8221;. Events or in this case Notifications are used for having loosely coupled relationships between instances [...]]]></description>
			<content:encoded><![CDATA[<p>If you are living in the ActionScript world and you are looking for something like an event handling system, you might won&#8217;t find any. I googled events and custom events, callbacks and finally figured out that they call it &#8220;Notifications&#8221;.</p>
<p>Events or in this case Notifications are used for having loosely coupled relationships between instances in your application. So it is possible to let other instances listen to specific events without knowing each other.</p>
<p>If you want to listen to a custom event in ActionScript it would look something like this:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">instance<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;eventType&quot;</span><span style="color: #000066; font-weight: bold;">,</span> eventHandler<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<br />
<span style="color: #339966; font-weight: bold;">function</span> eventHandler<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;event triggered&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span><br />
<span style="color: #000000;">&#125;</span></div></div>
<p>In your instance you would trigger the event like this:</p>
<div class="codecolorer-container actionscript3 default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript3 codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #004993;">dispatchEvent</span><span style="color: #000000;">&#40;</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Event</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;eventType&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></div></div>
<p>The Objective-C approach to listen to a Notification looks like this:</p>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span><br />
&nbsp; &nbsp; addObserver<span style="color: #002200;">:</span>self<br />
&nbsp; &nbsp; selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>eventHandler<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span><br />
&nbsp; &nbsp; name<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;eventType&quot;</span><br />
&nbsp; &nbsp; object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> <span style="color: #002200;">&#93;</span>;<br />
<br />
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>eventHandler<span style="color: #002200;">:</span> <span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> notification<br />
<span style="color: #002200;">&#123;</span><br />
&nbsp; &nbsp; NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;event triggered&quot;</span><span style="color: #002200;">&#41;</span>;<br />
<span style="color: #002200;">&#125;</span></div></div>
<p>The funny thing is that you don&#8217;t need to put this Event Listener on an instance. It just listens to the eventtype and as far as I understood you can trigger this event wherever you want. This makes it even more loosely coupled than the instance.addEventListener(); approach.</p>
<p>The &#8220;NSNotificationCenter&#8221; is something like a Singleton which is one unique Class that manages your events in your application.</p>
<p>And this is how you dispatch or trigger the event:</p>
<div class="codecolorer-container objc default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="objc codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span><br />
&nbsp; &nbsp; postNotificationName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;eventType&quot;</span><br />
&nbsp; &nbsp; object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> <span style="color: #002200;">&#93;</span>;</div></div>
<p>That&#8217;s it. Actually I like the approach in Objective-C even if it is more to write it is pretty easy to read.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rwichmann.com/2009/03/02/custom-events-in-objective-c/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>

