当前位置: 首页 > news >正文

cnzz站长统计怎么添加到博客网站一键生成原创动漫

cnzz站长统计怎么添加到博客网站,一键生成原创动漫,太原做网站哪里好,品牌建设部门工作职责与分工想要 Objective-C 版本的 LiveEventBus 效果(跨页面/跨模块通信,支持粘性和非粘性事件)。在 iOS 里对应的就是 NSNotificationCenter,但是它 默认不支持粘性事件,所以如果你想要“粘性”,需要自己封装一层。…

想要 Objective-C 版本的 LiveEventBus 效果(跨页面/跨模块通信,支持粘性和非粘性事件)。在 iOS 里对应的就是 NSNotificationCenter,但是它 默认不支持粘性事件,所以如果你想要“粘性”,需要自己封装一层。


1. 系统自带方式(非粘性事件)

这是最常见的写法(类似 Android 普通的 EventBus / LiveEventBus 的非粘性模式):

注册监听

// 在需要监听的类里(比如 viewDidLoad)
[[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(onUserLogin:)name:@"UserLoginNotification"object:nil];- (void)onUserLogin:(NSNotification *)notification {NSString *user = notification.userInfo[@"user"];NSLog(@"用户登录成功: %@", user);
}

发送通知

[[NSNotificationCenter defaultCenter] postNotificationName:@"UserLoginNotification"object:niluserInfo:@{@"user": @"Tom"}];

特点:

  • 简单,零依赖。

  • 不支持“粘性事件”,也就是说如果事件在订阅前就发了,新订阅者不会收到。


2. 封装一个带粘性事件的 EventBus

自己写一个单例,存储最后一次事件,当新订阅时立即触发一次:

@interface EventBus : NSObject
@property (nonatomic, strong) NSMutableDictionary<NSString *, NSNotification *> *stickyEvents;
+ (instancetype)shared;
- (void)post:(NSString *)name userInfo:(NSDictionary *)userInfo sticky:(BOOL)sticky;
- (void)observe:(NSString *)name observer:(id)observer selector:(SEL)selector sticky:(BOOL)sticky;
@end@implementation EventBus+ (instancetype)shared {static EventBus *instance;static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{instance = [[EventBus alloc] init];instance.stickyEvents = [NSMutableDictionary dictionary];});return instance;
}- (void)post:(NSString *)name userInfo:(NSDictionary *)userInfo sticky:(BOOL)sticky {NSNotification *notification = [NSNotification notificationWithName:name object:nil userInfo:userInfo];if (sticky) {self.stickyEvents[name] = notification;}[[NSNotificationCenter defaultCenter] postNotification:notification];
}- (void)observe:(NSString *)name observer:(id)observer selector:(SEL)selector sticky:(BOOL)sticky {[[NSNotificationCenter defaultCenter] addObserver:observer selector:selector name:name object:nil];if (sticky) {NSNotification *last = self.stickyEvents[name];if (last) {// 手动立即分发上一次事件#pragma clang diagnostic push#pragma clang diagnostic ignored "-Warc-performSelector-leaks"[observer performSelector:selector withObject:last];#pragma clang diagnostic pop}}
}@end

使用示例

发布事件
[[EventBus shared] post:@"UserLoginNotification"userInfo:@{@"user": @"Tom"}sticky:YES];
订阅事件(新订阅者会马上收到粘性事件)
[[EventBus shared] observe:@"UserLoginNotification"observer:selfselector:@selector(onUserLogin:)sticky:YES];- (void)onUserLogin:(NSNotification *)notification {NSLog(@"粘性事件 - 登录用户: %@", notification.userInfo[@"user"]);
}

总结:

  • 如果只要普通通知 → 用 NSNotificationCenter

  • 如果要 LiveEventBus 粘性效果 → 用上面封装的 EventBus 单例。

http://www.dtcms.com/a/445184.html

相关文章:

  • 彩票网站维护需要几天提升学历有哪些渠道
  • 网站读取速度慢wordpress 微网站
  • 做源码演示的网站株洲制作网站
  • 网站用什么语言做中山营销型网站设计
  • 佛山企业网站建设流程服务平台app
  • 重庆网站建设seo公司建设人才库网站
  • 下载学校网站模板聊城房地产网站建设
  • 学校网站建设阶段性目标h5页面制作工具哪个好
  • 网站建设外包宜宾市做网站多少钱
  • 北京企业建站服务中企不良广告入口
  • 北京专业做网站推广苏州建设交易中心网站
  • 做旅游网站的原因公司做网站的费用怎么做账
  • 做电影网站资源哪里来的台州做网站联系方式
  • 做片头片尾比较好的网站北京建筑设计院待遇好吗
  • 如何建设百度网站个人理财的网站开发
  • 上海知名的网站建设十大购物网站
  • 杭州网站网络 科技公司百度pc端入口
  • 微网站建设套餐网站查询功能 技术支持 中企动力
  • 林州网站建设服务数据平台
  • 昆明经济技术开发区官方门户网站WordPress商品浏览权限
  • 网站排行榜福州网络推广
  • 网站布局建议网站整合营销等服务
  • 源码下载网站源码wordpress设置全屏背景图片
  • 写网站软件哪个网站可以免费下载电视剧看
  • 网站排名如何稳定河南新闻头条最新消息
  • 网站开发多久外贸网站建设软件有哪些
  • 制作app网站sem优化托管
  • 大学生个人网站制作微信微网站开发教程
  • 唐山网站建设推广做门户网站用什么模板好
  • 导购网站怎么建立三亚市住房与城乡建设局网站