键值观察者的使用及注意事项
来源:互联网 发布:奥尼尔新秀体测数据 编辑:程序博客网 时间:2024/06/05 19:27
//
// Person.h
// Lesson_18_02_KVO(键值观察者)
//
// Created by lanou3g on 15/9/8.
// Copyright (c) 2015年 lanou3g.com. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic,retain)NSString * name;
@end
//
// Person.m
// Lesson_18_02_KVO(键值观察者)
//
// Created by lanou3g on 15/9/8.
// Copyright (c) 2015年 lanou3g.com. All rights reserved.
//
#import "Person.h"
@implementation Person
- (void)dealloc
{
[_name release];
[super dealloc];
}
@end
//
// ViewController.h
// Lesson_18_02_KVO(键值观察者)
//
// Created by lanou3g on 15/9/8.
// Copyright (c) 2015年 lanou3g.com. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// Lesson_18_02_KVO(键值观察者)
//
// Created by lanou3g on 15/9/8.
// Copyright (c) 2015年 lanou3g.com. All rights reserved.
//
#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@property (retain, nonatomic) IBOutlet UITextField *texeField;
- (IBAction)didClickButton:(UIButton *)sender;
@property (nonatomic,retain)Person * person;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.person = [[Person alloc] init];
// 1. 注册观察者(为被观察者添加一个观察者并设置观察的属性)
[_person addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"hello"];
[_person release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
// 5.移除观察者
[_person removeObserver:self forKeyPath:@"name" context:@"hello"];
[_texeField release];
[super dealloc];
}
- (IBAction)didClickButton:(UIButton *)sender {
// 2.改变属性name的值
_person.name = _texeField.text;
}
// 3.只要是被观察者的属性值发生变化,就会执行下面的这个方法(这个方法会自动调用,不需要手动调用)
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
NSLog(@"被检测的对象的属性所在的路径:%@",keyPath);
NSLog(@"被观察者:%@",object);
NSLog(@"被观察的属性所在状态下的值:%@",change);
NSLog(@"在注册观察者的时候传过来的context的值:%@",context);
// 4. 取出新值和旧值(nil:空对象 @"":空字符串)
if (![change[@"new"] isEqualToString:change[@"old"]]) {
self.view.backgroundColor = [UIColor colorWithRed:arc4random() %256/255.0 green:arc4random() %256/255.0 blue:arc4random() %256/255.0 alpha:1.0];
}
// 5.移除观察者(不写在这里,写在这里就不会有实现效果,应该写在dealloc里,即生命周期和person一致)
// [_person removeObserver:self forKeyPath:keyPath context:context];
}
@end
// Person.h
// Lesson_18_02_KVO(键值观察者)
//
// Created by lanou3g on 15/9/8.
// Copyright (c) 2015年 lanou3g.com. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic,retain)NSString * name;
@end
//
// Person.m
// Lesson_18_02_KVO(键值观察者)
//
// Created by lanou3g on 15/9/8.
// Copyright (c) 2015年 lanou3g.com. All rights reserved.
//
#import "Person.h"
@implementation Person
- (void)dealloc
{
[_name release];
[super dealloc];
}
@end
//
// ViewController.h
// Lesson_18_02_KVO(键值观察者)
//
// Created by lanou3g on 15/9/8.
// Copyright (c) 2015年 lanou3g.com. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// Lesson_18_02_KVO(键值观察者)
//
// Created by lanou3g on 15/9/8.
// Copyright (c) 2015年 lanou3g.com. All rights reserved.
//
#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@property (retain, nonatomic) IBOutlet UITextField *texeField;
- (IBAction)didClickButton:(UIButton *)sender;
@property (nonatomic,retain)Person * person;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.person = [[Person alloc] init];
// 1. 注册观察者(为被观察者添加一个观察者并设置观察的属性)
[_person addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:@"hello"];
[_person release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
// 5.移除观察者
[_person removeObserver:self forKeyPath:@"name" context:@"hello"];
[_texeField release];
[super dealloc];
}
- (IBAction)didClickButton:(UIButton *)sender {
// 2.改变属性name的值
_person.name = _texeField.text;
}
// 3.只要是被观察者的属性值发生变化,就会执行下面的这个方法(这个方法会自动调用,不需要手动调用)
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
NSLog(@"被检测的对象的属性所在的路径:%@",keyPath);
NSLog(@"被观察者:%@",object);
NSLog(@"被观察的属性所在状态下的值:%@",change);
NSLog(@"在注册观察者的时候传过来的context的值:%@",context);
// 4. 取出新值和旧值(nil:空对象 @"":空字符串)
if (![change[@"new"] isEqualToString:change[@"old"]]) {
self.view.backgroundColor = [UIColor colorWithRed:arc4random() %256/255.0 green:arc4random() %256/255.0 blue:arc4random() %256/255.0 alpha:1.0];
}
// 5.移除观察者(不写在这里,写在这里就不会有实现效果,应该写在dealloc里,即生命周期和person一致)
// [_person removeObserver:self forKeyPath:keyPath context:context];
}
@end
使用观察者步骤和注意事项:
1:添加观察者
2.实现观察者方法
3.移除观察者(也是注意事项:使用完成后记得移除)
0 0
- 键值观察者的使用及注意事项
- python字典多键值及重复键值的使用
- 基于键值的观察者模式编程
- 锂电池的使用及注意事项
- TreeMap的使用及注意事项
- openSessionInView的使用及注意事项
- shared_ptr 的使用及注意事项
- crontab 的使用及注意事项
- stringstrream的使用及注意事项
- 索引的使用及注意事项
- NSUserDefaults 的使用及注意事项
- popupwindow的使用及注意事项
- webView的使用及注意事项
- sesseion的使用及注意事项
- PopupWindow 的使用及注意事项
- BottomSheetDialog的使用及注意事项
- AsyncTask的使用及注意事项
- KVO-键值观察者
- Yoshua Bengio等大神传授:26条深度学习经验
- CentOS下搭建LAMP环境
- LeetCode OJ8 String to Integer (atoi) 小结
- android开发中关于VersionCode和VersionName
- RAID磁盘阵列中LUN的理解
- 键值观察者的使用及注意事项
- Computer Science Conference Rankings
- window.location.href无效/不跳转的原因分析
- System.currentTimeMillis()获得当前时间
- 类的静态成员
- 如何在MathType中输入对数函数
- springMVC教程初级(四)Controller篇(结果、参数 )
- 评论星星表示好差实现代码
- uva 11134 Fabled Roo