接下来咱们来讲讲通知—NSNotification
先来了解一下。什么是通知?
通过学习KVO,我们发现KVO是一种简单的观察者设计模式,涉及到两个对象,分别是观察者和被观察者。这种方式实质有很大的局限性,那么OC的‘Foundation’框架又为开发者提供了新的一种观察者设计模式,即通知。
**通知,是一种发送给一个或者多个的观察者,用来通知其在程序中发生了某个事件的消息**
Cocoa中的通知机制遵循的是一种广播的模式。它是一种程序中事件的发起者或者是处理者和其他想要知道该事件的对象沟通的一种方式。消息的接收者,也就是观察者响应该事件来改变自己的UI、行为或者状态。
在OC中,使用NSNotification类来表示一个通知。
那么我们先创建一下工程:
老规矩—ios–Application–Single View Application —后面不讲
然后 command+N 创建图片里面的工程,–ios–source–Cocoa touch class–自己看吧,这些都简单
OK,咱们去到ViewController进入代码块:
/*
name:通知名称
object:通知发起人
*/
// 初始化一个通知(NSNotification)的实例对象
NSNotification *notification1 = [NSNotification notificationWithName:@"通知名称1" object:self];
NSNotification *notification2 = [NSNotification notificationWithName:@"通知名称2" object:self userInfo:@{@"content":@"No smoking"}];
//通知中心 单例类,拿到通知中心的单例
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
/*
建立通知的发送机制,如下:
1.注册相关的监听者,并实现在需要的时候回调方法
2.创建并发送通知,在需要的时候,被监听者的对象去到通知中心发送消息
3.移除监听者,在‘dealloc’方法中,移除通知
*/
Student *stu = [[Student alloc]init];
[center postNotification:notification2];
// [center postNotificationName:@"通知名称3" object:self]; //不需要传入通知对象
//
// [center postNotificationName:@"通知名称4" object:self userInfo:@{@"boom!boom!boom!":@"Your mom"}];//不需要通知对象
Weather *weather = [[Weather alloc]init];
PhoneUser *phoneUser = [[PhoneUser alloc]init];
// [weather sendMessage];
我们再去到Student.m文件:
-(id)init{ //重写init方法
if (self = [super init]) {
//注册监听者
/*
1.要去接收通知的对象
2.接收通知要回调的相应方法
3.接收通知的名字
4.发起通知的对象,一般不需要知道,通常填nil
*/
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notificationAction:)name:@"通知名称2" object:nil]; //通知中心是唯一的
}
return self;
}
//监听到通知后回调方法
-(void)notificationAction:(NSNotification *)notification{
NSLog(@"%@",notification.userInfo);
}
-(void)dealloc{
//移除某个通知的监听者
[[NSNotificationCenter defaultCenter]removeObserver:self name:@"通知名称2" object:nil];
//移除所有通知的监听者
// [[NSNotificationCenter defaultCenter]removeObserver:self];
}
然后,咱给天气Weather一个方法(Weather.h):-(void)sendMessage;
去到Weather.m实现一下:
-(void)sendMessage{
//发送通知
[[NSNotificationCenter defaultCenter]postNotificationName:@"WeatherAndPhoneUser" object:self userInfo:@{@"weather":@"Sunny"}];
}
去到手机用户PhoneUser.m文件:
-(id)init{
if (self = [super init]) {
//注册监听者
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(notificationAction:) name:@"WeatherAndPhoneUser" object:nil];
}
return self;
}
-(void)notificationAction:(NSNotification *)notification{
NSDictionary *dic = notification.userInfo;
NSLog(@"%@",dic);
}
-(void)dealloc{
//移除监听者
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
一开始创建的Student类,就是为了为解释通知而使用。
即解释:
1.注册相关的监听者,并实现在需要的时候回调方法
2.创建并发送通知,在需要的时候,被监听者的对象去到通知中心发送消息
3.移除监听者,在‘dealloc’方法中,移除通知
后面是为了实现手机用户的一个监听,来得到天气的消息。即 在天气中发送消息,手机用户监听。
由于里面重复调用到了WeatherAndPhoneUser,我们便在Supporting Files 中给它一个后缀为.pch文件来进行宏定义。首先在Supporting Files右键创建一个pct文件,–NewFiles…–Other–PCH File–再点击OC_10_3,即你工程的名字—然后点击Build Settings 找到prefix Header,然后做一下步骤:
输入$(SRCROOT)/OC_10_3(工程名)/PrefixHeader.pch
之所以要按照这样的规范去写,是因为其换了电脑依然能继续使用,若你直接拖动工程中的文件路径,或许没有出错,但是换了台电脑就废了,就会报错。