[center addObserverForName: kGRZoneSystem_ZoneSystemChangedNotification object: nil queue: [NSOperationQueue mainQueue] usingBlock: ^(NSNotification *notification) { [self adaptToZoneChange]; }];
extern NSString *BWStitchGroup_VisualAttributeChangeNotification;
In the .m
file:
NSString *BWStitchGroup_VisualAttributeChangeNotification
= @"BWStitchGroup Visual Attribute Change Notification";
...
NSNotificationCenter *center; center = [NSNotificationCenter defaultCenter]; [center postNotificationName: BWStitchGroup_VisualAttributeChangeNotification object: self];If you want to pass a userinfo dictionary, you can use a variant method:
NSDictionary *userInfo = ...; [center postNotificationName: BWStitchGroup_VisualAttributeChangeNotification object: self userInfo: userInfo];
NSNotificationCenter *center; center = [NSNotificationCenter defaultCenter]; [center addObserver: self selector: @selector(groupVisualChange:) name: BWStitchGroup_VisualAttributeChangeNotification object: group];Where the selector looks like this:
- (void) groupVisualChange: (NSNotification *) notification { // do stuff with the notification } // groupVisualChange
-dealloc
:
NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center removeObserver: self];
#import <Cocoa/Cocoa.h> #import <stdio.h> // for printf() @interface NotificationSpy { } + (void) startSpying; + (void) stopSpying; @end // NotificationSpy // prevent us from adding ourselves multiple times static BOOL g_spying; @implementation NotificationSpy // turn on listening for all notifications. + (void) startSpying { if (!g_spying) { NSNotificationCenter *center; // first the default notification center, which is all // notifications that just happen inside of our program center = [NSNotificationCenter defaultCenter]; [center addObserver: self selector: @selector(observeDefaultCenterStuff:) name: nil object: nil]; // then the NSWorkspace notification center, which tells us things // like other applications launching, and the machine sleeping // and waking center = [[NSWorkspace sharedWorkspace] notificationCenter]; [center addObserver: self selector: @selector(observeWorkspaceStuff:) name: nil object: nil]; // and lastly the distributed notification center. This is a // global (to the computer) notification center. You can find // out when a different program gets focus, or the sound or // screen brightness changes. center = [NSDistributedNotificationCenter notificationCenterForType: NSLocalNotificationCenterType]; [center addObserver: self selector: @selector(observeDistributedStuff:) name: nil object: nil]; g_spying = YES; } } // startSpying // remove us as observers + (void) stopSpying { if (!g_spying) { NSNotificationCenter *center; // take us off the default center for our app center = [NSNotificationCenter defaultCenter]; [center removeObserver: self]; // and for the workspace center = [[NSWorkspace sharedWorkspace] notificationCenter]; [center removeObserver: self]; // and finally off of the machine-wide center center = [NSDistributedNotificationCenter notificationCenterForType: NSLocalNotificationCenterType]; [center removeObserver: self]; g_spying = NO; } } // stopSpying + (void) observeDefaultCenterStuff: (NSNotification *) notification { // QuietLog can also be found in the quickies QuietLog (@"default: %@", [notification name]); } // observeDefaultCenterStuff + (void) observeDistributedStuff: (NSNotification *) notification { QuietLog (@"distributed: %@", [notification name]); } // observeDistributedStuff + (void) observeWorkspaceStuff: (NSNotification *) notification { QuietLog (@"workspace: %@", [notification name]); } // observeWorkspaceStuff @end // NotificationSpy