How can I use IBOutletCollection to connect multiple UIImageViews to the same outlet?
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.
Now I just work with the imageViews
collection
for (UIImageView *imageView in self.imageViews) {
imageView.image = someImage;
}
BalestraPatrick
iOS Developer and Computer Science University Student. SOreadytohelp
Updated on December 31, 2020Comments
-
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 about 11 yearsBut 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 about 11 yearsYou 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 about 11 yearsBut which images do I need to populate it if Only have 1 images?
-
Paul.s about 11 yearsI literally have no idea what you mean?
-
BalestraPatrick about 11 yearsIf 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 about 11 yearsPerfectly what I needed!! Thanks a lot :)
-
SmileBot almost 11 yearsIt'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 about 10 yearsShould this property be "strong"? Most IBOutlets are "weak" when using ARC. Are Outlet Collections an exception?
-
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 useweak
for views because they will be held onto by their parent view. -
Amr Lotfy almost 9 yearsDo 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 ?