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 ...
随机推荐
- ftp断点续传
有时候ftp的文件太大了 容易断掉 使用shell下载 1 #!/bin/bash 2 cd /data2/GATK2/refSeqDB/1000genomePhase3 3 ftp -v -n 19 ...
- on delete cascade 和on update cascade
这是数据库外键定义的一个可选项,用来设置当主键表中的被参考列的数据发生变化时,外键表中响应字段的变换规则的.update 则是主键表中被参考字段的值更新,delete是指在主键表中删除一条记录:on ...
- 【NOIP 2016】斗地主
题意 NOIP 2016 斗地主 给你一些牌,按照斗地主的出牌方式,问最少多少次出完所有的牌. 分析 这道题的做法是DFS. 为了体现这道题的锻炼效果,我自己写了好多个代码. Ver1 直接暴力搞,加 ...
- 【CITE】VS2012程序打包部署
选择Debug模式将项目重新生成,并保证没有任何bug 选择解决方案,右击——添加——新建项目——安装和部署 下载过打包工具InstallShield2013LimitedEdition, ...
- mongodb配置及简单示例
安装 在官网下载安装 https://www.mongodb.com/ 配置 我的电脑—>右键属性—>左边列表中的高级程序设置—>环境变量 点击path 把你的mongodb文件路径 ...
- 微软Azure云平台Hbase 的使用
In this article What is HBase? Prerequisites Provision HBase clusters using Azure Management portal ...
- uploader上传
综述 Uploader是非常强大的异步文件上传组件,支持ajax.iframe.flash三套方案,实现浏览器的全兼容,调用非常简单,内置多套主题支持和常用插件,比如验证.图片预览.进度条等. 广泛应 ...
- Hbase0.98的环境搭建
0 安装前提: jDK7 ,hadoop1.1.2 1 下载与hadoop对应的hbase版本: http://mirror.bit.edu.cn/apache/hbase/hbase-0.98.12 ...
- 报错总结_java.lang.RuntimeException: Invalid action class configuration that references an unknown class name
在使用SSH进行项目开发时,一不小心就可能出现以上的错误提示. 这样的问题可以简单理解为未找到名字为XXX的action 1)xxxAction没有在Struts.xml中配置相应的action: 大 ...
- jquery mobile 请求数据方法执行时显示加载中提示框
在jquery mobile开发中,经常需要调用ajax方法,异步获取数据,如果异步获取数据方法由于网速等等的原因,会有一个反应时间,如果能在点击按钮后数据处理期间,给一个正在加载的提示,客户体验会更 ...