码迷,mamicode.com
首页 > 其他好文 > 详细

KVO KVC

时间:2014-05-20 11:02:32      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:class   c   ext   a   int   string   

@interface FoodData : NSObject

{

    NSString * foodName;

    float foodPrice;    

}

@end

 

////////////////////////////////////////////////////////////////////////////

    

foodData = [[FoodDataalloc]init];

    [foodDataaddObserver:selfforKeyPath:@"foodName"options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOldcontext:nil];

    [foodDataaddObserver:selfforKeyPath:@"foodPrice"options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOldcontext:nil];

 

    ///调用

    [foodDatasetValue:@"apple"forKey:@"foodName"];

    [foodDatasetValue:@"100.0"forKey:@"foodPrice"];

 

///////响应

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

{

    if( [keyPath isEqualToString:@"foodName"])

    {

        NSLog(@"foodName:%@",[foodDatavalueForKey:@"foodName"]);

    }

    else if( [keyPath isEqualToString:@"foodPrice"])

    {

         NSLog(@"foodPrice:%@",[foodDatavalueForKey:@"foodPrice"]);

    }

}

KVO KVC,布布扣,bubuko.com

KVO KVC

标签:class   c   ext   a   int   string   

原文地址:http://www.cnblogs.com/rollrock/p/3737747.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!