31

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?

Meet Doshi
  • 4,241
  • 10
  • 40
  • 81
BalestraPatrick
  • 9,944
  • 4
  • 30
  • 43

1 Answers1

83

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;
}
Paul.s
  • 38,494
  • 5
  • 70
  • 88
  • 1
    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. – BalestraPatrick Apr 05 '13 at 15:50
  • 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 – Paul.s Apr 05 '13 at 15:52
  • But which images do I need to populate it if Only have 1 images? – BalestraPatrick Apr 05 '13 at 16:08
  • 3
    I literally have no idea what you mean? – Paul.s Apr 05 '13 at 17:50
  • 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 Apr 05 '13 at 18:29
  • 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. – SmileBot Aug 04 '13 at 03:23
  • Should this property be "strong"? Most IBOutlets are "weak" when using ARC. Are Outlet Collections an exception? – AndrewCr May 01 '14 at 22:01
  • 2
    @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. – Paul.s May 02 '14 at 08:16
  • 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 ? – Amr Lotfy May 27 '15 at 11:29