NSNotificationCenter通知

2023-12-27 07:20

本文主要是介绍NSNotificationCenter通知,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

使用观察者模式来实现的用于跨层传递消息的机制

参考文章

ios消息机制(NSNotification 和 NSNotificationCenter)
透彻理解 NSNotificationCenter 通知(含实现代码) - 掘金

NSNotificationCenter

@property (class, readonly, strong) NSNotificationCenter *defaultCenter;

该属性是获取 NSNotificationCenter 唯一单例,它就是一个消息分发中心,通过使用这个唯一的实例我们进行添加通知、发送通知和移除通知

使用方法

添加通知

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

Observer为响应者,selector为一个响应通知的方法,name是一个标识,通知中心主要是通过它来实现消息的精确分发。

registerForNotifications:注册通知在viewdidload里面调用

发送通知

[[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:nil userInfo:nil];//使用NSNotification
NSNotification *notification = [[NSNotification alloc] initWithName:@"test0" object:_obj2 userInfo:@{@"key":@"_obj2"}];
[[NSNotificationCenter defaultCenter] postNotification:notification];

发送通知和添加通知对应,需要name、object参数,这里多了一个userInfo,该参数可以把你需要携带的数据发送给该通知的响应者。

移除通知

//移除该响应者的全部通知
[[NSNotificationCenter defaultCenter]  removeObserver:self];//移除该响应者 name == @"test" 的全部通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"test" object:nil];

unregisterFromNotifications:移除通知在dealloc里面调用

NSNotificationCenter声明类

@interface NSNotificationCenter : NSObject
@property (class, readonly, strong) NSNotificationCenter *defaultCenter;- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSNotificationName)aName object:(nullable id)anObject;
- (void)removeObserver:(id)observer;
- (void)removeObserver:(id)observer name:(nullable NSNotificationName)aName object:(nullable id)anObject;
@end

这篇关于NSNotificationCenter通知的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/542310

相关文章

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

一文详解Java Condition的await和signal等待通知机制

《一文详解JavaCondition的await和signal等待通知机制》这篇文章主要为大家详细介绍了JavaCondition的await和signal等待通知机制的相关知识,文中的示例代码讲... 目录1. Condition的核心方法2. 使用场景与优势3. 使用流程与规范基本模板生产者-消费者示例

AOP之执行前通知@Before

Spring AOP之执行前通知@Before 此文章说一下执行前通知,即注解@Before。 作用 多用于日志记录、权限校验、初始化资源等。 触发时间 目标函数执行触发。 定义 public class AopBeforeAspect {@Before("execution(public * com.example.demo.service.impl.AccountServiceI

安卓8.0通知栏适配

一、谷歌为什么在安卓8.0对通知栏进行修改         系统通知栏作为除桌面启动图标之外的唯一入口,对app的日活起到非常重要的作用,因此各个app开发商都希望能抢占用户的通知栏。随着移动端的普及和安卓手机存储容量的扩大,用户手机能安装的app数量越来越多,一台手机安装几十到上百个app已经不足为奇,各个app都极尽可能的抢占通知栏,而安卓系统本身又没有一种规范来约束app开发商的这种行为,

ubuntu —— 命令行执行完毕通知(undistract-me)

当我们在命令行下:   (1)git clone 一个项目时(2)sudo apt-get install 时(3)sudo make 时。。。 往往要经历比较长的时间等待,一方面我们不能盲等,时间白白流失,我们需要转而去做其他事情,另一方面,我们有不确定方才的任务是否执行完毕,我们会时不时的去查看下该命令是否执行完毕了(类似计算机中的轮循机制),这样效率比较低,饱受 distract(分心

Notification PendingIntent失效,每个通知都响应第一个PendingIntent

1、bug描述 可生成多个通知,但通知生成后,点击均响应第一个通知对应的PendingIntent。 但之前是可以正常使用的。 2、可能原因 期间,对AndroidStudio进行升级到3.2.1,buildToolsVersion更新到28.0.3。 并没有其他的额外涉及通知的操作,所以可能的原因是build更新,使得PendingIntent.getActivity的第2个和第4个参数重新起作

app版本更新,通知形式显示安装包下载进度

也是公司的项目需要,就稍微研究了下,参考网上一些不错的思路,但其适用版本都比较早,所以通知做了适配了Android 8.0,及权限问题等问题。 原理;下载apk过程中,发起一个通知,并不断发起最新进度的相同ID的通知,覆盖上一个通知,达到显示当前下载进度的效果。 demo已上传:https://download.csdn.net/download/u013370255/10603681 下面

通知Notification(可展开的大布局)使用,适配android8.0

补充修正: 2018-11-07 问题:Notification PendingIntent失效,每个通知都响应第一个PendingIntent https://blog.csdn.net/u013370255/article/details/83791750 2018-08-16 问题:app版本更新,通知形式显示安装包下载进度 https://blog.csdn.net/u01337025

NSNotificationCenter用法总结

NSNotificationCenter未必一定要建在消息接收者的类中。可以放在别的类中,先实例化一下,然后把observer赋值为刚对象。 这里的observer相当于接受者(receiver),object相当于发送者(poster)。理解了这点就可以较灵活地使用通知了。 iPhone软件开发的时候会遇到这种情况:打开APP后会在后台运行某个方法,例如下载文件,下载完成后可能需要调用某