iOS中通知机制
2015-12-17 16:20
513 查看
通知中心是 Foundation 框架的一个子系统,它向应用程序中注册为某个事件观察者的所有对象广播消息(即通知)。(从编程角度而言,它是
任何对象都可以观察通知,但要做到这一点,该对象必须注册,以接收通知。在注册时,它必须指定选择器,以确定由通知传送所调用的方法;方法签名必须只有一个参数:通知对象。注册后,观察者也可以指定发布对象。
(以上是官方文档中的解释)
------------------------------------------华丽的分割线----------------------------------------------------------
通知中心包括两个重要的类:
(1)NSNotificationCenter: 实现NSNotificationCenter的原理是一个观察者模式,获得NSNotificationCenter的方法只有一种,那就是[NSNotificationCenter
defaultCenter] ,通过调用静态方法defaultCenter就可以获取这个通知中心的对象了,而NSNotificationCenter是一个单例模式,而这个通知中心的对象会一直存在于一个应用的生命周期。
(2) NSNotification:
这是消息携带的载体,通过它,可以把消息内容传递给观察者。
(3)一个NSNotificationCenter可以有许多的通知消息NSNotification,对于每一个NSNotification可以有很多的观察者Observer来接收通知。
通过上面的介绍可以知道,通过通知中心也可以实现不同类之间的参数传递。
注意当接受到消息后,不想再收到消息了,要把observer删除remove。
下面介绍如何使用(具体解释看文档)。
(1)NSNotification
:用于创建传递的消息
[cpp] view
plaincopy
Creating Notifications
+ notificationWithName:object:
+ notificationWithName:object:userInfo:
Getting Notification Information
– name
– object
– userInfo
(2)NSNotificationCenter
:用于发送消息
[cpp] view
plaincopy
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
[cpp] view
plaincopy
#import <Foundation/Foundation.h>
@interface Poster : NSObject
-(void)postMessage;
@end
Poster.m
[cpp] view
plaincopy
#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
[cpp] view
plaincopy
#import <Foundation/Foundation.h>
@interface Observer : NSObject
-(void)observer;
@end
Observer.m
[cpp] view
plaincopy
#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
[cpp] view
plaincopy
#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;
}
NSNotificationCenter类的实例)。该事件可以是发生在应用程序中的任何事情,例如进入后台状态,或者用户开始在文本栏中键入。通知是告诉观察者,事件已经发生或即将发生,因此让观察者有机会以合适的方式响应。通过通知中心来传播通知,是增加应用程序对象间合作和内聚力的一种途径。
任何对象都可以观察通知,但要做到这一点,该对象必须注册,以接收通知。在注册时,它必须指定选择器,以确定由通知传送所调用的方法;方法签名必须只有一个参数:通知对象。注册后,观察者也可以指定发布对象。
(以上是官方文档中的解释)
------------------------------------------华丽的分割线----------------------------------------------------------
通知中心包括两个重要的类:
(1)NSNotificationCenter: 实现NSNotificationCenter的原理是一个观察者模式,获得NSNotificationCenter的方法只有一种,那就是[NSNotificationCenter
defaultCenter] ,通过调用静态方法defaultCenter就可以获取这个通知中心的对象了,而NSNotificationCenter是一个单例模式,而这个通知中心的对象会一直存在于一个应用的生命周期。
(2) NSNotification:
这是消息携带的载体,通过它,可以把消息内容传递给观察者。
(3)一个NSNotificationCenter可以有许多的通知消息NSNotification,对于每一个NSNotification可以有很多的观察者Observer来接收通知。
通过上面的介绍可以知道,通过通知中心也可以实现不同类之间的参数传递。
注意当接受到消息后,不想再收到消息了,要把observer删除remove。
下面介绍如何使用(具体解释看文档)。
(1)NSNotification
:用于创建传递的消息
[cpp] view
plaincopy
Creating Notifications
+ notificationWithName:object:
+ notificationWithName:object:userInfo:
Getting Notification Information
– name
– object
– userInfo
(2)NSNotificationCenter
:用于发送消息
[cpp] view
plaincopy
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
[cpp] view
plaincopy
#import <Foundation/Foundation.h>
@interface Poster : NSObject
-(void)postMessage;
@end
Poster.m
[cpp] view
plaincopy
#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
[cpp] view
plaincopy
#import <Foundation/Foundation.h>
@interface Observer : NSObject
-(void)observer;
@end
Observer.m
[cpp] view
plaincopy
#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
[cpp] view
plaincopy
#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;
}
相关文章推荐
- iOS21 广告服务
- JS判断客户端是否是iOS或者Android
- assert 在iOS 中的作用
- iOS分享 - AFNetworking之多图片/文件上传
- IOS开发-通知与消息机制
- iOS 3DES加密解密(一行代码搞定)
- IOS开发之——绘图(CGContext)
- iOS20 IOS开发之NSLog使用技巧
- iOS中FMDB的使用
- IOS 9 遇到的问题
- bios大神
- iOS RTMP 视频直播开发笔记(5)- iOS H.264 实时硬编码实现原理
- IOS响应式编程框架ReactiveCocoa(RAC)使用示例
- iOS BUG收集: -[__NSCFNumber rangeOfCharacterFromSet:]: unrecognized selector sent to instance
- 几个ios正则表达式 判断邮箱 手机号码 身份证号码
- IOS中延时执行的几种方式的比较和汇总
- iOS---点击tableviewcell中的按钮
- iOS开发 -- SVN的使用
- initWithCoder方法中不能设置子对象控件属性
- CocoaAsyncSocket使用详解