RyJ
RyJ

Reputation: 4025

iOS - Change behavior of UIImageView setImage: (custom setter)

I want to perform additional tasks whenever the image of a UIImageView is set. I'm attempting to define a custom setter method, but having no luck.

h:

@property (nonatomic, strong, setter = setImage:) UIImage *image;

m:

- (void)setImage:(UIImage *)image {
    self.image = image;
    // additional tasks here
}

This obviously incurs an infinite loop. How do I do this?

Upvotes: 3

Views: 1033

Answers (2)

Jakub Truhlář
Jakub Truhlář

Reputation: 20720

Obviously you are triggering setter with self.image = image;

use _image = image; to avoid that.

Upvotes: 0

Matt
Matt

Reputation: 1586

Instead of

    self.image = image;

do

    [super setImage:image];

Also, you don't need to specify the setter since that is the default.

Upvotes: 5

Related Questions