有没有办法得到通知时,我的UIImageView.image属性更改?

有没有办法在UIImageView.image属性上设置观察者,所以我可以得到通知的属性已被更改? 也许与NSNotification? 我怎么去做这个?

我有大量的UIImageViews,所以我需要知道哪一个更改发生。

我该怎么做呢? 谢谢。

这就是所谓的键值观测。 任何符合键值编码的对象都可以被观察到,并且这包括具有属性的对象。 请阅读此编程指南 ,了解KVO如何工作以及如何使用它。 这是一个简短的例子(免责声明:它可能不工作)

- (id) init { self = [super init]; if (!self) return nil; // imageView is a UIImageView [imageView addObserver:self forKeyPath:@"image" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL]; return self; } - (void) observeValueForKeyPath:(NSString *)path ofObject:(id) object change:(NSDictionary *) change context:(void *)context { // this method is used for all observations, so you need to make sure // you are responding to the right one. if (object == imageView && [path isEqualToString:@"image"]) { UIImage *newImage = [change objectForKey:NSKeyValueChangeNewKey]; UIImage *oldImage = [change objectForKey:NSKeyValueChangeOldKey]; // oldImage is the image *before* the property changed // newImage is the image *after* the property changed } }