kvo示例代码
by 伍雪颖
- (void)addImageViewObserver {
[_imageView
addObserver:self
forKeyPath:NSStringFromSelector(@selector(image))
options:NSKeyValueObservingOptionInitial
context:nil];
}
- (void)removeImageViewObserver {
[_imageView
removeObserver:self
forKeyPath:NSStringFromSelector(@selector(image))];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context {
if ([object
isKindOfClass:[_imageView
class]]) {
if ([keyPath isEqualToString:NSStringFromSelector(@selector(image))]) {
[self updateImage];
}
}
}
时间: 2024-10-11 17:15:57