// 初始化通知中心 NSNotificationCenter *center =[NSNotificationCenter defaultCenter];
通知中心(NSNotificationCenter)提供了方法來註冊一個監聽通知的監聽器(Observer)async
方法一:post
- (void)addObserver:(id)observer selector:(SEL)aSelector name:(NSString *)aName object:(id)anObject;
方法二:動畫
- (id)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue
usingBlock:(void (^)(NSNotification *note))block;
通知中心(NSNotificationCenter)提供了相應的方法來幫助發佈通知atom
- (void)removeObserver:(id)observer; - (void)removeObserver:(id)observer name:(NSString *)aName object:(id)anObject;
- (void)dealloc { //[super dealloc]; 非ARC中須要調用此句 [[NSNotificationCenter defaultCenter] removeObserver:self]; }
兩個新聞機構(騰訊新聞、新浪新聞),每當發佈新聞時,通知訂閱了該新聞的用戶。spa
新聞機構類 NewsCompany.h線程
// 新聞發佈機構 #import <Foundation/Foundation.h> @interface NewsCompany : NSObject /** * 機構名稱 */ @property (nonatomic, copy) NSString *name; @end
NewsCompany.m3d
#import "NewsCompany.h" @implementation NewsCompany @end
訂閱者類code
Person.hserver
#import <Foundation/Foundation.h> @interface Person : NSObject /** * 姓名 */ @property (nonatomic, copy) NSString *name; - (void)newsCome:(NSNotification *)note; @end
Person.m對象
#import "Person.h" #import "NewsCompany.h" @implementation Person // 收到通知後,回調監聽器的這個方法,而且把通知對象當作參數傳入 - (void)newsCome:(NSNotification *)note { // 通知的發佈者 NewsCompany *obj = note.object; NSLog(@"%@接收到了%@發出的通知,通知內容是:%@", self.name, obj.name, note.userInfo); } // 通常在監聽器銷燬以前取消註冊 - (void)dealloc { // [super dealloc]; [[NSNotificationCenter defaultCenter] removeObserver:self]; } @end
通知中心
main.m
#import <Foundation/Foundation.h> #import "Person.h" #import "NewsCompany.h" int main(int argc, const char * argv[]) { @autoreleasepool { // 1.初始化機構 NewsCompany *tx = [[NewsCompany alloc] init]; tx.name = @"騰訊新聞"; NewsCompany *sina = [[NewsCompany alloc] init]; sina.name = @"新浪新聞"; // 2.初始化2我的 Person *zhangsan = [[Person alloc] init]; zhangsan.name = @"張三"; Person *lisi = [[Person alloc] init]; lisi.name = @"李四"; // 初始化通知中心 NSNotificationCenter *center =[NSNotificationCenter defaultCenter]; // 3.註冊通知監聽器 // zhangsan只監聽tx發出的junshi_news_come通知 [center addObserver:zhangsan selector:@selector(newsCome:) name:@"junshi_news_come" object:nil]; // lisi監聽tx發的全部通知 [center addObserver:lisi selector:@selector(newsCome:) name:nil object:tx]; // 4.發佈通知 // tx發佈了一則叫作junshi_news_come的通知 [center postNotificationName:@"junshi_news_come" object:tx userInfo:@{@"title" : @"伊拉克戰爭中止了", @"intro" : @"伊拉克戰爭中止了.........."}]; // sina發佈了一則叫作junshi_news_come的通知 [center postNotificationName:@"yule_news_come" object:sina userInfo:@{@"title" : @"6456456456456", @"intro" : @"7657567567567"}]; } return 0; }
運行結果:
監聽:
// 2.監聽鍵盤的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
取消監聽:
- (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; }
在使用過程當中,咱們須要注意,最後通知的線程,是由發起通知的線程決定的。若是發起通知是主線程,則收到的通知也是主線程。
更新UI必需要在主線程中更新,所以,咱們最好在全部的通知回調中,都判斷一下,若是當前線程不是主線程,則回到主線程。
宏定義以下,這個在SDWebImage裏有這個宏定義。
#define dispatch_main_async_safe(block)\ if ([NSThread isMainThread]) {\ block();\ } else {\ dispatch_async(dispatch_get_main_queue(), block);\ }