通知

作者: 和女神经常玩 | 来源:发表于2023-01-01 22:54 被阅读0次

接口部分

@interface NotificationCenter : NSObject

+ (void)postNotificationWithName:(NSString *)notificationName;

+ (void)postNotificationWithName:(NSString *)notificationName param:(NSDictionary * __nullable)param;

+ (id <NSObject>)addNotificationOnMainQueueWithName:(NSString *)notificationName block:(void (^)(NSDictionary * __nullable param))block;

+ (void)removeObserver:(id)observer;

+ (void)removeObserver:(id)observer name:(NSString *)name;

+ (void)addNotificationWithObserver:(id)observer selector:(SEL)aSelector name:(nullable NSNotificationName)aName;

@end

实现部分

@implementation NotificationCenter

+ (void)postNotificationWithName:(NSString *)notificationName
{
    [self postNotificationWithName:notificationName param:nil];
}
+ (void)postNotificationWithName:(NSString *)notificationName param:(NSDictionary *)param
{
    [[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:nil userInfo:param];
}

+ (id <NSObject>)addNotificationOnMainQueueWithName:(NSString *)notificationName block:(void (^)(NSDictionary * __nullable param))block
{
    return [[NSNotificationCenter defaultCenter] addObserverForName:notificationName object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        NSDictionary *userInfo = note.userInfo;
        block(userInfo);
    }];
}
+ (void)removeObserver:(id)observer
{
    if (observer) {
        [[NSNotificationCenter defaultCenter] removeObserver:observer];
    }
}
+ (void)removeObserver:(id)observer name:(NSString *)name
{
    if (observer) {
        [[NSNotificationCenter defaultCenter] removeObserver:observer name:name object:nil];
    }
}
+ (void)addNotificationWithObserver:(id)observer selector:(SEL)aSelector name:(nullable NSNotificationName)aName
{
    [[NSNotificationCenter defaultCenter] addObserver:observer selector:aSelector name:aName object:nil];
}

@end

相关文章

  • 通知!通知!

    我到现在已有三位粉丝,现在我要开始发福利啦! 我下一篇文章开始写小说啦!希望大家多多支持!谢谢!

  • 通知!通知!

    小仙近日找了一个编辑(啥也不是),有事请找她昂, @小编辑染染 她负责我的更新日常,而且此人闲的要死,如有评论,都...

  • 通知--通知

    因为事情开始多起来了,很难保证下午开始更了,所以决定了,之后每天早上更。 假期结束,很多事情需要去处理,现在不...

  • 通知-通知

    昨天下午,写了一篇文章,可有敏感词,没有成功通过审核,今天晚上,努力搞一篇出来,换另外一个故事,昨天的那个故事,我...

  • 通知! 通知 ! 通知书 !

  • 重要通知!重要通知!重要通知!

    非常感谢小伙伴的关注和支持,以后摄影相关内容将不再这里更新。 喜欢毒家摄影指南内容的小伙伴欢迎关注微信公众号:sm...

  • 推送通知-后台通知/静默通知

    后台远程推送iOS7开始允许应用收到通知后直接在后台(background)状态下运行一段代码,而无需用户点击,可...

  • 通知:本地通知和远程通知

    通知中心(NotificationCenter)和通知(UILocalNotification)是雷锋和雷峰塔的关...

  • Spring 返回通知 异常通知 环绕通知

    返回通知 在方法正常返回结果后的通知。返回通知可以访问到方法的返回值。 示例(附带返回结果的返回通知): 异常通知...

  • 通知与移除通知

    [[NSNotificationCenterdefaultCenter]postNotificationName:...

网友评论

      本文标题:通知

      本文链接:https://www.haomeiwen.com/subject/gyefcdtx.html