OC学习篇之---通知(NSNotificationCenter) 2014-12-14 10:20 2430人阅读 评论(0) 收藏

在前一篇文章中咱们介绍了OC中很经常使用的两个技术:KVC和KVO: http://blog.csdn.net/jiangwei0910410003/article/details/41912937,今天咱们来看一下OC中另外的一个经常使用技术:通知(Nofitication)java

其实这里的通知和以前说到的KVO功能很想,也是用于监听操做的,可是和KVO不一样的是,KVO只用来监听属性值的变化,这个发送监听的操做是系统控制的,咱们控制不了,咱们只能控制监听操做,相似于Android中系统发送的广播,咱们只能接受。可是通知就不同了,他的监听发送也是又咱们本身控制,咱们能够在任何地方任什么时候机发送一个通知,相似于Android中开发者本身发送的广播。从这一点看来,通知的使用场景更为普遍了。app


下面就来看一下例子:post

仍是护士和小孩的那个例子spa

Children.h.net

//
//  Children.h
//  44_KVO
//
//  Created by jiangwei on 14-10-16.
//  Copyright (c) 2014年 jiangwei. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Children : NSObject

@property NSInteger *hapyValue;


@end
定义了一个属性:hapyValue


Children.mcode

//
//  Children.m
//  44_KVO
//
//  Created by jiangwei on 14-10-16.
//  Copyright (c) 2014年 jiangwei. All rights reserved.
//

#import "Children.h"

@implementation Children

- (id) init{
    self = [super init];
    if(self != nil){
        //启动定时器
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
        self.hapyValue= 100;
    }
    return self;
}

- (void) timerAction:(NSTimer *) timer{
    //使用set方法修改属性值,才能触发KVO
    _hapyValue--;
    NSLog(@"%@",_hapyValue);
    if(_hapyValue <80){
        //发送通知
        //这里和KVO的区别,咱们能够手动的发送通知
        //注意通知的名称,传递的参数必须和定义通知的地方的参数值要一致
        //将Children对象传递过去
        [[NSNotificationCenter defaultCenter] postNotificationName:@"happyValueNotification" object:self];
    }

}


@end
定义了一个定时器,可是咱们看到这里的timerAction方法中就开始发送一个通知了:

//发送通知
//这里和KVO的区别,咱们能够手动的发送通知
//注意通知的名称,传递的参数必须和定义通知的地方的参数值要一致
//将Children对象传递过去
[[NSNotificationCenter defaultCenter] postNotificationName:@"happyValueNotification" object:self];
咱们在属性值发生变化的地方发送一个通知:NSNotificationCenter

第一个参数:通知的名称,这个名称必须和后面接受通知的名称一致server

第二个参数:能够传递的一个参数对象对象


Nure.hblog

//
//  Nure.h
//  44_KVO
//
//  Created by jiangwei on 14-10-16.
//  Copyright (c) 2014年 jiangwei. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Nure : NSObject

@end

Nure.m

//
//  Nure.m
//  44_KVO
//
//  Created by jiangwei on 14-10-16.
//  Copyright (c) 2014年 jiangwei. All rights reserved.
//

#import "Nure.h"
#import "Children.h"

@implementation Nure

- (id) init{
    self = [super init];
    if(self != nil){
        
        //监听一个通知,当收到通知时,调用notificationAction方法
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction) name:@"happyValueNotification" object:nil];
        
    }
    return self;
}


- (void) notificationAction:(NSNotification *)notification{
    //这里咱们拿到Children对象进行操做
    Children *children = notification.object;
    NSLog(@"触发通知");
}

- (void)dealloc{
    //移除指定的通知,否则会形成内存泄露
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"happyValueNotification" object:nil];
    
    //Children对象能够添加多个通知
    //下面的方法是能够移除Children中全部通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
在Nure类中咱们开始接受通知了:

//监听一个通知,当收到通知时,调用notificationAction方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction) name:@"happyValueNotification" object:nil];
使用addObserver方法来监听通知

第一个参数:监听者对象ip

第二个参数:监听处理逻辑的方法

第三个参数:通知的名称

第四个参数:通知发送的时候传递过来的参数对象


一、处理通知的方法

- (void) notificationAction:(NSNotification *)notification{
    //这里咱们拿到Children对象进行操做
    Children *children = notification.object;
    NSLog(@"触发通知");
}
这里会传递一个NSNotification对象,经过object属性能够获取到监听对象了,由于咱们在发送通知的时候传递过来的这个对象。那么这里咱们就能够获取监听对象的属性值了,可是这里咱们若是想知道属性值变化前和变化后的值,咱们能够在Children类中在定义一个属性专门用来记录旧的属性值,这样就能够了。


二、销毁方法

- (void)dealloc{
    //移除指定的通知,否则会形成内存泄露
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"happyValueNotification" object:nil];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

在销毁的方法中,咱们能够须要移除监听者,传递过去通知名

可是这里咱们会注意到,还有一个方法:removeObserver方法,是用来移除全部监听者的,由于可能有多个监听者。


总结

OC中KVO操做和通知都是很重要的一个操做,他们的原理是基于观察者模式的,可是KVO操做没有通知灵活。可是KVO也有本身的优势,好比能够记录新旧值,这个通知就比较麻烦点了,因此咱们在使用的时候视状况而定,通常监听属性值变化的咱们仍是使用KVO.

相关文章
相关标签/搜索