如何发布和接收 NSNotifications(目标 C) |通知(在 Swift 中)? [英] How to post and receive an NSNotifications (Objective C) | Notifications (in Swift)?

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

问题描述

是否有一种易于理解的模式如何发送 NSNotification(目标 C)|通知(在 Swift 中)以及如何接收通知?代码片段?文档写了大约 150 页关于该主题的内容.想看一个简单的例子.

Is there an easy-to-grock pattern how to send a NSNotification (Objective C) | Notification (in Swift) 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];

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

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