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會基於字符串來匹配.
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.
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類型的
