Notifications和NotificationsCenter的使用

来源:互联网 发布:淘宝卖蜂蜜要备案吗 编辑:程序博客网 时间:2024/04/29 16:15

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;

}


/**************************************************************************/

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

0 0