iOS中通知实现观察者模式

KVO与通知都可实现观察者模式。

一、通知简介

NSNotificationCenter是一个消息通知机制,类似广播。观察者只需要向消息中心注册消息,当有地方发出这个消息的时候,通知中心会发送给注册这个消息的对象。消息发送者和消息接受者两者可以互相一无所知,完全解耦。
观察者向消息中心注册以后,在不需要接受消息时需要向消息中心注销,属于典型的观察者模式。

消息通知中重要的两个类:
(1) NSNotificationCenter: 实现NSNotificationCenter的原理是一个观察者模式,获得NSNotificationCenter的方法只有一种,那就是[NSNotificationCenter defaultCenter] ,通过调用静态方法defaultCenter就可以获取这个通知中心的对象了。NSNotificationCenter是一个单例模式,而这个通知中心的对象会一直存在于一个应用的生命周期。
(2) NSNotification: 这是消息携带的载体,通过它,可以把消息内容传递给观察者。

二、通知的使用

1、在当前控制器发送通知接收通知

#import "ViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//1.注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(firstNotificationFunc:) name:@"First" object:nil];

}
//2.通知回调方法
- (void)firstNotificationFunc:(NSNotification *)notification{
    NSString *name = [notification name];
    NSLog(@"打印%@",name);
}

//3.销毁
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
//4.发送通知
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"First" object:self];
}
@end

注意:只有注册通知之后才可以接收发送的通知。
通知可传递参数,userInfo、object。
2、在push的下一界面发送通知,在上一控制器接收通知

//第一个控制器
#import "ViewController.h"
#import "NextViewController.h"
@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"第一个控制器";
//1.注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(firstNotificationFunc:) name:@"First" object:nil];
}
//2.通知回调方法
- (void)firstNotificationFunc:(NSNotification *)notification{
    NSString *name = [notification name];
    NSLog(@"打印%@%@",self.title,name);
}
//3.销毁
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    NSLog(@"销毁");
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    //跳转
    NextViewController *vc = [[NextViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
@end

**********************************************************

//第二个控制器
#import "NextViewController.h"

@interface NextViewController ()

@end

@implementation NextViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"第二个控制器";
    self.view.backgroundColor = [UIColor whiteColor];

}
//发送通知
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{   
    [[NSNotificationCenter defaultCenter] postNotificationName:@"First" object:nil];
    
}
@end

//也可以根据通知名移除注册的通知

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

三、通知的优缺点

优点:

  1. 不需要编写多少代码,实现比较简单。
  2. 对于一个发出的通知,多个对象能够做出反应,即一对多的消息通知机制。
  3. 可以传递参数,userInfo, object。

缺点:

  1. 在编译器不会检查通知是否能够被观察者正确的处理;
  2. 注册的观察者,一定要移除。
  3. 只有注册了通知,才能接收通知消息。注册通知对象要在发送通知的前面才可接收到通知。
  4. 通知发出后,controller不能从观察者获得任何的反馈信息。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户感兴趣的那么通过通知...
    莫离_焱阅读 11,612评论 1 8
  • 什么是观察者模式?我们先打个比方,这就像你订报纸。比如你想知道美国最近放生了些新闻,你可能会订阅一份美国周刊,然后...
    泥孩儿0107阅读 4,023评论 0 0
  • iOS 提供了一种 “同步的” 消息通知机制NSNotificationCenter,观察者只要向消息中心注册, ...
    MasterChen阅读 6,673评论 4 16
  • NSNotificationCenter对象(通知中心)提供了在程序中广播消息的机制,它实质上就是一个通知分发表。...
    9de75b652cd9阅读 4,075评论 0 1
  • 转载自南峰子的技术博客 一个NSNotificationCenter对象(通知中心)提供了在程序中广播消息的机制,...
    我消失1314阅读 4,417评论 0 2

友情链接更多精彩内容