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

混江龙づ霸主 提交于 2019-11-26 05:32:14

问题


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?


回答1:


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;
}


来源:https://stackoverflow.com/questions/15836930/how-can-i-use-iboutletcollection-to-connect-multiple-uiimageviews-to-the-same-ou

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!