/
// 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模式 观察者模式的更多相关文章

  1. IOS中(Xcode) DEBUG模式(RELEASE模式)控制NSLog输出,NSLog两种不同情况的输出方式

    [新年新气象,2016/01/04] 俺们在开发IOS程序过程中,经常需要用到NSLog输出一些信息,甚至有的开发过程,必须在控制台查看输出,有经验的程序员通过控制台输出就能知道整个数据交互的一个流程 ...

  2. iOS 中的 promise 模式

    1.概述 异步编程 App 开发中用得非常频繁,但异步请求后的操作却比较麻烦.Promise 就是解决这一问题的编程模型.其适用于 延迟(deferred) 计算和 异步(asynchronous)  ...

  3. iOS中的事件响应链、单例模式、工厂模式、观察者模式

    学习内容 欢迎关注我的iOS学习总结--每天学一点iOS:https://github.com/practiceqian/one-day-one-iOS-summary iOS中事件传递和相应机制 i ...

  4. IOS中KVO模式的解析与应用

    IOS中KVO模式的解析与应用 最近老翁在项目中多处用到了KVO,深感这种模式的好处.现总结如下: 一.概述 KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修 ...

  5. 【原】IOS中KVO模式的解析与应用

    最近老翁在项目中多处用到了KVO,深感这种模式的好处.现总结如下: 一.概述 KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知.简单 ...

  6. iOS开发——高级篇——iOS中如何选择delegate、通知、KVO(以及三者的区别)

      在开发IOS应用的时候,我们会经常遇到一个常见的问题:在不过分耦合的前提下,controllers[B]怎么进行通信.在IOS应用不断的出现三种模式来实现这种通信:1委托delegation2通知 ...

  7. iOS中如何选择delegate、通知、KVO(以及三者的区别)

    转载自:http://blog.csdn.net/dqjyong/article/details/7685933 在开发IOS应用的时候,我们会经常遇到一个常见的问题:在不过分耦合的前提下,contr ...

  8. 【学习总结】iOS中NSNotification、delegate、KVO三者之间的区别与联系?

    在开发ios应用的时候,我们会经常遇到一个常见的问题:在不过分耦合的前提下,controllers间怎么进行通信.在IOS应用不断的出现三种模式来实现这种通信: 1.委托delegation: 2.通 ...

  9. iOS中关于KVC与KVO知识点

    iOS中关于KVC与KVO知识点 iOS中关于KVC与KVO知识点  一.简介 KVC/KVO是观察者模式的一种实现,在Cocoa中是以被万物之源NSObject类实现的NSKeyValueCodin ...

随机推荐

  1. easyui-datebox 和easyui-datetimebox 设置默认时间当前时间

    //显示当前日期 formatterDate = function (date) { var day = date.getDate() > 9 ? date.getDate() : " ...

  2. DRUPAL点滴

    1 admin/config/user-interface/xxx在hook_menu里定义这样一个path,就会在configuration界面看到xxx的链接 2 $news_items = db ...

  3. wps的几个优点

    wps的几个优点 1.wps非常小巧,wps2011源文件38M,安装后160M,wps2010安装过后158M,工具栏.程序设置.模板可保存在一个文件里面,重装后用\WPS Office Perso ...

  4. java 内部类1

    内部类: 常用的情景:一个类要经常访问另一个类的成员. 举个例子: class人 { 血,氧气... class 心脏{...} } 内部类的优势: 成员内类,作为外部类的成员,可以方便随意的访问外部 ...

  5. android AutoCompleteTextView 自定义BaseAdapter

    最近项目中需要做搜索功能,实现类似 Google.Baidu 搜索的 下拉提示效果.Android为我们提供了 AutoCompleteTextView 控件来完成此功能. 网上好多例子都是简单使用 ...

  6. ubuntu下安装基本配置

    安装ubuntu更新: sudo apt-get update sudo apt-get upgrade 安装Docky: sudo add-apt-repository ppa:ricotz/doc ...

  7. c语言详解sizeof

    一.sizeof的概念   sizeof是C语言的一种单目操作符,如C语言的其他操作符++.--等.     它并不是函数.     sizeof操作符以字节形式给出了其操作数的存储大小.     操 ...

  8. Firefox火狐Flash插件卡死问题完美解决方法(转载)

    http://www.ihacksoft.com/firefox-flash-protectedmode.html 其实这个问题以前就出现过,而最近该问题又出现在最新的 Windows 8.1 系统中 ...

  9. journal

    dec 5 rpt prep exam dec 4 lie to me dec 3 exam dec 2 preparation for exam dec 1 preparation for exam ...

  10. http://ecgui.com/?hg=0&nr=0

    eCGUI- 微型嵌入式GUI体积小巧 大小在 100KB~180KB! 特性: 多窗口支持| 完全中文输入/显示| 多数常用GUI控件| 99.9% ANSI C 编写| 已成功移植 DOS,Lin ...