如何发布和接收通知? [英] How to post and receive an notification?

查看:141
本文介绍了如何发布和接收通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个易于格斗的模式如何发送通知以及如何接收通知?代码段?该文档在该主题上写了150页。想看一个简单的例子。

Is there an easy-to-grock pattern how to send a notification and how to receive one? Code snippet? The docs write like 150 pages on the topic. Would like to see a quick example.

推荐答案

发送通知:

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyCacheUpdatedNotification" object:self];

收到:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cacheUpdated:) name:@"MyCacheUpdatedNotification" object:nil];

行动:

- (void)cacheUpdated:(NSNotification *)notification {
[self load];
}

并处置它:

[[NSNotificationCenter defaultCenter] removeObserver:self];

这篇关于如何发布和接收通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆