atastrophic
atastrophic

Reputation: 3143

Pointer to Pointer in Objective-C

I'm trying to learn to play with pointers here.

I have a UIImageView. I need to point its image property to another UIImageViews image property, so that whenever I change the second UIImageViews image, the first one gets updated automatically.

Some pointer manipulation here but I can't seem to get my head around it.

Upvotes: 0

Views: 127

Answers (3)

aadarshsg
aadarshsg

Reputation: 2089

Try to override the setter. Make a subclass of UIImageView, have a property for second UIImageView and write something like

-(void)setImage:(UIImage*)image{
    _image = image;
    self.secondImageView.image = image;
}

Hope this helps.

Upvotes: 0

Bonnie
Bonnie

Reputation: 4953

you can use Key-Value Observing

from Apple Docs

Key-value observing provides a mechanism that allows objects to be notified of changes to specific properties of other objects.

KVO’s primary benefit is that you don’t have to implement your own scheme to send notifications every time a property changes.

[imageView1 addObserver:self
                forKeyPath:@"image"
                   options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
                   context:NULL];

- (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.
}

Upvotes: 2

sunkehappy
sunkehappy

Reputation: 9091

That is impossible. They are just pointers. For example aImageView and bImageView. You can set them's image pointer to point to the same UIImage. But change one of them does NOT change the other.

Maybe you can consider to use KVO to do what you want to do. Change one then your method will be called. Then in your method you can change the other.

Upvotes: 2

Related Questions