今天项目要用到NSNotificationCenter,我喜欢叫它消息(有的地方叫通知)。前两天有弄过消息推送,所以想对不了解的人解释一下,ios消息推送与这个消息不是一回事!(我感觉他俩名字有的相似,怕有人误会)
因为本人菜鸟一枚,所以之前弄过一次这个。但是今天要用的时候发现什么都忘了,所以上网去找(我之前学习的时候看过一个有demo的文章),但是网上讲的都是我不了解的名词(观察者。。就是接受消息的)。。。。
不过还好最后找到了,在此个大家分享一下:http://blog.youkuaiyun.com/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, const char * argv[])
- {
- @autoreleasepool {
- //注意这里的顺序,要先observer,然后再poster
- Observer *myObserver = [[Observer alloc] init];
- [myObserver observer];
- Poster *poster = [[Poster alloc] init];
- [poster postMessage];
- }
- return 0;
- }
