一 NSNotificationCenter 通知中心
每个应用程序都有一个默认通知中心NSNotificationCenter,负责协助不同对象之间的消息通信,任何一个对象都可以向通知中心发布通知,其监听对象 Observer 可用接受 发布者 对象的通知。NSNotificationCenter对象,采用单例设计模式,defaultCenter 方法就可用huohuo获取NSNotificationCenter对象。
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
二 NSNotification 手动创建通知
2.1 通知的3个属性
属性名字 | 说明 |
- (NSString *)name | 通知的名称 |
- (id)object; | 通知发布者 |
- (NSDictionary *)userInfo | 通知的其他信息 |
2.2 创建通知
//对象方法
+ (instancetype)notificationWithName:(NSString *)aName object:(id)anObject;
+ (instancetype)notificationWithName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;
//类方法
- (instancetype)initWithName:(NSString *)name object:(id)object userInfo:(NSDictionary *)userInfo;
注意:一般不用自己创建通知。而采用系统默认的通知中心 NSNotificationCenter。
三 发布通知
通知中心NSNotificationCenter 提供了相应的方法来帮助发布通知
3.1 发布通知
发布一个notification通知,可在notification对象中设置通知的名称、通知发布者、额外信息等
- (void)postNotification:(NSNotification *)notification;
发布一个名称为aName的通知,anObject为这个通知的发布者
- (void)postNotificationName:(NSString *)aName object:(id)anObject;
发布一个名称为aName的通知,anObject为这个通知的发布者,aUserInfotong通知的其他信息
- (void)postNotificationName:(NSString *)aName object:(id)anObject userInfo:(NSDictionary *)aUserInfo;
3.2 参数说明
四 注册通知
通知中心 NSNotificationCenter ,提供了方法来注册一个监听通知的监听器 Observer 的方法.
4.1 注册通知方法一
//
// observer:监听器,即谁要接收这个通知
// aSelector:收到通知后,回调监听器的这个方法,并且把通知对象当做参数传入
// aName:通知的名称。如果为nil,那么无论通知的名称是什么,监听器都能收到这个通知
// anObject:通知发布者。如果为anObject和aName都为nil,监听器都收到所有的通知
//
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
官方文档说明:
4.2 注册通知方法二
//
// name:通知的名称
// obj:通知发布者
// queue:决定了block在哪个操作队列中执行,如果传nil,默认在当前操作队列中同步执行
// block:收到对应的通知时,会回调这个block
//
- (id)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block;
//
// name:通知的名称
// obj:通知发布者
// queue:决定了block在哪个操作队列中执行,如果传nil,默认在当前操作队列中同步执行
// block:收到对应的通知时,会回调这个block
//
- (id)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification *note))block;
五 取消注册
通知中心不会保留监听器对象,在通知中心注册过的对象,必须在该对象释放前取消注册。否则,当相应的通知再次出现时,通知中心仍然会向该监听器发送消息。因为相应的监听器对象已经被释放了,所以可能会导致应用崩溃
5.1 取消注册监听器
- (void)removeObserver:(id)observer;
- (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;
5.2 监听器对象销毁之需要取消注册
如在监听器中加入下列代码
- (void)dealloc {
//[super dealloc]; 非ARC中需要调用此句
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
六 实例
新闻小实例
类:GMNews 新闻发布者
GMReader 读者
6.1 GMNews 类
==GMNews.h
#import <Foundation/Foundation.h>
@interface GMNews : NSObject
/**
* 发布者
*/
@property (nonatomic, copy) NSString *promulgator;
/**
* 新闻类别
*/
@property (nonatomic, copy) NSString *newsType;
/**
* 新闻标题
*/
@property (nonatomic, copy) NSString *title;
/**
* 新闻内容
*/
@property (nonatomic, copy) NSString *contents;
@end
===GMNews.m
#import "GMNews.h"
@implementation GMNews
@end
6.2 GMReader 类
===GMReader.h
#import <Foundation/Foundation.h>
#import "GMNews.h"
@interface GMReader : NSObject
/**
* 读者名字
*/
@property (nonatomic, copy) NSString *readerName;
@property (nonatomic, copy) NSString *readType;
/**
* 接收新闻
*
* @param news 新闻对象
*/
-(void)receiveNews:(GMNews *)news;
@end
===GMReader.m
#import "GMReader.h"
@implementation GMReader
/**
* 监听到消息后,掉用方法
*
* @param notification 通知
*/
-(void)receiveNews:(NSNotification *)notification
{
GMNews *news = notification.object;
NSLog(@"=======================");
NSLog(@"新闻发布者:%@",news.promulgator);
NSLog(@"新闻类型:%@",news.newsType);
NSLog(@"新闻标题:%@",news.title);
NSLog(@"新闻内容:%@",news.contents);
}
-(void)dealloc{
/**
* 当对象销毁时,需要从通知中心删除监听对象
*/
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
6.3 Main.m
//
// main.m
// 28Notification
//
// Created by 雷国敏 on 15-3-14.
// Copyright (c) 2015年 ___FULLUSERNAME___. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "GMNews.h"
#import "GMReader.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
/** 通知测试 NSNotification **/
NSString *newsType = @"IT";
//1.创建新闻对象
GMNews *sineNews = [[GMNews alloc]init];
sineNews.promulgator = @"新浪新闻";
sineNews.newsType = newsType;
sineNews.title = @"IT资信";
sineNews.contents = @"IT 程序员,就是任性!";
GMNews *netEaseNews = [[GMNews alloc]init];
netEaseNews.promulgator = @"网易新闻";
netEaseNews.newsType = newsType;
netEaseNews.title = @"IT消息";
netEaseNews.contents = @"人类如果没有IT,那会是什么样?";
//2.创建阅读对象
GMReader *readerLgm = [[GMReader alloc]init];
readerLgm.readerName = @"leigm";
//reader.readType = newsType;
//3.获取默认通知
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
//4.注册通知
/**
* 注册监听
*
* @param : addObserver: 监听者
* @param : selector: 收到通知调用相应方法
* @param : name: 监听通知的名称(如果为nil,则接受指定发布者 object 的所有信息)
* @param : object: 监听谁发布的通知(如果为nil,则接受所有发布者的所有信息)
*
*/
// 注册新浪新闻
[center addObserver:readerLgm selector:@selector(receiveNews:) name:nil object:nil];
// 注册网易新闻
// [center addObserver:readerLgm selector:@selector(receiveNews:) name:nil object:netEaseNews];
//5.发布通知
/**
* 发布通知
*
* @param : postNotificationName: 通知的名称
* @param : object: 通知的发布者
* @param : userInfo: 关于通知的信息(额外消息)
*/
// [center postNotificationName:newsType object:sineNews userInfo:nil];
// 发布新浪新闻
[center postNotificationName:newsType object:sineNews];
// 发布网易新闻
[center postNotificationName:newsType object:netEaseNews];
}
return 0;
}
6.4 输出
代码下载地址:http://pan.baidu.com/s/1bn3z0Ur