Can someone please show me an example of a Cocoa Obj-C object, with a custom notification, how to fire it, subscribe to it, and handle it?
Asked
Active
Viewed 5.0k times
70
-
4Vague question. Try asking a more specific question, or search Apple's documentation. – danielpunkass May 10 '09 at 04:52
-
6I wouldn't normally comment on a question like this, but seeing as how you received a "con" then mine can be a "pro". This question allows for a *concise* answer dealing strictly with the topic. I merely want to find out one simple thing - not *scour* apple's documentation (which would most likely be worth-while anyway). So thanks for asking this question. I see your +15 atm on the question being congruent with my sentiment. – Jacksonkr Mar 26 '12 at 18:58
-
Here is a app that i wrote, it might help you https://github.com/kylelk/Notification-example/tree/master – kyle k Nov 03 '13 at 02:14
3 Answers
82
@implementation MyObject
// Posts a MyNotification message whenever called
- (void)notify {
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:self];
}
// Prints a message whenever a MyNotification is received
- (void)handleNotification:(NSNotification*)note {
NSLog(@"Got notified: %@", note);
}
@end
// somewhere else
MyObject *object = [[MyObject alloc] init];
// receive MyNotification events from any object
[[NSNotificationCenter defaultCenter] addObserver:object selector:@selector(handleNotification:) name:@"MyNotification" object:nil];
// create a notification
[object notify];
For more information, see the documentation for NSNotificationCenter.

Jason Coco
- 77,985
- 20
- 184
- 180
-
What's the point of using notification then? Why not just call [object handleNotification] straight? – user4951 May 28 '12 at 15:44
-
3Loose coupling. Note the "// somewhere else" comment... The notification is a kind of broadcast message. Any object instance can listen to a notification and doesn't need to conform to any particular delegate protocol or similar. There may be many instances listening to a single message. The sender doesn't need to have pointers to the object instance(s) it wishes to notify. – Andrew Hodgkinson Jul 07 '12 at 08:22
45
Step 1:
//register to listen for event
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(eventHandler:)
name:@"eventType"
object:nil ];
//event handler when event occurs
-(void)eventHandler: (NSNotification *) notification
{
NSLog(@"event triggered");
}
Step 2:
//trigger event
[[NSNotificationCenter defaultCenter]
postNotificationName:@"eventType"
object:nil ];

Georg Fritzsche
- 97,545
- 26
- 194
- 236

mracoker
- 886
- 9
- 14
6
Make sure to unregister notification (observer) when your object is deallocated. Apple documentation states: "Before an object that is observing notifications is deallocated, it must tell the notification center to stop sending it notifications".
For Local Notifications the next code is applicable:
[[NSNotificationCenter defaultCenter] removeObserver:self];
And for observers of distributed notifications:
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self];

Grigori A.
- 2,628
- 1
- 21
- 19