[英]Binding pointers in Objective-C
在我的代码中的某个时刻,我将UIImageView *
imageView.image
设置为UIImage *
myImage
。 从这一点来看,我很乐意在imageView
中查看myImage
的变化。
在任何myImage
更改时,我想imageView
显示myImage
。 希望你能理解。 谢谢你。
-- 编辑 1 以获得更好的解释。
在代码中的某个时刻,我将 imageView 的图像设置为myClass.image
。 从这一点开始,我想imageView
对myClass.image
的任何变化做出反应。 例如myClass.image = [UIImage imageNamed:@"foo"];
. imageView
和myClass.image
都是保留的属性。 现在,当我更改myClass.image
时,我还必须设置imageView.image = myClass.image
才能在图像视图中看到新图像。
看看 KVO 。
将观察者添加到myClass.image
属性,当它发生变化时,更新您的imageView
。 代码看起来像:
// To add the observer
[myClass addObserver:self forKeyPath:@"image" options:0 context:0];
// And to listen for changes
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"image"])
myImageView.image = object.image;
else
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
我不久前为此编写了一个实用程序 class,因此您可以从我的保管箱中下载它。
语法是这样的:
[self bind:@"myImage" toObject:imageView keyPath:@"image"];
...
[self unbind:@"myImage" fromObject:imageView keyPath:@"image"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.