天天看点

NSNotification 通知中心

虽然通知中心用的很熟悉了 但是还是做个备忘笔记吧!好记性不如烂笔头!部分来自网络

项目中通知最好不要用的太多!最重要的是注册的通知一定要及时的删除!不然后患无穷呀!

NSNotification 介绍

1.     通知中心概述

通知中心实际上是在程序内部提供了消息广播的一种机制。通知中心不能在进程间进行通信。实际上就是一个二传手,把接收到的消息,根据内部的一个消息转发表,来将消息转发给需要的对象。通知中心是基于观察者模式的,它允许注册、删除观察者。

一个NSNotificationCenter可以有许多的通知消息NSNotification,对于每一个NSNotification可以有很多的观察者Observer来接收通知。

2.     通知中心两个重要的类

NSNotificationCenter:这是iOS中通知中心的灵魂,由该类实现了观察者模式,并给开发者提供了诸如注册、删除观察者的接口,我们可以通过一个单例来获得它的实例(注,一个程序内部只有一个NSNotificationCenter实例对象)。

NSNotification:这是消息携带的载体,通过它,可以把消息内容传递给观察者。其中:name对应消息名称标示。object一般是发送者本身、dictionary则是传递的消息内容。

3.     通知中心如何使用

通过下图,我们可以看出,通知中心的使用可以分为4个步骤。

这里需要额外提一点的是:发送消息不仅仅可以有用户发起,也可以是系统发起。

当我们注册了某个消息的观察者后,如果有了对应的消息,则观察者会收到相应的消息,并展开处理。这里需要注意的是,当使用完消息之后,不想在接收到消息,则需要把观察者移除,否则会出现错误。

注册通知:即要在什么地方接受消息

[[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector (mytest:) name:@" postData " object:nil];

参数介绍:

addObserver:观察者,即在什么地方接收通知;

selector:收到通知后调用何种方法,即回调函数;

name:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。

发送通知:调用观察者处的方法。

[[NSNotificationCenter defaultCenter] postNotificationName:@" postData " object:searchFriendArray];

上面的介绍来自互联网!

下面让我们进行实战吧

我先在ViewController.m  里的   viewDidLoad里面写了两个通知!

5秒后发送通知 一个 8 秒后在发送个通知 但是观察者的名字都是go

但是观察者观察的对象是不用的,他们分别是 array,和 string, 我在ObserverViewController.m 这个类里只 观察了array ,所以string 的对象是没有被观察者发现的,如何想要观察者 都要监听到观察者名字为go 发送的消息!那么通常会把object 设置为空!

  [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(setValueAction:)name:@"go"object:nil];

- (void)viewDidLoad

{

    [superviewDidLoad];

    [NSTimer scheduledTimerWithTimeInterval:5target:selfselector:@selector(sendNotificationAciton:)userInfo:nilrepeats:NO];

        //这个通知对方是收不到的因为观察者object我观察了发送的对象!

    [NSTimer scheduledTimerWithTimeInterval:8target:selfselector:@selector(sendNotificationStringAciton:)userInfo:nilrepeats:NO];

}

- (void)sendNotificationAciton:(id)sender

{

    array = [[NSArrayalloc]initWithObjects:@"1",@"2",nil];

    //发送通知

   [[NSNotificationCenter defaultCenter]postNotificationName:@"go"object:array];

}

-(void)sendNotificationStringAciton:(id)sender

{

    NSString *string = @"hello";

    [[NSNotificationCenter defaultCenter]postNotificationName:@"go" object:self userInfo:@{@"key":string}];

  //  [[NSNotificationCenterdefaultCenter]postNotificationName:@"go"object:string];

    [NSNotificationCenter defaultCenter]postNotificationName:<#(nonnull NSNotificationName)#> object:<#(nullable id)#> userInfo:<#(nullable NSDictionary *)#>];

}

ObserverViewController.m 类里面的函数

- (void)viewDidLoad

{

    ViewController *objectview = [[ViewControlleralloc]init];

    //观察者

 [[NSNotificationCenter defaultCenter]addObserver:selfselector:@selector(setValueAction:)name:@"goobject:objectview.array]

        [superviewDidLoad];

}

-(void)setValueAction:(NSNotification *)notification

{

    id array_id = [notification object];

    NSArray *array = array_id;

    _value.text = [arrayobjectAtIndex:1];

    [[NSNotificationCenterdefaultCenter]removeObserver:selfname:@"go"object:nil];

}

demo 下载地址http://download.csdn.net/detail/lengshengren/6504833