美文网首页iOS程序员iOS程序猿
设计模式--通知中心(广播设计模式)

设计模式--通知中心(广播设计模式)

作者: Cy_Star | 来源:发表于2017-10-20 15:18 被阅读51次

1、通知中心(NSNotificationCenter)

  通知中心 :任意一个发送者可以发送 消息/广播 给任意一个接收者(听众),
  这就是通知中心的一个主要作用
 (如:需要在多个页面/对象 传递参数,通知中心就是其中一种传递方式)

2、创建通知中心

//1、取得通知中心
NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];

3、发送消息(NSNotification)

 -(void)looper;
- (void) inform_center;


-(void)looper
{
   // 启动一个定时器 循环发送广播
   [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(inform_center) userInfo:nil repeats:YES];

}

- (void) inform_center
{
  //1、取得通知中心
  NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];

  //2、时间戳,做定时循环广播
  static int i;
  NSString * count = [NSString stringWithFormat:@"Audience %d",i++];
  NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:@"Inform_center", @"name", count, @"Value", nil];

  //消息内容
  //1、发送广播
  [nc postNotificationName:@"Inform" object:self userInfo:dic];
  /*
    可获取的方法
    name:(NString *) -- 通知中心消息的名字,带着参数object:
    object:(id) 消息的对象
    userInfo:(NSDictionary) 消息中可以带的参数
  */
}

4、接收消息(NSNotification)

 // 接收消息是一个回调机制,要注册接收哪个消息,
 如果这个消息得到了响应,在对象当中就能够回调接收消息的方法

-(void) Listen;

-(void) Listen
{

  //1、注册
  [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(recvIform:) name:@"Inform" object:nil];

  /* \ 只要是这个广播:(name:@"Inform"),addObserver: self 就调用
   selector: (recvIform:)这个方法,
   addObserver:self selector:@selector(recvIform:) == [self recvIform];
 
   object: 一般都设为nil,这样就等于不用指定的广播站就可以收听 “Iform” 这个频段的广播
    注意:name:@"Inform" 必须跟 [nc postNotificationName:@"Inform" object:self userInfo:dic]; 的 postNotificationName:@"Inform"保持一致,
    可以说是发送频道跟接收频道保持一致才能互通,听众才可以听到广播发出的通知。
  */

}

//2、接收广播数据
-(void)recvIform:(NSNotification *) nc{

  //nc : 具体的广播消息

  NSString * name = nc.name;

  NSLog(@"name is %@",name);
  NSLog(@"nc is %@",nc);
}

5、使用

// 创建广播站
Inform * inf = [[Inform alloc]init];
[inf looper]; // 调用循环的广播

//听众
Audience * aud = [[Audience alloc]init];
[aud Listen];//听广播

[[NSRunLoop currentRunLoop] run];
//
输出接收到的消息.png

相关文章

  • 设计模式--通知中心(广播设计模式)

    1、通知中心(NSNotificationCenter) 2、创建通知中心 3、发送消息(NSNotificati...

  • iOS分层架构设计

    大家都知道,在移动设计开发中有很多种模式,最常用的单例设计模式、MVC设计模式、工厂设计模式、KVO、通知、代理等...

  • 2019-01-27 面试题

    [TOC] 1 设计模式是什么? 你知道哪些设计模式,并简要叙述。 单例模式 通知模式 代理模式 工厂模式 2 多...

  • Swift-单例模式

    iOS开发中过程中最熟悉的设计模式应该是单例模式,例如NotificationCenter通知中心、Applica...

  • iOS ReactiveObjc框架解析

    简介 ReactiveObjc将原有的各种设计模式,包括代理、Target/Action、block、通知中心以及...

  • iOS中消息传递方式 - 07

    一.通知:在iOS中由通知中心进行消息接收和消息广播,是一种一对多的消息传递方式。 二.代理:是一种通用的设计模式...

  • iOS中消息传递方式

    通知:在iOS中由通知中心进行消息接收和消息广播,是一种一对多的消息传递方式。 代理:是一种通用的设计模式,iOS...

  • iOS-MVC

    有这么一个说法——设计模式。设计模式包括单例、代理、通知。 使用设计模式可以使项目的架构层次更加清晰,使项目模式化...

  • Spring Data Redis-keyspace notif

    观察者模式 这种创建一个消息中心, 所有消息发布者发布消息到中心, 中心把消息通知到各订阅者的设计模式在GUI程序...

  • 设计模式

    常用的设计模式有,单例设计模式、观察者设计模式、工厂设计模式、装饰设计模式、代理设计模式,模板设计模式等等。 单例...

网友评论

    本文标题:设计模式--通知中心(广播设计模式)

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