Search code examples
uiimageviewswift5xcode11

Reload all UIImageViews on current screen


I'm making an iOS app that shows profile images of friended users on a single page. There might be more than one identical profile image for each user displayed on the same page at any one time. The images are subviews of various custom views. Long pressing on one of the images allows the user to change it. After they have done so, all identical profile images on that page (x number of images) should be changed to the new image.

How can I get all UIImageViews on the page that contain the same profile image to update, without having to set each one individually?

Ideally, I would like to 'reload' the view controller, but I can't find a way to do this. I have tried using setNeedsDisplay(), but it doesn't work.

I'm new to iOS development.


Solution

  • OK, I solved this by subclassing UIImageView and adding a 'user' instance property and an array of all initialized instances as a static property. Now when a profile image is changed in one imageView, I can iterate over all instances and swap out any that belong to the same user.