ios 如何使用 IBOutletCollection 将多个 UIImageViews 连接到同一个插座?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15836930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:03:29  来源:igfitidea点击:

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

iosobjective-cxcodeuiimageviewiboutletcollection

提问by BalestraPatrick

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.

我有 10 个 UIImageViews 做同样的事情(他们有一些用计时器改变他们的图像的无效方法)。我的 UIImageView 是一个插座,我想将所有 10 个 imageView 连接到同一个插座,但界面生成器不允许我。

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?

我发现有一个解决方案,IBOutletCollection。谁能向我解释如何使用它来将多个 imageViews 连接到同一个插座?

回答by Paul.s

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 NSArraybut when the nib is loaded it will be populated with your imageView's

这只是一个正常的NSArray但是当笔尖被加载时,它会被你的 imageView 填充



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:

在具有多个 imageView 的视图控制器的头文件中,您需要添加上面的属性 - 它可能如下所示:

@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.

现在在界面构建器中,您将所有 imageView 连接到这一属性。

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

Now I just work with the imageViewscollection

现在我只处理imageViews集合

for (UIImageView *imageView in self.imageViews) {
  imageView.image = someImage;
}