捷径系列:Notification
该系列文章来自http://borkware.com/quickies/。无论是学习还是开发都可以从这里获得很多有用的代码段,从而省去了很多调查和搜索的时间。
1 发布通知
头文件:
- extern NSString *BWStitchGroup_VisualAttributeChangeNotification;
.m文件:
- NSString *BWStitchGroup_VisualAttributeChangeNotification
- = @"BWStitchGroup Visual Attribute Change Notification";
- ...
- NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
- [center postNotificationName:
- BWStitchGroup_VisualAttributeChangeNotification
- object: self];
- 如果你需要传递一个userinfo的字典,你可以使用一个演化的方法:
- NSDictionary *userInfo = ...;
- [center postNotificationName: BWStitchGroup_VisualAttributeChangeNotification
- object: self
- userInfo: userInfo];
2 接收通知
- NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
- [center addObserver: self
- selector: @selector(groupVisualChange:)
- name: BWStitchGroup_VisualAttributeChangeNotification
- object: group];
其中选择器大体如下:
- - (void) groupVisualChange: (NSNotification *) notification
- {
- // do stuff with the notification
- } // groupVisualChange
3 取消通知
在-dealloc中不要忘记取消在通知中心的注册:
- NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
- [center removeObserver: self];
4 观察所有通知
Cocoa中有三种类型的通知:一个用于应用,一个用于工作区(Workspace),另一个是分布式通知中心(Distributed Notification Center)。以下是如何同时观察它们代码:
- #import <Cocoa/Cocoa.h>
- #import <stdio.h> // for printf()
- @interface NotificationSpy
- {
- }
- + (void) startSpying;
- + (void) stopSpying;
- @end // NotificationSpy
- // 防止重复注册
- static BOOL g_spying;
- @implementation NotificationSpy
- + (void) startSpying
- {
- if (!g_spying) {
- NSNotificationCenter *center;
- // 第一个是默认通知中心,其所有的通知都来自当前应用程序
- center = [NSNotificationCenter defaultCenter];
- [center addObserver: self
- selector: @selector(observeDefaultCenterStuff:)
- name: nil
- object: nil];
- // 接着是NSWorkspace通知中心。
- // 通知其他应用启动、以及机器休眠和唤醒等时间
- center = [[NSWorkspace sharedWorkspace]
- notificationCenter];
- [center addObserver: self
- selector: @selector(observeWorkspaceStuff:)
- name: nil
- object: nil];
- // 最后是分布式通知中心。
- // 这是一个全局(整个计算机)的通知中心,
- // 你可以观察到不同程序获得焦点以及声音屏幕亮度改变等事件
- center = [NSDistributedNotificationCenter
- notificationCenterForType: NSLocalNotificationCenterType];
- [center addObserver: self
- selector: @selector(observeDistributedStuff:)
- name: nil
- object: nil];
- g_spying = YES;
- }
- }
- + (void) stopSpying
- {
- if (!g_spying) {
- NSNotificationCenter *center;
- center = [NSNotificationCenter defaultCenter];
- [center removeObserver: self];
- center = [[NSWorkspace sharedWorkspace] notificationCenter];
- [center removeObserver: self];
- center = [NSDistributedNotificationCenter
- notificationCenterForType: NSLocalNotificationCenterType];
- [center removeObserver: self];
- g_spying = NO;
- }
- }
- + (void) observeDefaultCenterStuff: (NSNotification *) notification
- {
- NSLog(@"default: %@", [notification name]);
- }
- + (void) observeDistributedStuff: (NSNotification *) notification
- {
- NSLog(@"distributed: %@", [notification name]);
- }
- + (void) observeWorkspaceStuff: (NSNotification *) notification
- {
- NSLog(@"workspace: %@", [notification name]);
- }
- @end
转载于:https://blog.51cto.com/bj007/417944