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 ...
随机推荐
- wampserver2.5 apache2.4.9:forbidden,本机可以访问,局域网内部能访问。
wampserver2.5 apache2.4.9:forbidden,本机可以访问,局域网内部能访问. 因为做项目,多人分工,需要局域网内访问各自的项目. 然后安装了wampserver2.5,Ap ...
- retrifit
Retrofit 特点 性能最好,处理最快 使用REST API时非常方便: 传输层默认就使用OkHttp: 支持NIO: 拥有出色的API文档和社区支持 速度上比volley更快: 如果你的应用程序 ...
- JSP lifecycle - with servlet
编译阶段: servlet容器编译servlet源文件,生成servlet类 初始化阶段: 加载与JSP对应的servlet类,创建其实例,并调用它的初始化方法 执行阶段: 调用与JSP对应的serv ...
- 使用Objective-C的文档生成工具
前言 做项目的人多了,就需要文档了.今天开始尝试写一些项目文档.但是就源代码来说,文档最好和源码在一起,这样更新起来更加方便和顺手.象Java语言本身就自带javadoc命令,可以从源码中抽取文档.今 ...
- 用java实现冒泡排序法
一.基本思路: 冒泡排序是一种简单的交换类排序.其基本思路是,从头开始扫描待排序的元素,在扫描过程中依次对相邻元素进行比较,将关键字值大的元素后移.每经过一趟排序后,关键字值最大的元素将移到末尾,此时 ...
- 【LINUX】Linux学习小结
****xargs命令**** 当需要将参数列表转换成小块分段传递给其他命令时,可以使用xargs命令.栗子如下: 若想在启动lampp之后用kill方式杀掉全部的进程就可以用下面的命令: ps -e ...
- hdu------(1757)A Simple Math Problem(简单矩阵快速幂)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- 什么是 Unix 以及它为什么这么重要?
大多数操作系统可以被划分到两个不同的家族.除了微软的基于Windows NT的操作系统外,几乎所有其他的都可以追溯到Unix. Linux,Mac OS X,Android,iOS,Chrome OS ...
- WebAPI用法
ASP.NET Web API(一):使用初探,GET和POST数据[Parry] HttpClient + ASP.NET Web API, WCF之外的另一个选择[dudu] 通过这两篇文章让我了 ...
- JNI的一些知识:
JNI字段描述符"([Ljava/lang/String;)V" 2012-05-31 12:16:09| 分类: Android |举报|字号 订阅 "([Ljava/ ...