If you are living in the ActionScript world and you are looking for something like an event handling system, you might won’t find any. I googled events and custom events, callbacks and finally figured out that they call it “Notifications”.
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.
If you want to listen to a custom event in ActionScript it would look something like this:
function eventHandler(e:Event)
{
trace("event triggered");
}
In your instance you would trigger the event like this:
The Objective-C approach to listen to a Notification looks like this:
addObserver:self
selector:@selector(eventHandler:)
name:@"eventType"
object:nil ];
-(void)eventHandler: (NSNotification *) notification
{
NSLog(@"event triggered");
}
The funny thing is that you don’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.
The “NSNotificationCenter” is something like a Singleton which is one unique Class that manages your events in your application.
And this is how you dispatch or trigger the event:
postNotificationName:@"eventType"
object:nil ];
That’s it. Actually I like the approach in Objective-C even if it is more to write it is pretty easy to read.
Excellent! I knew there was a way for Flash developers to learn Obj-C! thanks for the great post!
Can you dispatch an event from any class? like a NSObject? because I am dispatch ing from a class to the application delegate and I get nothing?
Thanks a lot!
Tried to find this solution in many books, forums and so on during a couple of days!
Thanks again!
I knew handling shared data through the app delegate was a no-no and after I couldn’t get delegation to work (plus it was overkill). Notifications were my goal, this made is super simple, thanks! Rob
hi i have used this code in my application…….this is realy helpful for me. thanks
Thanks for this, quick, concise and works like a charm!
Thanks for this clear comparison. Objective-c is not always easy coming from an as2/3 background!
keep it up man!
Thanks Raphael, that’s a great tip
Thanks! Great post. For .NET programmers, this is similar to RaiseEvent.
this is great! I’m primarily a flash developer and decided to look at an event notification system in objective c — I landed your post first
First, I just blindly copied and pasted your example into my main App Delegate and realized that the first block…
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(eventHandler:)
name:@”eventType”
object:nil ];
…should obviously be placed within a loading complete method like didFinishLaunchingWithOptions. And, then, within my sub views, I able to trigger the event handler in my main app delegate by adding:
[[NSNotificationCenter defaultCenter]
postNotificationName:@”eventType”
object:nil ];
to a method — ie a button handler — within each sub view. sweet! now I have a notification system that I’m familiar throughout my application. I’m sure you saved me lots of time and headaches.
Excellent tutorial!
I’m been looking for this information for a long time. Thank you Raphael. Great work!
Oh Yeah !
That’s exactly wath I was looking for !
Just like the others, you saved me a lot of time and headaches !
just thanks
Dude,
this is exactly what I was looking for. Thanks!
Top man!
First hit I got in Google and coming from a Flex/Parsley background, your post was spot on.
Keep it coming
Just what i needed! Thanks!
[...] Custom Events in Objective-C http://www.rwichmann.com/2009/03/02/custom-events-in-objective-c/ [...]
awesome. I wish every tutorial was so short, precise and to the point !