QuickTip: Notifications in Cocoa-touch
A very handy thing in cocoa is the use of notifications.
Although it doesn’t work exactly the same as Events in AS3, it can be used for the same principal.
There are 3 functions you need to keep in mind.
- (void)addObserver:(id)notificationObserver selector:(SEL)notificationSelector name:(NSString *)notificationName object:(id)notificationSender - (void)postNotificationName:(NSString *)notificationName object:(id)notificationSender userInfo:(NSDictionary *)userInfo - (void)removeObserver:(id)notificationObserver name:(NSString *)notificationName object:(id)notificationSender
addObserver is the same as addEventListener
removeObserver is the same as removeEventListener
postNotification is the same as dispatchEvent
Small demo:
//AppDelegate.m - (void)applicationDidFinishLaunching:(UIApplication *)application { //…. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handler:) name:@"event" object:nil]; } -(void)handler:(NSNotification *)notification { NSLog(@"%@",notification); //will output: //NSConcreteNotification 0x574ef0 {name = event; object = <rootViewController: 0x580530>} } //RootViewController.m - (void)viewDidLoad { //… [[NSNotificationCenter defaultCenter] postNotificationName:@"event" object:self]; }
This topic is quite hot in the net at the moment. What do you pay attention to while choosing what to write ?
great tips. I enjoyed reading this,
Don
Great post, well written!,
Hi! The post is really interesting! I
Andy, i must say, it’s nice to have found your blog…i am an as3 developer currently learning cocoa and it’s so funny how i am always thinking…’oh, that’s like XXX in AS3′. Thanks man, will be reading more.