Eth*_*len 16 iphone xcode cocoa-touch objective-c ios
有没有办法在UIImageView.image属性上设置观察者,所以我可以得到关于属性何时被更改的通知?也许与NSNotification?我该怎么做呢?
我有大量的UIImageViews,所以我需要知道发生了哪一个更改.
我该怎么做呢?谢谢.
dre*_*lax 21
这称为键值观察.可以观察到任何符合键值编码的对象,这包括具有属性的对象.阅读有关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
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4929 次 |
最近记录: |