版權聲明

所有的部落格文章都可以在右邊[blog文章原始檔案]下載最原始的文字檔案,並依你高興使用 docutil 工具轉換成任何對應的格式方便離線閱覽,除了集結成書販賣歡迎任意取用,引用

Notification Note

Notification Note

Notification 做的事情和 delegate 差不多,把他想像成大家都可以收到的 delegate 就差不多了,一樣是一種 event 處理的機制。

What is Notification

Cocoa/Cocoa touch 中處理 event 最常用的機制就是 delegate,所謂 delegate 就是物件有一個屬性 delegate 當有 event 發生的時候會傳送對應的 message 給 delegate 指定的物件,如 UIApplication 會送 applicationDidFinishLaunching: 給 application.delegate 物件,這物件必須實作 UIApplicationDelegate。


http://s3.amazonaws.com/ember/UThpCMccqAZTaAGaD2igtsTVShVddq1z_m.png

Notification 也差不多不過作法有點不同, notification 利用 observer pattern 在使用上比 delegate 更靈活。

註冊 Notifications

首先物件假如要知道特定的 notification 必須先和 notification center 註冊。

http://s3.amazonaws.com/ember/mjk6FfBJX8Y4drDcFeVXDzY9NJn0WA8g_m.png

sample code:

// 註冊 UIKeyboardWillShowNotification 並指定由 keyboardWillShow: 處理。
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShow:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];
// 註冊 UIKeyboardWillHideNotification 並指定由 keyboardWillHide: 處理。
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillHide:)
                                             name:UIKeyboardWillHideNotification
                                           object:nil];

之後當 event 發生, notificaiton center 會收到訊息,並依據註冊的物件發送 notification, 就像 email 的群組寄信一樣。

http://s3.amazonaws.com/ember/d2WLS3Y9EN30CzJQtYL5rxhTe5WnjTR2_m.png

指定接收 notification 的 selector 參數固定是一個 NSNotificaiton 物件 , NSNotificaiton 物件包含三主要的資訊 - name , - object , - userInfo ,這三個資訊依照不同的 notification 會有不同的內容,詳細情形要參考說明文件。

下面範例實作上面註冊範例中的 KeyboardWillShow: ,這個 selector 接收 UIKeyboardWillShowNotification 產生的 notification , UIKeyboardWillShowNotification notification 沒有 object , userInfo 包含一個 NSDictionary 物件,有哪些 Key 也要參考 notification 的定義文件,在本例 UIKeyboardWillShowNotification 的 userInfo 有系統鍵盤沒進入畫面前的座標數據 (UIKeyboardFrameBeginUserInfoKey) 進入畫面後的座標數據(UIKeyboardFrameEndUserInfoKey) 這兩個數據通常差別只有在 origin.y 還沒進入畫面前 origin.y 應該剛好是裝置畫面 (screen) 的最大高度,所以在螢幕外面。 證據還有啟動鍵盤的時候指係觀察鍵盤是從螢幕下方上升進入螢幕,所以使用的 core animate 只是單純的座標移動。

- (void)KeyboardWillShow:(NSNotificaiton *)notification
{
     NSDictionary *userInfo = [notification userInfo];
     // Get the origin of the keyboard when it's displayed.
     NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
     CGRect keyboardRect = [aValue CGRectValue];
     // ...
}

Remove notification

解除註冊也很簡單

[[NSNotificationCenter defaultCenter] removeObserver:self];
// or 都可以
[[NSNotificationCenter defaultCenter] removeObserver:self name:nil object:nil];

Post Notificaiton

如果要自行產生 notification 手間必須建立 notification 物件 ( NSNotification ) 然後使用 postNotification: 送至 notification center。

更簡便的方法是直接使用 postNotificationName: object: 直接告知 notification center。

如果需要更詳細的資訊請參閱 "Notification Programming Topics for Cocoa"

沒有留言:

Related Posts with Thumbnails