打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Cocoa中KVO的简单用法
KVO概述:
KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知。
简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应的观察者了。

KVO的优点:
当有属性改变,KVO会提供自动的消息通知。这样开发人员不需要自己去实现这样的方案:每次属性改变了就发送消息通知。
这是KVO机制提供的最大的优点。因为这个方案已经被明确定义,获得框架级支持,可以方便地采用。
开发人员不需要添加任何代码,不需要设计自己的观察者模型,直接可以在工程里使用。
其次,KVO的架构非常的强大,可以很容易的支持多个观察者观察同 一个属性,以及相关的值。

使用步骤如下:
1. 注册,指定被观察者的属性,
2. 实现回调方法
3. 触发回调方法
4. 移除观察

KVO使用例子代码如下:
###############Model(模型)###############
#import<Foundation/Foundation.h>
@interface Music : NSObject {
    // 监听的属性
   NSString *musicName;
}
@end

#import "Music.h"
@implementation Music
@end

###############ViewController(视图控制器)###############
#import <UIKit/UIKit.h>
@class Music;
@interface ViewController : UIViewController {
    Music*music;   
}
@property (nonatomic, retain) Music *music;
@end

@implementation ViewController
@synthesize music;

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [superviewDidLoad];
   
    music =[[Music alloc] init];
   
    // 添加观察者 注册当属性发生改变的时候被调用的
    [music addObserver:selfforKeyPath:@"musicName" options:NSKeyValueObservingOptionNew |NSKeyValueObservingOptionOld context:nil];
   
    //UILabel控件
    UILabel*musicLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 150,280, 21)];
   musicLabel.font = [UIFont fontWithName:@"ArialMT" size:18];
   musicLabel.textColor = [UIColor redColor];
   musicLabel.tag = 100;
    [self.viewaddSubview:musicLabel];
    [musicLabelrelease];
   
    //UITextField控件
    UITextField*musicTextField = [[UITextField alloc] initWithFrame:CGRectMake(20,200, 280, 21)];
   musicTextField.font = [UIFont fontWithName:@"ArialMT"size:18];
   musicTextField.placeholder = @"Please enter some words.";
   musicTextField.backgroundColor = [UIColor whiteColor];
   
    //UITextField输入内容时候调用
   [musicTextField addTarget:selfaction:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
   
    [self.viewaddSubview:musicTextField];
   [musicTextField release];

   self.view.backgroundColor = [UIColor grayColor];
}

- (void)textFieldDidChange:(id)sender {
    UITextField*textField = (UITextField *)sender;
   NSLog(@">>>>>>>>>>>>>>>%@",textField.text);
    //修改正在监听的属性,将调用下面回调方法
    [music setValue:textField.textforKey:@"musicName"];
}

// 只要Music类的"musicName"属性发生的变化都会触发到以下的方法
-(void)observeValueForKeyPath:(NSString *)keyPathofObject:(id)object change:(NSDictionary *)change context:(void*)context {
    UILabel*label = (UILabel *)[self.view viewWithTag:100];
    //如果改变的属性是"musicName"
    if([keyPathisEqualToString:@"musicName"]) {
       // 将 当前的musicName属性的值赋值给UILabel
       label.text = [musicvalueForKey:@"musicName"];
       // 输出改变前的值
       NSLog(@"old musicName is %@",[changeobjectForKey:@"old"]);
       // 输出改变后的值
       NSLog(@"new musicName is %@",[change objectForKey:@"new"]);       
    }
}

#pragma mark - Memory Management
- (void)dealloc {
    // 移除观察者
    [music removeObserver:selfforKeyPath:@"musicName"];
    [musicrelease];
    [superdealloc];
}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
UI--普通控件总结1--控件使用,ui--总结1--控件
iOS:KVO的概述与使用
UI 常用方法总结之----UILabel UITextField
解析KVO实现原理
IOS 如何选择delegate、notification、KVO?
iOS 中KVC、KVO、NSNotification、delegate 总结及区别
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服