我疯了,我在网上到处找,但我总是找到相同的 KVO 观察代码。然而我的 observeValueForKeyPath: 从未被调用过;这是我用来观察 UILabel taxmeterValue 的简单代码:
-(id) initWithCoder:(NSCoder *)aDecoder{
self=[super initWithCoder:aDecoder];
if (self){
[taximeterValue addObserver:self forKeyPath:@"text" options:(NSKeyValueObservingOptionNew |
NSKeyValueObservingOptionOld) context:NULL];
}
return self;
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context {
if ([keyPath isEqual:@"text"]) {
if (myBookingAlert) myBookingAlert.taximeterValue=self.taximeterValue;
NSLog(@"the text changed");
// put your logic here
}
// be sure to call the super implementation
// if the superclass implements it
[super observeValueForKeyPath:keyPath
ofObject:object
change:change
context:context];
}