捷径系列:Notification
捷径系列: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
捷径系列:Notification相关推荐
- ORACLE HANDBOOK系列之十四:变化通知(Change Notification)
在App开发的过程中,有些数据访问频率很高但是数据变化不大,我们一般会让它驻留内存以提高访问性能,但是此种机制存在一个问题,那就是如何监测数据的变化,Oracle 10g中引入的 Change Not ...
- PWA(Progressive Web App)入门系列:Notification
前言 在很多场景下,需要一种通知的交互方式来提醒用户,传统方式下可以在页面实现一个 Dialog,或通过修改网页的 title 来实现消息的通知.然而传统的实现存在着一定的不足,在网页最小化的情况下, ...
- iOS监听模式系列之本地通知Notification
本地通知 本地通知是由本地应用触发的,它是基于时间行为的一种通知形式,例如闹钟定时.待办事项提醒,又或者一个应用在一段时候后不使用通常会提示用户使用此应用等都是本地通知.创建一个本地通知通常分为以下几 ...
- iOS开发系列--通知与消息机制
http://www.cocoachina.com/ios/20150318/11364.html 概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户感兴趣 ...
- typescript 叹号_TypeScript系列(五)最佳实践
前言 在进入主题之前,我们先来简单回顾一下前四篇文章想要表达的主题: 当Redux遇到TypeScript:这篇文章从redux的action出发,介绍了as和可判别联合类型(Discriminate ...
- 坑系列 —— 缓存+哈希=高并发?
2019独角兽企业重金招聘Python工程师标准>>> 今天继续坑系列,高可用已经讲过了,当前互联网时代,怎么少的了高并发呢?高并发和高可用一样, 已经变成各个系统的标配了,如果你的 ...
- SAP QM 稳定性研究功能研习系列1 - 稳定性研究总流程
SAP QM 稳定性研究功能研习系列1 - 稳定性研究总流程 如下图是SAP系统里标准的Stability Study Processing. 2,QM01创建一个QS类型(StabilStudy w ...
- SAP PM 初级系列21 - 一个技术关闭的维修工单不能再被修改了!
SAP PM 初级系列21 - 一个技术关闭的维修工单不能再被修改了! 维修工单号:102333362已经被Techical completion了, 试图执行IW32去修改它, 系统提示说:Noti ...
- SAP PM 初级系列15 - IW33事务代码界面里查看维修工单对应的维修通知单的几个方法
SAP PM 初级系列15 - IW33事务代码界面里查看维修工单对应的维修通知单的几个方法 IW33进入维修工单显示界面,有好几种方法能查看该维修工单对应的前端维修通知单的信息. 1,在Header ...
最新文章
- ExtJS中layout的12种布局风格
- 连接moogDB数据库
- MCU,RTOS,物联网之间的关系。
- 叉叉框架_叉/连接框架
- 基于Linux的Socket编程之TCP全双工Server-Client聊天程序
- Linux操作系统load average过高,kworker占用较多cpu
- 服务器绑定域名可修改吗,云服务器可以绑定域名吗
- Sql Server发布订阅如何添加新表如何不初始化整个快照
- windows下python安装gmpy2_安装Python模块gmpy2中的问题解决
- Android下拉刷新上拉更多瀑布流(附源码)
- openHEVC在Windows下构建vs32/64位工程(vs2013)
- NMath应用教程:如何通过代码访问底层数据和应用函数
- Unix环境高级编程(十)信号续
- Ubuntu 配置VNC时,出现“perl: warning: Falling back to the standard locale (C).”等提示错误解决方法...
- 实心和空心哪个抗弯能力强_为什么轮胎不设计成实心的?不怕爆胎 480阅读
- 永中office属于职称计算机吗,职称计算机考核永中OFFICE辅导之电子表格.doc
- 开心盒子助手3.0版|开心盒子助手工具3.0电脑版
- 多目标优化——帕累托最优
- NC65单据节点开发
- ie登录显示登录到ftp服务器,ie浏览器登陆ftp服务器