declare object:
interface StockData : NSObject {
NSString * stockName;
float price;
}
@end
@implementation StockData
@end
Add Observer:
[stockForKVO addObserver:self forKeyPath:@"price" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];
Set Value:
myLabel.text = [stockForKVO valueForKey:@"price"];
Reset Value, trigger observer method:
[stockForKVO setValue:@"20.0" forKey:@"price"];
Call observer method:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if([keyPath isEqualToString:@"price"])
{
myLabel.text = [stockForKVO valueForKey:@"price"];
}
}
Remove observer:
- (void)dealloc
{
[super dealloc];
[stockForKVO removeObserver:self forKeyPath:@"price"];
[stockForKVO release];
}
No comments:
Post a Comment