现在的位置: 首页 > 综合 > 正文

iOS 通知中心 NSNotificationCenter(消息机制)

2018年05月11日 ⁄ 综合 ⁄ 共 3705字 ⁄ 字号 评论关闭

今天项目要用到NSNotificationCenter,我喜欢叫它消息(有的地方叫通知)。前两天有弄过消息推送,所以想对不了解的人解释一下,ios消息推送与这个消息不是一回事!(我感觉他俩名字有的相似,怕有人误会)

因为本人菜鸟一枚,所以之前弄过一次这个。但是今天要用的时候发现什么都忘了,所以上网去找(我之前学习的时候看过一个有demo的文章),但是网上讲的都是我不了解的名词(观察者。。就是接受消息的)。。。。

不过还好最后找到了,在此个大家分享一下:http://blog.csdn.net/crayondeng/article/details/9372079

我的这个demo就是从他那里弄来的(就是我自己敲了一边)。。

1)NSNotification
:用于创建传递的消息

  1. Creating Notifications  
  2. + notificationWithName:object:  
  3. + notificationWithName:object:userInfo:  
  4. Getting Notification Information  
  5. – name  
  6. – object  
  7. – userInfo  

(2)NSNotificationCenter
:用于发送消息

  1. Getting the Notification Center  
  2. + defaultCenter  
  3. Managing Notification Observers  
  4. – addObserverForName:object:queue:usingBlock:  
  5. – addObserver:selector:name:object:  
  6. – removeObserver:  
  7. – removeObserver:name:object:  
  8. Posting Notifications  
  9. – postNotification:  
  10. – postNotificationName:object:  
  11. – postNotificationName:object:userInfo:  


demo(例子中基本上涉及到以上所有的方法了):

定义了两个类:Poster(发送消息)和Observer(接受消息)

Poster.h

  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface Poster : NSObject  
  4.   
  5. -(void)postMessage;  
  6.   
  7. @end  

Poster.m

  1. #import "Poster.h"  
  2.   
  3. @implementation Poster  
  4.   
  5. -(void)postMessage{  
  6.       
  7.     //1.下面两条语句等价  
  8.     //二者的区别是第一条语句是直接发送消息内容,第二条语句先创建一个消息,然后再发送消息  
  9.     [[NSNotificationCenter defaultCenter] postNotificationName:@"PosterOne" object:@"This is posterone!"];  
  10.       
  11. //    [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"PosterOne" object:@"This is posterone"]];  
  12.       
  13.     //2.下面两条语句等价  
  14.     //参数:userInfo  --- Information about the the notification.  
  15.     [[NSNotificationCenter defaultCenter] postNotificationName:@"PosterTwo" object:@"This is postertwo" userInfo:[NSDictionary dictionaryWithObject:@"value" forKey:@"key"]];  
  16.       
  17. //    [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:@"PosterTwo" object:@"This is postertwo" userInfo:[NSDictionary dictionaryWithObject:@"value" forKey:@"key"]]];  
  18. }  
  19.   
  20. @end  

Observer.h

  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface Observer : NSObject  
  4.   
  5. -(void)observer;  
  6.   
  7. @end  

Observer.m

  1. #import "Observer.h"  
  2.   
  3. @implementation Observer  
  4.   
  5. -(void)observer {  
  6.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callBack1:) name:@"PosterOne" object:nil];  
  7.       
  8.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callBack2:) name:@"PosterTwo" object:nil];  
  9.       
  10.     //删除所有的observer  
  11. //    [[NSNotificationCenter defaultCenter] removeObserver:self];  
  12.     //删除名字为name的observer  
  13. //    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"PosterOne" object:nil];  
  14.       
  15. }  
  16.   
  17. -(void)callBack1:(NSNotification*)notification{  
  18.     NSString *nameString = [notification name];  
  19.     NSString *objectString = [notification object];  
  20.     NSLog(@"name = %@,object = %@",nameString,objectString);  
  21. }  
  22.   
  23. -(void)callBack2:(NSNotification*)notification{  
  24.     NSString *nameString = [notification name];  
  25.     NSString *objectString = [notification object];  
  26.     NSDictionary *dictionary = [notification userInfo];  
  27.     NSLog(@"name = %@,object = %@,userInfo = %@",nameString,objectString,[dictionary objectForKey:@"key"]);  
  28. }  
  29.   
  30. @end  

main.m

  1. #import <Foundation/Foundation.h>  
  2. #import "Poster.h"  
  3. #import "Observer.h"  
  4.   
  5. int main(int argc, const char * argv[])  
  6. {  
  7.   
  8.     @autoreleasepool {  
  9.           
  10.         //注意这里的顺序,要先observer,然后再poster  
  11.         Observer *myObserver = [[Observer alloc] init];  
  12.         [myObserver observer];  
  13.           
  14.         Poster *poster = [[Poster alloc] init];  
  15.         [poster postMessage];  
  16.     }  
  17.     return 0;  
  18. }  

如果你明白这个demo的话我想你就明白消息是怎么传送的了。大笑

抱歉!评论已关闭.