對象間的通訊以一種盲目結構進行,也就是MVC的廣播站機制,在iOS7裏叫作Notification通知數據庫
獲取通知中心共享實例: [NSNotificationCenter defaultCenter]
安全
收聽系統廣播:addObserver;app
關閉廣播:removeObserver(很重要)指針
[center removeObserver:self];
code
ororm
[center removeObserver:self name:UIContentSizeCategoryDidChangeNotification object:nil];
dealloc: 會在你的對象將要離開堆前被調用,全部屬性都爲nil,幾乎已經再也不是一個對象的時候 調用這個方法。是最後的關聯。因此也能夠在這裏解決不安全保留指針的問題。可是不建議使用server
使用數據庫構建更復雜模型的時候,介紹如何使用廣播站機制來監聽模型的變化(後面介紹)對象
- (void)addObserver:(id)observer // 想要收聽廣播的對象(self當前控制器) <!--當廣播上出現內容時會調用它--> selector:(SEL)methodToInvokeIfSomethingHappens <!--name是廣播站的名稱--> name:(NSString *)name <!--你是否只想收聽某個特定對象發出的廣播(nil表示頻率上任何廣播)--> object:(id)sender;
- (void)methodToInvokeIfSomethingHappens:(NSNotification *)notification { <!--廣播站的名字,和上面的參數同樣--> notification.name <!--object 就是向你發送這個通知的對象--> notification.object <!--userInfo是取決於廣播站發出的信息--> notification.userInfo // notification-specific information about what happened } ps:userInfo你想知道是什麼,能夠用isKindOfClass或者respondsToSelector等來使用它
<!--注意:該方法裏不能訪問屬性,由於此時屬性已經從堆中移除--> - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; }