How can I use IBOutletCollection to connect multiple UIImageViews to the same outlet?

32,336

Declare a property to hold your imageView's and then hook them up in interface builder like normal

@property (nonatomic, strong) IBOutletCollection(UIImageView) NSArray *imageViews;

it's just a normal NSArray but when the nib is loaded it will be populated with your imageView's


Update

In the header file for you view controller which has the multiple imageView's on you need to add the property above - it may look something like this:

@interface MyViewController : UIViewController

@property (nonatomic, strong) IBOutletCollection(UIImageView) NSArray *imageViews;
// other properties

@end

Now in the interface builder you connect all the imageView's to this one property.

enter image description here enter image description here

Now I just work with the imageViews collection

for (UIImageView *imageView in self.imageViews) {
  imageView.image = someImage;
}
Share:
32,336
BalestraPatrick
Author by

BalestraPatrick

iOS Developer and Computer Science University Student. SOreadytohelp

Updated on December 31, 2020

Comments

  • BalestraPatrick
    BalestraPatrick over 3 years

    I have 10 UIImageViews which do the same thing (they have some void methods that change their image with a timer). My UIImageView is an outlet and I want to connect all the 10 imageViews to the same outlet, but interface builder doesn't allow me.

    I found that there is a solution, IBOutletCollection. Can anyone explain to me how to use this to connect multiple imageViews to the same outlet?

  • BalestraPatrick
    BalestraPatrick about 11 years
    But I have only a imageView in my code and that's what I want. I want to connect this image in code with multiple imageViews in the view in storyboard.
  • Paul.s
    Paul.s about 11 years
    You need to create an array to hold a reference to all of these imageViews. The way to do this is with an IBOutletCollection as I have shown above
  • BalestraPatrick
    BalestraPatrick about 11 years
    But which images do I need to populate it if Only have 1 images?
  • Paul.s
    Paul.s about 11 years
    I literally have no idea what you mean?
  • BalestraPatrick
    BalestraPatrick about 11 years
    If I have 10 equal images in my UIView in storyboard and I have some methods that perform actions on an UIImageView outlet called "image". When I go to my storyboard, I can connect the outlet image only to 1 image view. I want to be able to connect this outlet to all my 10 images! Understood? :)
  • BalestraPatrick
    BalestraPatrick about 11 years
    Perfectly what I needed!! Thanks a lot :)
  • SmileBot
    SmileBot almost 11 years
    It's simple. Just as you normally make a connection from an image in the storyboard to your iboutlet. With the collection just drag from each image to the one iboutlet. Many to one. Use the assistant editor.
  • AndrewCr
    AndrewCr about 10 years
    Should this property be "strong"? Most IBOutlets are "weak" when using ARC. Are Outlet Collections an exception?
  • Paul.s
    Paul.s about 10 years
    @AndrewCr yes it should be. We are declaring an NSArray and the only object interested in it is out viewController. You can use weak for views because they will be held onto by their parent view.
  • Amr Lotfy
    Amr Lotfy almost 9 years
    Do I have to set the outlet for each object one at a time ? I tried to set outlet (collection) for multiple buttons by selecting them all in the canvas and left drag to the outlet collection property but that didn't work, is there another way to set the outlet for multiple selected objects at once instead of setting it one by one ?