如何发布和接收NSNotifications(目标C)通知(在Swift中)? [英] How to post and receive an NSNotifications (Objective C) | Notifications (in Swift)?
本文介绍了如何发布和接收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屋!
查看全文