iOS学习笔记13 -- NSNotificationCenter

一、NSNotificationCenter 相比于Delegate,可以实现更大跨度的通信机制。可以在两个无引用关系的对象之间进行通信。

二、NSNotificationCenter的通信原理使用了观察者模式:

1、NSNotificationCenter注册观察者对某个事件(以字符串命名)感兴趣,及该事件触发时该执行的Selector或Block
2、NSNotificationCenter在某个时机激发事件(以字符串命名)
3、观察者在收到感兴趣的事件时,执行相应的Selector或Block

三、在这个类NSNotificationCenter中,我们可以看到以下方法:

+ (NSNotificationCenter *)defaultCenter;

- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSString *)aName object:(nullable id)anObject;

- (void)postNotification:(NSNotification *)notification;
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject;
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject userInfo:(nullable NSDictionary *)aUserInfo;

- (void)removeObserver:(id)observer;
- (void)removeObserver:(id)observer name:(nullable NSString *)aName object:(nullable id)anObject;

- (id <NSObject>)addObserverForName:(nullable NSString *)name object:(nullable id)obj queue:(nullable NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block NS_AVAILABLE(10_6, 4_0);

我们一个一个来。

1、首先:

+ (NSNotificationCenter *)defaultCenter;

没有错,这个方法用于返回一个NSNotificationCenter对象,例如下面的使用:

[[NSNotificationCenter defaultCenter]postNotificationName:@"NETWORK_STATE_CONNECTED" object:self userInfo:nil];

2、接着:

//增加观察者
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(nullable NSString *)aName object:(nullable id)anObject;

参数说明:
observer: 添加观察的对象,例如在某个类中调用,则写为self
aSelector: 收到通知时,执行的Selector
aName: 通知的名称,用于区分不同的通知
anObject: 对哪个发送者对象发出的事件作出响应,nil 时表示接受所有发送者的事件。

//移除观察者
- (void)removeObserver:(id)observer name:(nullable NSString *)aName object:(nullable id)anObject;
//注册通知
- (void)postNotification:(NSNotification *)notification;
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject;
- (void)postNotificationName:(NSString *)aName object:(nullable id)anObject userInfo:(nullable NSDictionary *)aUserInfo;

参数说明:
aName: 通知的名称(即通知的标识)
anObject: 通知的注册对象
aUserInfo:通知传递的对象

3、带block的添加观察者的方法:

- (id <NSObject>)addObserverForName:(nullable NSString *)name object:(nullable id)obj queue:(nullable NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block NS_AVAILABLE(10_6, 4_0);

例如:

self.observer = [[NSNotificationCenter defaultCenter]addObserverForName:@"NOTIFICATION_NAME2" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {

        NSDictionary *dic = note.userInfo;

        NSString *string = [dic objectForKey:@"key"];

        NSLog(@"通知已接收,传递的对象2是:%@",string);

    }];

移除方式也是有所不同:

if (self.observer) {

    [[NSNotificationCenter defaultCenter]removeObserver:self.observer];

    self.observer = nil;

    }

例子:https://git.oschina.net/vampireLocker/NSNotificationCenterDemo.git

小总结: NSNotificationCenter的使用,无非就是注册通知、添加观察者、观察到通知后做出响应、在适当的时候移除通知。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值