捷径系列:Notification

该系列文章来自http://borkware.com/quickies/。无论是学习还是开发都可以从这里获得很多有用的代码段,从而省去了很多调查和搜索的时间。

1 发布通知
头文件:

 
  
  1. extern NSString *BWStitchGroup_VisualAttributeChangeNotification;  

.m文件:

 
  
  1. NSString *BWStitchGroup_VisualAttributeChangeNotification  
  2.      = @"BWStitchGroup Visual Attribute Change Notification";  
  3. ...  
  4. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];  
  5. [center postNotificationName:   
  6.         BWStitchGroup_VisualAttributeChangeNotification  
  7.         object: self];  
  8. 如果你需要传递一个userinfo的字典,你可以使用一个演化的方法:  
  9. NSDictionary *userInfo = ...;  
  10. [center postNotificationName: BWStitchGroup_VisualAttributeChangeNotification  
  11.  object: self  
  12.   userInfo: userInfo]; 

2 接收通知

 
  
  1. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];  
  2. [center addObserver: self  
  3.  selector: @selector(groupVisualChange:)  
  4.  name: BWStitchGroup_VisualAttributeChangeNotification  
  5.  object: group]; 

其中选择器大体如下:

 
  
  1. - (void) groupVisualChange: (NSNotification *) notification  
  2. {  
  3.      // do stuff with the notification  
  4. // groupVisualChange 

3 取消通知
在-dealloc中不要忘记取消在通知中心的注册:

 
  
  1. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];  
  2. [center removeObserver: self]; 

4 观察所有通知
Cocoa中有三种类型的通知:一个用于应用,一个用于工作区(Workspace),另一个是分布式通知中心(Distributed Notification Center)。以下是如何同时观察它们代码:

 
  
  1. #import <Cocoa/Cocoa.h>  
  2. #import <stdio.h> // for printf()  
  3.  
  4. @interface NotificationSpy  
  5. {  
  6. }  
  7.  
  8. + (void) startSpying;  
  9. + (void) stopSpying;  
  10.  
  11. @end // NotificationSpy  
  12.  
  13. // 防止重复注册  
  14. static BOOL g_spying;  
  15.  
  16.  
  17. @implementation NotificationSpy  
  18. + (void) startSpying  
  19. {  
  20.      if (!g_spying) {  
  21.         NSNotificationCenter *center;  
  22.  
  23.         // 第一个是默认通知中心,其所有的通知都来自当前应用程序  
  24.         center = [NSNotificationCenter defaultCenter];  
  25.         [center addObserver: self  
  26.                 selector: @selector(observeDefaultCenterStuff:)  
  27.                 name: nil  
  28.                 object: nil];  
  29.  
  30.          // 接着是NSWorkspace通知中心。  
  31.         // 通知其他应用启动、以及机器休眠和唤醒等时间  
  32.         center = [[NSWorkspace sharedWorkspace]  
  33.                   notificationCenter];   
  34.         [center addObserver: self  
  35.                 selector: @selector(observeWorkspaceStuff:)  
  36.                 name: nil  
  37.                 object: nil];  
  38.  
  39.         // 最后是分布式通知中心。  
  40.     // 这是一个全局(整个计算机)的通知中心,  
  41.     // 你可以观察到不同程序获得焦点以及声音屏幕亮度改变等事件  
  42.         center = [NSDistributedNotificationCenter   
  43.                    notificationCenterForType: NSLocalNotificationCenterType];  
  44.         [center addObserver: self  
  45.                 selector: @selector(observeDistributedStuff:)  
  46.                 name: nil  
  47.                 object: nil];  
  48.  
  49.         g_spying = YES;  
  50.     }  
  51. }   
  52.  
  53. + (void) stopSpying  
  54. {  
  55.     if (!g_spying) {  
  56.         NSNotificationCenter *center;  
  57.  
  58.         center = [NSNotificationCenter defaultCenter];  
  59.         [center removeObserver: self];  
  60.  
  61.         center = [[NSWorkspace sharedWorkspace] notificationCenter];  
  62.         [center removeObserver: self];  
  63.  
  64.         center = [NSDistributedNotificationCenter   
  65.         notificationCenterForType: NSLocalNotificationCenterType];  
  66.         [center removeObserver: self];  
  67.  
  68.         g_spying = NO;  
  69.     }  
  70. }  
  71.  
  72. + (void) observeDefaultCenterStuff: (NSNotification *) notification  
  73. {  
  74.     NSLog(@"default: %@", [notification name]);  
  75. }  
  76.  
  77. + (void) observeDistributedStuff: (NSNotification *) notification  
  78. {  
  79.     NSLog(@"distributed: %@", [notification name]);  
  80. }   
  81.  
  82. + (void) observeWorkspaceStuff: (NSNotification *) notification  
  83. {  
  84.     NSLog(@"workspace: %@", [notification name]);  
  85. }   
  86. @end