// 观察者模式
/*
在Cocoa Touch框架中,观察者模式的具体应用有两个:通知机制(notification)机制和KVO(Key-Value Observing)机制
*/
// 通知机制:通知机制和委托模式不同的是,前者是“一对多”的对象之间的通信,后者是“一对一”的对象之间的通信
/*
通知主要有三种:广播通知(broadcast notification)、本地通知(lacal notification)和推送通知(push notification)
广播通知:广播通知可以在一个应用内部的多个对象之间发送消息,主要是发送消息。
本地通知:本地通知是给用户一种“提示”,它的“提示”方式有警告对话框、发出声音、振动和应用图标上显示数字,在计划时间到达时,本地通知由本地ios发出。
推送通知:推送通知由第三方程序发送给苹果的远程服务器,再由远程服务器推送给ios的特定应用。
*/
通知机制实际运用:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"appStatus" object:nil userInfo:nil];
// userInfo:是一个字典对象
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handle:) name:@"appStatus" object:nil];
/*
addObserver:注册观察者
selector:收到通知的回调方法
name:通知名称
object:通知所带的参数
*/
}
- (void)handle:(NSNotification *)obj
{
id m_obj = [obj object];
NSString *str = [obj name];
NSDictionary *dict = [obj userInfo];
NSLog(@"%@",m_obj);
NSLog(@"%@",str);
NSLog(@"%@",dict);
}
至此,通知机制介绍已经完毕,程序运行效果如下:
2014-09-06 23:36:49.788 Test002[1963:907] (null)
2014-09-06 23:36:49.789 Test002[1963:907] appStatus
2014-09-06 23:36:49.790 Test002[1963:907] (null)
// KVO模式不像通知机制那样通过一个通知中心通知所有观察者,而是在对象属性变化时通知会被直接发送给观察者
/*
关键是两个方法:
addObserver: forKeyPath:options:注册观察者
observeValueForKeyPath: ofObject: change: context:观察者重写该方法以响应属性的变化
*/
KVO模式实际运用:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.watcher = [AppStatusWatcher new];
[self addObserver:self.watcher forKeyPath:@"appStatus" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"Pass Context"];
self.appStatus = @"Launch";
return YES;
/*
addObserver:注册的观察者
forKeyPath:要观察的属性
options:属性变化设置的选项,NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld,把属性新旧值都传递给观察者
context:是上下文,类型为(void *),因此,如果传递“空”,应该是“NULL”而非“nil”
*/
}
- (void)applicationWillResignActive:(UIApplication *)application
{
self.appStatus = @"resignActive";
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
self.appStatus = @"EnterBackground";
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
self.appStatus = @"EnterForeground";
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
self.appStatus = @"BecomeActive";
}
- (void)applicationWillTerminate:(UIApplication *)application
{
self.appStatus = @"Terminate";
}
//
// AppStatusWatcher.h
//
#import <Foundation/Foundation.h>
@interface AppStatusWatcher : NSObject
@end
//
// AppStatusWatcher.m
//
#import "AppStatusWatcher.h"
@implementation AppStatusWatcher
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"Property '%@' of object '%@' chaged: %@ context: %@",keyPath,object,change,context);
}
@end
至此,KVO模式介绍已经完毕,程序运行效果如下:
2014-09-06 23:50:17.009 Test002[2131:907] Property 'appStatus' of object '<KFAppDelegate: 0x7521290>' chaged: {
kind = 1;
new = Launch;
old = "<null>";
} context: Pass Context
2014-09-06 23:50:17.015 Test002[2131:907] Property 'appStatus' of object '<KFAppDelegate: 0x7521290>' chaged: {
kind = 1;
new = BecomeActive;
old = Launch;
} context: Pass Context