IOS 中的KVO模式 观察者模式
/
// KvoObject.h
// KVO
//
// Created by lin kang on 16/6/7.
// Copyright © 2016年 lin kang. All rights reserved.
// #import <Foundation/Foundation.h> @interface KvoObject : NSObject @property (nonatomic,retain) NSString *changeStr; @end //
// KvoObject.m
// KVO
//
// Created by lin kang on 16/6/7.
// Copyright © 2016年 lin kang. All rights reserved.
// #import "KvoObject.h" @implementation KvoObject
@synthesize changeStr = _changeStr; +(BOOL)automaticallyNotifiesObserversForKey:(NSString *)key{
//进行手动调用观察者模式 返回NO
if ([key isEqualToString:@"changeStr"]) {
return NO;
}else{
return [super automaticallyNotifiesObserversForKey:key];
}
} -(NSString *)changeStr{
if (_changeStr == nil) {
_changeStr = @"KVO模式";
}
return _changeStr;
} -(void)setChangeStr:(NSString *)changeStr{
_changeStr = changeStr;
//要进行触发观察者模式,需触发这两个方法
[self willChangeValueForKey:@"changeStr"];
[self didChangeValueForKey:@"changeStr"];
}
@end
//
// ViewController.m
// KVO
//
// Created by lin kang on 16/6/7.
// Copyright © 2016年 lin kang. All rights reserved.
// #import "ViewController.h"
#import "AppDelegate.h"
#import "ViewController1.h" @interface ViewController ()
{
AppDelegate *_appDelegate;
UILabel *_lable;
}
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; // Do any additional setup after loading the view, typically from a nib.
UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 21)];
lb.center =self.view.center;
lb.text = _appDelegate.kvoObject.changeStr;
lb.backgroundColor = [UIColor blueColor];
_lable = lb;
[self.view addSubview:lb]; UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
[bt setTitle:@"点我" forState:UIControlStateNormal];
[bt setBackgroundColor:[UIColor redColor]];
[bt setFrame:CGRectMake(100, 300, 60, 44)];
[self.view addSubview:bt];
[bt addTarget:self action:@selector(btc:) forControlEvents:UIControlEventTouchUpInside]; //注册自己为观察者
[_appDelegate.kvoObject addObserver:self forKeyPath:@"changeStr" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:nil];
} //接受观察通知
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
NSLog(@"change:%@",change);
NSString *newStr = [change objectForKey:@"new"];
_lable.text = newStr;
// [self.view setNeedsDisplay];
} -(void)btc:(UIButton *)sender{
ViewController1 *vc1 = [[ViewController1 alloc] init];
[self.navigationController pushViewController:vc1 animated:YES];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
//
// ViewController1.m
// KVO
//
// Created by lin kang on 16/6/7.
// Copyright © 2016年 lin kang. All rights reserved.
// #import "ViewController1.h"
#import "AppDelegate.h" @interface ViewController1 ()
{
AppDelegate *_appDelegate;
UILabel *_lable;
}
@end @implementation ViewController1 - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
_appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
// Do any additional setup after loading the view, typically from a nib.
UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 21)];
lb.center =self.view.center;
lb.text = _appDelegate.kvoObject.changeStr;
lb.backgroundColor = [UIColor blueColor];
_lable = lb;
[self.view addSubview:lb]; UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
[bt setTitle:@"改变对象" forState:UIControlStateNormal];
[bt setBackgroundColor:[UIColor redColor]];
[bt setFrame:CGRectMake(100, 300, 60, 44)];
[self.view addSubview:bt];
[bt addTarget:self action:@selector(changeing:) forControlEvents:UIControlEventTouchUpInside];
//注册自己为观察者
[_appDelegate.kvoObject addObserver:self forKeyPath:@"changeStr" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:nil];
} //改变对象
-(void)changeing:(UIButton *)sender{
_appDelegate.kvoObject.changeStr = @"我变了,操!";
} //接收通知
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
NSLog(@"change:%@",change);
NSString *newStr = [change objectForKey:@"new"];
_lable.text = newStr;
// [self.view setNeedsDisplay];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ @end
所谓观察者模式运用:当对象属性改变是,同时通知多个页面的进行更改。
IOS 中的KVO模式 观察者模式的更多相关文章
- IOS中(Xcode) DEBUG模式(RELEASE模式)控制NSLog输出,NSLog两种不同情况的输出方式
[新年新气象,2016/01/04] 俺们在开发IOS程序过程中,经常需要用到NSLog输出一些信息,甚至有的开发过程,必须在控制台查看输出,有经验的程序员通过控制台输出就能知道整个数据交互的一个流程 ...
- iOS 中的 promise 模式
1.概述 异步编程 App 开发中用得非常频繁,但异步请求后的操作却比较麻烦.Promise 就是解决这一问题的编程模型.其适用于 延迟(deferred) 计算和 异步(asynchronous) ...
- iOS中的事件响应链、单例模式、工厂模式、观察者模式
学习内容 欢迎关注我的iOS学习总结--每天学一点iOS:https://github.com/practiceqian/one-day-one-iOS-summary iOS中事件传递和相应机制 i ...
- IOS中KVO模式的解析与应用
IOS中KVO模式的解析与应用 最近老翁在项目中多处用到了KVO,深感这种模式的好处.现总结如下: 一.概述 KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修 ...
- 【原】IOS中KVO模式的解析与应用
最近老翁在项目中多处用到了KVO,深感这种模式的好处.现总结如下: 一.概述 KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知.简单 ...
- iOS开发——高级篇——iOS中如何选择delegate、通知、KVO(以及三者的区别)
在开发IOS应用的时候,我们会经常遇到一个常见的问题:在不过分耦合的前提下,controllers[B]怎么进行通信.在IOS应用不断的出现三种模式来实现这种通信:1委托delegation2通知 ...
- iOS中如何选择delegate、通知、KVO(以及三者的区别)
转载自:http://blog.csdn.net/dqjyong/article/details/7685933 在开发IOS应用的时候,我们会经常遇到一个常见的问题:在不过分耦合的前提下,contr ...
- 【学习总结】iOS中NSNotification、delegate、KVO三者之间的区别与联系?
在开发ios应用的时候,我们会经常遇到一个常见的问题:在不过分耦合的前提下,controllers间怎么进行通信.在IOS应用不断的出现三种模式来实现这种通信: 1.委托delegation: 2.通 ...
- iOS中关于KVC与KVO知识点
iOS中关于KVC与KVO知识点 iOS中关于KVC与KVO知识点 一.简介 KVC/KVO是观察者模式的一种实现,在Cocoa中是以被万物之源NSObject类实现的NSKeyValueCodin ...
随机推荐
- tomcat集群配置
http://www.finereporthelp.com/help/24/1/3/3.html#a:4.1
- android中的通信机制总结
第一种:使用handler来进行通信 handler 大家可以把它想象成主线程(UI线程)的一个子线程,它可以给主线程(UI线程)发送数据从而更新主线程(UI线程)的UI与逻辑,handler ...
- 优之星S900平板电脑root方法
优之星S900root方法: 尝试百度一键root安卓的apk版本root失败. 电脑上下载百度一键root的Windows版本,平板设置→开发者选项→允许USB调试√,保持唤醒状态√,安全设置→允许 ...
- QQ等级图标排名说明_QQ等级表,QQ最高等级(皇冠) qq到一星要5天
从2007年11月29日中午12:00开始,在不改变原有计算方式的情况下,加速QQ会员等级升级.QQ会员用户在原有通过每天在线2小时累积活跃天数来获取相应QQ等级增长的基础上,还可以根据QQ会员VIP ...
- struts2 I18N 国际化
1. 准备properties文件 globalMessages_en_US.properties globalMessages_zh_CN.properties 2. 配置struts.xml &l ...
- dede如何新建一个ajax服务端输出文件
<?phprequire_once(dirname(__FILE__)."/include/common.inc.php");AjaxHead(); $dsql-> ...
- java中运算符的解析和计算
package com.LBH; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import j ...
- 笔记2:傻瓜式盗QQ程序
1.一个简单的盗QQ号的方法 仿做一个QQ的窗体 PS:当然里面有用的控件只有两个输入框和一个登陆按钮,其他的尽量做得像一些吧! 点登陆时的后台代码: PS:需要导入两个包:using System. ...
- linux查看是否已安装GCC及安装GCC
输入:gcc -v;如果提示未找到命令即表示没有安装 使用:yum install gcc即可
- poj1190 生日蛋糕(深搜+剪枝)
题目链接:poj1190 生日蛋糕 解题思路: 深搜,枚举:每一层可能的高度和半径 确定搜索范围:底层蛋糕的最大可能半径和最大可能高度 搜索顺序:从底层往上搭蛋糕,在同一层尝试时,半径和高度都是从大到 ...