iOS NSNotification传递带参数的通知

普通的通知使用
注册观察者

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

发送通知

[[NSNotificationCenter defaultCenter] 
postNotificationName:@"ThisIsANoticafication" object:nil];

传递带参数的通知
在发送通知时设置object参数

[[NSNotificationCenter defaultCenter] 
postNotificationName:@"ThisIsANoticafication" object:@{@"parameter1":@"1",@"parameter2":@"2"}];

这里传入了一个字典,那么如何在接收通知的时候得到这个字典呢

[[NSNotificationCenter defaultCenter] 
addObserver:self selector:@selector(getNotificationAction:) name:@"ThisIsANoticafication" object:nil];
- (void)getNotificationAction:(NSNotification *)notification{
    NSDictionary * infoDic = [notification object];
    // 这样就得到了我们在发送通知时候传入的字典了
}

当然传入的参数可以是其他类型。

posted @   brave-sailor  阅读(1170)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2018-07-15 Android-低功耗蓝牙(BLE)-客户端(主机/中心设备)和服务端(从机/外围设备)
2018-07-15 Android BLE 总结-源码篇(BluetoothLeAdvertiser)
2018-07-15 安卓低功耗蓝牙——手机作为外围设备
点击右上角即可分享
微信分享提示