NSNotification的使用

1、基本使用

#import "NormalViewController.h"

@interface NormalViewController ()

@end

@implementation NormalViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 注册通知中心
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(notificationEvent:)
                                                 name:NSStringFromClass([self class])
                                               object:nil];
    
    // 发送消息
    [[NSNotificationCenter defaultCenter] postNotificationName:NSStringFromClass([self class])
                                                        object:nil
                                                      userInfo:@{@"name": @"YouXianMing"}];
    
    // 发送消息
    [[NSNotificationCenter defaultCenter] postNotificationName:NSStringFromClass([self class])
                                                        object:nil
                                                      userInfo:@{@"age": @"18"}];
}

- (void)notificationEvent:(id)sender
{
    NSNotification *tmp = (NSNotification *)sender;
    NSLog(@"%@", tmp.userInfo);
}

- (void)dealloc
{
    // 移除通知中心
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:NSStringFromClass([self class])
                                                  object:nil];
}

@end

2、封装完善使用

1)增加NSObject的类别NSObject+NotificationCenter.h

//.h
#import <Foundation/Foundation.h>

#ifndef SELF_CLASS_NAME 
#define SELF_CLASS_NAME   [self className]
#endif

@interface NSObject (NotificationCenter)

@property (nonatomic, strong) NSString   *notificationName;  // 通知中心名字

// 发送通知消息
- (void)sendMessage:(NSDictionary *)obj toName:(NSString *)name;

// 注册通知中心
- (void)registerNotificationName:(NSString *)name selector:(SEL)selector;

// 移除通知中心
- (void)removeNotification:(NSString *)name;

// 发送消息的对象
- (id)messageObject;

// 消息名字
- (NSString *)messageName;

// 当前对象名字
+ (NSString *)ClassName;
- (NSString *)className;

@end

//.m
#import "NSObject+NotificationCenter.h"
#import <objc/runtime.h>

@implementation NSObject (NotificationCenter)

/* ================== 扩展了一个属性 ================== */
static char notificationNameFlag;
- (void)setNotificationName:(NSString *)notificationName
{
    objc_setAssociatedObject(self, ¬ificationNameFlag,
                             nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    objc_setAssociatedObject(self, ¬ificationNameFlag,
                             notificationName,
                             OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSString *)notificationName
{
    return objc_getAssociatedObject(self, ¬ificationNameFlag);
}
/* ================== 扩展了一个属性 ================== */


- (void)sendMessage:(NSDictionary *)obj toName:(NSString *)name
{
    // 发送消息
    [[NSNotificationCenter defaultCenter] postNotificationName:name
                                                        object:nil
                                                      userInfo:obj];
}

- (void)registerNotificationName:(NSString *)name selector:(SEL)selector
{
    // 如果没有提供名字,则用这个类的名字来注册
    if (name == nil)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:selector
                                                     name:NSStringFromClass([self class])
                                                   object:nil];
    }
    else
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:selector
                                                     name:name
                                                   object:nil];
    }
}

- (void)removeNotification:(NSString *)name
{
    // 如果没有提供名字,则用这个类的名字来移除(注意哦,此处需要与注册处的名字一致!)
    if (name == nil)
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:NSStringFromClass([self class])
                                                      object:nil];
    }
    else
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self
                                                        name:name
                                                      object:nil];
    }
}

+ (NSString *)ClassName
{
    // 返回类名
    return NSStringFromClass(self);
}

- (NSString *)className
{
    // 返回类名
    return NSStringFromClass([self class]);
}

- (id)messageObject
{
    // 消息实体内容
    if ([self isKindOfClass:[NSNotification class]])
    {
        NSNotification *tmp = (NSNotification *)self;
        return tmp.userInfo;
    }
    else
    {
        return nil;
    }
}

- (NSString *)messageName
{
    // 注册消息者的名字
    if ([self isKindOfClass:[NSNotification class]])
    {
        NSNotification *tmp = (NSNotification *)self;
        return tmp.name;
    }
    else
    {
        return nil;
    }
}

@end
使用代码

#import "RootViewController.h"
#import "NSObject+NotificationCenter.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 注册通知中心
    [self registerNotificationName:[self className]
                          selector:@selector(notificationEvent:)];
    
    // 对象A发送通知
    [@"A" sendMessage:@{@"name": @"YouXianMing"}
               toName:[self className]];
    
    // 对象B发送通知
    [@"B" sendMessage:@{@"age": @"18"}
               toName:[self className]];
}

- (void)notificationEvent:(id)sender
{
    // 获取到message
    id object = [sender messageObject];
    
    // 打印message
    NSLog(@"%@", object);
}

- (void)dealloc
{
    // 移除注册的通知中心
    [self removeNotification:[self className]];
}

@end

扩展属性的使用

1、新建一个Student类,继承自NSObject

//.h
#import <Foundation/Foundation.h>

@interface Student : NSObject

@property (nonatomic, strong) NSString *name;

@end

//.m
#import "Student.h"
#import "NSObject+NotificationCenter.h"

@implementation Student

@synthesize name = _name;
- (void)setName:(NSString *)name
{
    _name = name;
    
    if (self.notificationName)
    {
        [self sendMessage:@{@"data": name} toName:self.notificationName];
    }
}
- (NSString *)name
{
    return _name;
}

@end

使用:

#import "RootViewController.h"
#import "NSObject+NotificationCenter.h"
#import "Student.h"

@interface RootViewController ()

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 注册通知中心
    [self registerNotificationName:[self className]
                          selector:@selector(notificationEvent:)];
    

    Student *stu         = [Student new];
    stu.notificationName = [self className];
    stu.name             = @"YouXianMing";
}

- (void)notificationEvent:(id)sender
{
    // 获取到message
    id object = [sender messageObject];
    
    // 打印message
    NSLog(@"%@", object);
}

- (void)dealloc
{
    // 移除注册的通知中心
    [self removeNotification:[self className]];
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值