您的位置:首页 > 移动开发 > IOS开发

iOS 通知中心 NSNotificationCenter & NSNotification

2013-09-10 11:40 176 查看
转载
http://blog.csdn.net/crayondeng/article/details/9372079
通知中心是 Foundation 框架的一个子系统,它向应用程序中注册为某个事件观察者的所有对象广播消息(即通知)。(从编程角度而言,它是
NSNotificationCenter
类的实例)。该事件可以是发生在应用程序中的任何事情,例如进入后台状态,或者用户开始在文本栏中键入。通知是告诉观察者,事件已经发生或即将发生,因此让观察者有机会以合适的方式响应。通过通知中心来传播通知,是增加应用程序对象间合作和内聚力的一种途径。



任何对象都可以观察通知,但要做到这一点,该对象必须注册,以接收通知。在注册时,它必须指定选择器,以确定由通知传送所调用的方法;方法签名必须只有一个参数:通知对象。注册后,观察者也可以指定发布对象。

(以上是官方文档中的解释)

------------------------------------------华丽的分割线----------------------------------------------------------

通知中心包括两个重要的类:

(1)NSNotificationCenter:
实现NSNotificationCenter的原理是一个观察者模式,获得NSNotificationCenter的方法只有一种,那就是[NSNotificationCenter
defaultCenter] ,通过调用静态方法defaultCenter就可以获取这个通知中心的对象了,而NSNotificationCenter是一个单例模式,而这个通知中心的对象会一直存在于一个应用的生命周期。

(2)
NSNotification: 这是消息携带的载体,通过它,可以把消息内容传递给观察者。

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

通过上面的介绍可以知道,通过通知中心也可以实现不同类之间的参数传递。

注意当接受到消息后,不想再收到消息了,要把observer删除remove。

下面介绍如何使用(具体解释看文档)。

(1)NSNotification
:用于创建传递的消息

[cpp] view plaincopyCreating Notifications
+ notificationWithName:object:
+ notificationWithName:object:userInfo:
Getting Notification Information
– name
– object
– userInfo

(2)NSNotificationCenter :用于发送消息

[cpp] view plaincopyGetting the Notification Center
+ defaultCenter
Managing Notification Observers
– addObserverForName:object:queue:usingBlock:
– addObserver:selector:name:object:
– removeObserver:
– removeObserver:name:object:
Posting Notifications
– postNotification:
– postNotificationName:object:
– postNotificationName:object:userInfo:

demo(例子中基本上涉及到以上所有的方法了):
定义了两个类:Poster(发送消息)和Observer(接受消息)

Poster.h

[cpp] view plaincopy#import <Foundation/Foundation.h>

@interface Poster : NSObject

-(void)postMessage;

@end

Poster.m

[cpp] view plaincopy#import "Poster.h"

@implementation Poster

-(void)postMessage{

//1.下面两条语句等价
//二者的区别是第一条语句是之间发送消息内容,第二条语句先创建一个消息,然后再发送消息
[[NSNotificationCenter defaultCenter] postNotificationName:@"PosterOne" object:@"This is posterone!"];

// [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"PosterOne" object:@"This is posterone"]];

//2.下面两条语句等价
//参数:userInfo --- Information about the the notification.
[[NSNotificationCenter defaultCenter] postNotificationName:@"PosterTwo" object:@"This is postertwo" userInfo:[NSDictionary dictionaryWithObject:@"value" forKey:@"key"]];

// [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"PosterTwo" object:@"This is postertwo" userInfo:[NSDictionary dictionaryWithObject:@"value" forKey:@"key"]]];
}

@end

Observer.h

[cpp] view plaincopy#import <Foundation/Foundation.h>

@interface Observer : NSObject

-(void)observer;

@end

Observer.m

[cpp] view plaincopy#import "Observer.h"

@implementation Observer

-(void)observer {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callBack1:) name:@"PosterOne" object:nil];

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

//删除所有的observer
// [[NSNotificationCenter defaultCenter] removeObserver:self];
//删除名字为name的observer
// [[NSNotificationCenter defaultCenter] removeObserver:self name:@"PosterOne" object:nil];

}

-(void)callBack1:(NSNotification*)notification{
NSString *nameString = [notification name];
NSString *objectString = [notification object];
NSLog(@"name = %@,object = %@",nameString,objectString);
}

-(void)callBack2:(NSNotification*)notification{
NSString *nameString = [notification name];
NSString *objectString = [notification object];
NSDictionary *dictionary = [notification userInfo];
NSLog(@"name = %@,object = %@,userInfo = %@",nameString,objectString,[dictionary objectForKey:@"key"]);
}

@end

main.m

[cpp] view plaincopy#import <Foundation/Foundation.h>
#import "Poster.h"
#import "Observer.h"

int main(int argc, const char * argv[])
{

@autoreleasepool {

//注意这里的顺序,要先observer,然后再poster
Observer *myObserver = [[Observer alloc] init];
[myObserver observer];

Poster *poster = [[Poster alloc] init];
[poster postMessage];
}
return 0;
}

好了,大概有关的内容都差不多了吧


附:

不过有个方法

addObserverForName:object:queue:usingBlock:

还不太懂如何使用,暂时放一下,有知道了麻烦评论告诉了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: