如何使用 IBOutletCollection 将多个 UIImageViews 连接到同一个插座? [英] How can I use IBOutletCollection to connect multiple UIImageViews to the same outlet?

查看:23
本文介绍了如何使用 IBOutletCollection 将多个 UIImageViews 连接到同一个插座?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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?

推荐答案

声明一个属性来保存你的 imageView,然后像往常一样将它们连接到界面构建器中

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;

它只是一个普通的 NSArray 但是当 nib 被加载时,它会被你的 imageView 填充

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

更新

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

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

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

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

现在我只使用 imageViews 集合

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

这篇关于如何使用 IBOutletCollection 将多个 UIImageViews 连接到同一个插座?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆