【转】NSNotificationCenter用法总结

简介:

Notificationcenter的用法:

【原】NSNotificationCenter未必一定要建在消息接收者的类中。可以放在别的类中,先实例化一下,然后把observer赋值为刚对象。

这里的observer相当于接受者(receiver),object相当于发送者(poster)。理解了这点就可以较灵活地使用通知了。

iPhone软件开发的时候会遇到这种情况:打开APP后会在后台运行某个方法,例如下载文件,下载完成后可能需要调用某个方法来刷新界面,这时候可能没法在下载的函数中回调。NSNotificationCenter(通知)是一个很好的选择。

通知使用起来灰常的简单:

1、定义通知:

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

2、定义通知中使用的方法:

- (void)callBack{	NSLog(@"i am back.");}

3、调用通知:

- (void)getIT{	NSLog(@"get it.");	//发出通知	[[NSNotificationCenter defaultCenter] postNotificationName:@"back" object:self];}

 

notificationcenter的讲解

【转自】http://www.devdiv.com/home.php?mod=space&uid=82357&do=blog&id=3835

Notifications

Notification 包装了事件的信息, 比如窗口正在获取焦点或者网络连接正在断开. 需要订阅事件(例如, 一个文件想要知道正在编辑它的窗口将要被关闭)的object需要在notification center注册, 之后当事件发生的时候就会得到通知. 当事件发生的时候, 一个notification会被发送到notification center, 而后notification center马上就会把这个notification转发给所有订阅过这个事件的object. 当需要的时候, notification会被缓存在notification queue中….

Notification的原理

在 两个object之间传递信息最标准的方法是传递消息 – 一个object调用另外一个object的方法. 但是, 传递的消息这种方法要求发送消息的object知道消息的接收者和它能接收的消息类型. 这将会把两个object紧密的绑定起来 – 最值得注意的是这会让两个本来独立的子系统耦合在一起. 为了解决这些问题, 广播模型被提了出来. Object只是负责发送notification, 而NSNotificationCenter将负责把这个notification转发给所有相关的object.

一个 NSNotification(在这片文章里面简称notification)包含一个name, 一个object和一个可选的dictionary. Name是notification的标识. Object包含notification发送者想要发送的任意类型的object(一般来说就是发送这个notification的object本 身). Dictionary用来保存其他相关的东西(如果有的话).

任意object都可以发送notification. 任意object都可以在notification center注册以便在某个事件发生的时候能够得到通知. Notification center负责把接受的notification发送给所有注册过的消息接收者. 发送notification的object, notification里面包含的object和接收这个notification的object可以是同一个object, 也可以是三个不同的object. 发送notification的object不需要知道关于接受者的任何信息. 但是, 接受者至少需要知道notification的name和其所包含dictionary的key(如果有的话).

Notification和Delegation

就使用上看, notification系统和delegate很像, 但是他们有以下不同:

*Notification的接受者可以是多个object. 但是delegate object只能有一个. 这就阻止了返回值.
*一个object可以从notification center接受它想要的任意数量个notification, 而delegate只能接受预先定义好的delegate方法.
*发送notification的object完全不知到接受者是否存在.

Notification Centers

Notification center负责接收和发送notification. 当它接受到notification的时候会通知所有符合特定条件的接受者. Notification信息被包装在NSNotification里. Notification接收者在notification center里面注册以获得其他object发出的notification. 当事件发生的时候, 一个object发送相关的notification到notification center. Notification center将消息分发给每一个注册过的接受者. 发送notification的object和接受者可能是同一个.

Cocoa包含两种notification center:

*NSNotificationCenter类管理单个进程内部的notification.
*NSDistributedNotificationCenter管理一台机器上跨进程的notification.

NSNotificationCenter

每 一个进程都有一个默认的notification center, 你可以通过访问 NSNotificationCenter 的 +defaultCenter方法来得到它. 这种类型的notification center负责管理单个进程内部的notification. 如果需要管理同一台机器上不同进程之间的notification则需要用到NSDistributedNotificationCenter.

Notification center发送notification给接收者的方法是同步的. 也就是说, 当发送一个notification的时候, 除非所有的接收者都接到和处理了这个notification, 否则不会返回. 想要发送异步notification的话就需要用到notification queue了.

在一个多线程应用程序里, notification总是和发送者处于同一个线程里, 但是接受者可以在其他线程里.

NSDistributedNotificationCenter

每 一个进程都有一个默认的distributed notification center, 你可以通过访问 NSDistributedNotificationCenter 的 +defaultCenter方法来得到它. 这种类型的notification center负责管理一台机器上多个进程之间的notification. 如果需要在多台机器间通讯的话, 使用distributed objects.

发送一个distributed notification是非常昂贵的. Notification首先会被发送到一个系统级别的服务器上, 然后在分别分发到每一个注册过的进程里. 从发从消息到消息被接受到之间的延迟理论上来说是无限的. 事实上, 如果太多的notification被发送到服务器上, 那么服务器上的notification队列可能会被撑满, 这就有可能会造成notification的丢失.

Distributed notification会在一个进程的主循环里被发送出去. 一个进程必须保证有一个主循环在其内部运行, 例如 NSDefaultRunLoopMode, 然后才能接受到distributed notification. 如果接收进程是多线程的, 那么notification并不一定会被主线程接受到. 一般来说notification会被分发到主线程的主循环, 但是其他线程一样可以接收到.

一 般类型的notification center可以注册所有object的notification, 但是 distributed notification center只能注册字符串类型的notification. 因为发送者和接受者可能在不同进程里, notification里面包含的object不能保证指向同一个object. 所以, distributed notification center只能接受包含字符串类型的notification. Notification会基于字符串来匹配.

NotificationCenter的使用
 

1. 定义一个方法

update

2.订阅通知

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(update) name:@"update" object:nil]

3. 在要发出通知消息的地方

[[NSNotificationCenter defaultCenter] postNotificationName:@"update" object:nil];

----------------------------

虚拟键盘显示和消失的通知

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(keyboardWasShown:)

name:UIKeyboardDidShowNotification

object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(keyboardWasHidden:)

name:UIKeyboardDidHideNotification

object:nil];

------

- (void)keyboardWasShown:(NSNotification *) aNotification{

if(keyboardShown)

return;

NSDictionary *info = [aNotification userInfo];//获取通知信息

//get the size of the keyboard.

NSValue *aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];

CGSize keyboardSize = [aValue CGRectValue].size;

//Resize the scroll view

CGRect viewFrame = [scrollView frame];

viewFrame.size.height -= keyboardSize.height;

//Scroll the active text field into view

CGRect textFieldRect = [activeField frame];

[scrollView scrollRectToVisible:textFieldRect animated:YES];

keyboardShown = YES;

}

//Called when the UIKeyboardDidHideNotification is sent

- (void)keyboardWasHidden:(NSNotification *) aNotification{

NSDictionary *info = [aNotification userInfo];

//Get the size of the keyboard.

NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];

CGSize keyboardSize = [aValue CGRectValue].size;

//Reset the height of the scroll view to its original value

CGRect viewFrame = [scrollView Frame];

viewFrame.size.height += keyboardSize.height;

scrollView.frame = viewFrame;

keyboardShown = NO;

}

官方文档说明如下:

postNotificationName:object:userInfo:

Creates a notification with a given name, sender, and information and posts it to the receiver.

- (void)postNotificationName:(NSString *) notificationName object:(id) notificationSender userInfo:(NSDictionary *) userInfo
Parameters
notificationName

The name of the notification.

notificationSender

The object posting the notification.

userInfo

Information about the the notification. May be nil.

从以上可以看出,object表示发送者!!而不是要发送的参数。要发送的参数可以放到information里面,是NSDictionary类型的

本文转自编程小翁博客园博客,原文链接:http://www.cnblogs.com/wengzilin/archive/2012/03/17/2403939.html,如需转载请自行联系原作者

相关文章
|
Swift
深入浅出Swift(5)—— NSNotificationCenter
深入浅出Swift(5)—— NSNotificationCenter
66 0
|
容器
(三)UITabBarController和UITabBar的基本用法
(三)UITabBarController和UITabBar的基本用法
264 0
|
程序员 iOS开发
performSelector的原理以及用法
performSelector的原理以及用法
268 0
|
存储 对象存储 iOS开发
iOS通过NSUserDefaults实现简单的应用间数据传递
iOS通过NSUserDefaults实现简单的应用间数据传递
229 0
通知 NSNotificationCenter 详解
通知NSNotificationCenter详解(一)通知NSNotificationCenter详解(二)
734 0
|
数据安全/隐私保护
|
Web App开发 JavaScript