[英]Binding pointers in Objective-C
At some point in my code I set UIImageView *
imageView.image
to UIImage *
myImage
.在我的代码中的某个时刻,我将
UIImageView *
imageView.image
设置为UIImage *
myImage
。 From this point I would love to view changes of myImage
in imageView
.从这一点来看,我很乐意在
imageView
中查看myImage
的变化。
On any myImage
change, I want to imageView
to show myImage
.在任何
myImage
更改时,我想imageView
显示myImage
。 Hope you understand.希望你能理解。 Thank you.
谢谢你。
-- Edit 1 for better explanation. -- 编辑 1 以获得更好的解释。
At some point in code, i set imageView's image to myClass.image
.在代码中的某个时刻,我将 imageView 的图像设置为
myClass.image
。 And from this point I want to imageView
react on any change of myClass.image
.从这一点开始,我想
imageView
对myClass.image
的任何变化做出反应。 For example myClass.image = [UIImage imageNamed:@"foo"];
例如
myClass.image = [UIImage imageNamed:@"foo"];
. . Both
imageView
and myClass.image
are properties with retain. imageView
和myClass.image
都是保留的属性。 Now, when I change the myClass.image
, I also have to set imageView.image = myClass.image
to see new image in image view.现在,当我更改
myClass.image
时,我还必须设置imageView.image = myClass.image
才能在图像视图中看到新图像。
Take a look at KVO . 看看 KVO 。
Add an observer to the myClass.image
property and when it changes, update your imageView
.将观察者添加到
myClass.image
属性,当它发生变化时,更新您的imageView
。 The code would look something like:代码看起来像:
// 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];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.