0

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.

1
  • so whenever second change just give firstImageView.image = @"ImageName" Commented Jun 6, 2013 at 5:45

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

hmmm.... KVO is definitely an options but I was wondering if it could be manipulated via pointers... So I guess I can't play bad man here.
@tGilani IMO, just as what I said in the answer, it's impossible. Sorry about this. They are just pointers, not pointers to pointers.
2

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

1 Comment

thanks for the input, i do not have any practical need, i was just wondering if it could be done via pointers..
0

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.

2 Comments

that is a one way solutions, what happens when second imageview changes it's image ? Plus, i was wondering if pointers could be used for this purpose.
You can do the same in second imageview's image setter for two way functionality. You might have to check if _image =! image; to prevent recursive calls. Or you can use delegates. As far as the use of pointers goes, I think all this is already using pointers. No 'image object' is being passed around. Instead what you are passing is pointer to UIImage object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.