iOS 通知中心 NSNotificationCenter(訊息機制)
阿新 • • 發佈:2018-12-30
今天專案要用到NSNotificationCenter,我喜歡叫它訊息(有的地方叫通知)。前兩天有弄過訊息推送,所以想對不瞭解的人解釋一下,ios訊息推送與這個訊息不是一回事!(我感覺他倆名字有的相似,怕有人誤會)
因為本人菜鳥一枚,所以之前弄過一次這個。但是今天要用的時候發現什麼都忘了,所以上網去找(我之前學習的時候看過一個有demo的文章),但是網上講的都是我不瞭解的名詞(觀察者。。就是接受訊息的)。。。。
不過還好最後找到了,在此個大家分享一下:http://blog.csdn.net/crayondeng/article/details/9372079
我的這個demo就是從他那裡弄來的(就是我自己敲了一邊)。。
1)NSNotification :用於建立傳遞的訊息
- Creating Notifications
- + notificationWithName:object:
- + notificationWithName:object:userInfo:
- Getting Notification Information
- – name
- – object
- – userInfo
- Getting the Notification Center
- + defaultCenter
-
Managing Notification Observers
- – addObserverForName:object:queue:usingBlock:
- – addObserver:selector:name:object:
- – removeObserver:
- – removeObserver:name:object:
- Posting Notifications
- – postNotification:
- – postNotificationName:object:
- – postNotificationName:object:userInfo:
demo(例子中基本上涉及到以上所有的方法了):
定義了兩個類:Poster(傳送訊息)和Observer(接受訊息)
Poster.h
- #import <Foundation/Foundation.h>
- @interface Poster : NSObject
- -(void)postMessage;
- @end
Poster.m
- #import "Poster.h"
- @implementation Poster
- -(void)postMessage{
- //1.下面兩條語句等價
- //二者的區別是第一條語句是直接傳送訊息內容,第二條語句先建立一個訊息,然後再發送訊息
- [[NSNotificationCenter defaultCenter] postNotificationName:@"PosterOne" object:@"This is posterone!"];
- // [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"PosterOne" object:@"This is posterone"]];
- //2.下面兩條語句等價
- //引數:userInfo --- Information about the the notification.
- [[NSNotificationCenter defaultCenter] postNotificationName:@"PosterTwo" object:@"This is postertwo" userInfo:[NSDictionary dictionaryWithObject:@"value" forKey:@"key"]];
- // [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"PosterTwo" object:@"This is postertwo" userInfo:[NSDictionary dictionaryWithObject:@"value" forKey:@"key"]]];
- }
- @end
Observer.h
- #import <Foundation/Foundation.h>
- @interface Observer : NSObject
- -(void)observer;
- @end
Observer.m
- #import "Observer.h"
- @implementation Observer
- -(void)observer {
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callBack1:) name:@"PosterOne" object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callBack2:) name:@"PosterTwo" object:nil];
- //刪除所有的observer
- // [[NSNotificationCenter defaultCenter] removeObserver:self];
- //刪除名字為name的observer
- // [[NSNotificationCenter defaultCenter] removeObserver:self name:@"PosterOne" object:nil];
- }
- -(void)callBack1:(NSNotification*)notification{
- NSString *nameString = [notification name];
- NSString *objectString = [notification object];
- NSLog(@"name = %@,object = %@",nameString,objectString);
- }
- -(void)callBack2:(NSNotification*)notification{
- NSString *nameString = [notification name];
- NSString *objectString = [notification object];
- NSDictionary *dictionary = [notification userInfo];
- NSLog(@"name = %@,object = %@,userInfo = %@",nameString,objectString,[dictionary objectForKey:@"key"]);
- }
- @end
main.m
- #import <Foundation/Foundation.h>
- #import "Poster.h"
- #import "Observer.h"
- int main(int argc, constchar * argv[])
- {
- @autoreleasepool {
- //注意這裡的順序,要先observer,然後再poster
- Observer *myObserver = [[Observer alloc] init];
- [myObserver observer];
- Poster *poster = [[Poster alloc] init];
- [poster postMessage];
- }
- return 0;
- }