Silver challenge - setNeedsDisplay won't update UIImageView


#1

[code]

  • (IBAction)deleteImage:(id)sender {
    [[BNRImageStore sharedStore] deleteImageForKey:self.item.itemKey];
    self.imageView.image = nil;
    }[/code]

The above solution works. As soon as self.imageView.image is set to nil, the image disappear.
I happened to made a mistake in my code, instead of setting the image property(an instance of UIImage) of the imageView to nil, I set the imageView(an instance of UIImageView) to nil.

[code]

  • (IBAction)deleteImage:(id)sender {
    [[BNRImageStore sharedStore] deleteImageForKey:self.item.itemKey];
    self.imageView = nil;
    }[/code]
    Therefore, I try to click the button to remove the image, the image is still displayed on screen on the detail view. Then I go back to the table view and get into the detail view again, the image is not shown any longer.
    I know that the image is already removed from the data source when I clicked the button.
    But the view is not updated.
    Then I try to send the message setNeedsDisplay in order to have the view redrawn?
    Sending setNeedsDisplay to self.view seems not working (the view of the detail view controller).
    Sending setNeedsDisplay to self.imageView is also not working.

I would like to know why setNeedsDisplay cannot update the view and make the image disappear as soon as I click the button?

I also found some information on the class reference of UIImageView.
I am not quite sure if it is the reason. It would be great to have a good explanation.

[quote]If you are writing a custom subclass of UIImageView, you should be aware of the following behaviors:

The UIImageView class is optimized to draw its images to the display. UIImageView does not call the drawRect: method of its subclasses. If your subclass needs to include custom drawing code, you should subclass the UIView class instead.

New image view objects are configured to disregard user events by default. If you want to handle events in a custom subclass of UIImageView, you must explicitly change the value of the userInteractionEnabled property to YES after initializing the object.[/quote]

Thanks a lot!


#2

I just set self.imageView.image = nil, and that works for me.