xcode 我可以将具有不同标签的多个对象连接到同一个 IBOutlet 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8564249/
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-09-14 22:47:19  来源:igfitidea点击:

Can I connect multiple objects with different tags to the same IBOutlet?

iosobjective-cxcodeiboutletiboutletcollection

提问by Scott Walker

I have 30 buttons in one view in Interface Builder. Each has a different tag between 100001 and 100030. I've found it easy to use the same action for each button, passing along the tag for each one when pressed and using code to decide which level to load.

我在 Interface Builder 的一个视图中有 30 个按钮。每个按钮都有一个介于 100001 和 100030 之间的不同标签。我发现对每个按钮使用相同的操作很容易,按下时传递每个按钮的标签并使用代码来决定加载哪个级别。

I want to connect all the buttons to a single IBOutlet, but have each button load a different image based on the user's saved data and the button's tag.

我想将所有按钮连接到单个 IBOutlet,但让每个按钮根据用户保存的数据和按钮的标签加载不同的图像。

How do I do this?

我该怎么做呢?

回答by Caleb

Use IBOutletCollection to add an outlet collection to your view controller, like this:

使用 IBOutletCollection 将插座集合添加到您的视图控制器,如下所示:

@property (retain, nonatomic) IBOutletCollection(UIButton) NSMutableSet* buttons;

This will let you connect all your buttons to one outlet. The property buttonswill be a NSMutableSet containing all your buttons. You can continue to identify individual buttons using the button's tagproperty. This is handy if you want to iterate through all your buttons, perhaps to set up each button's image:

这将使您可以将所有按钮连接到一个插座。该属性buttons将是一个包含所有按钮的 NSMutableSet。您可以继续使用按钮的tag属性来识别单个按钮。如果您想遍历所有按钮,这很方便,也许是为了设置每个按钮的图像:

for (UIButton *b in self.buttons) {
    b.imageView.image = [self imageForTag:b.tag];
}

(You'll need to supply the -imageForTag:method to provide the right image for a given tag, or find some other way to map from tags to images.)

(您需要-imageForTag:提供为给定标签提供正确图像的方法,或者找到其他方法将标签映射到图像。)

Of course, if you already know the range of tag values for all your buttons, and if you've taken care to make the tags unique inside the view containing all the buttons, you can also just fetch each button individually using -viewWithTag:. This is probably not as fast as having the whole set of buttons already created, as you have with the outlet collection described above, but it does mean that there's one less thing to maintain.

当然,如果您已经知道所有按钮的标签值范围,并且如果您已经注意在包含所有按钮的视图中使标签唯一,您也可以使用-viewWithTag:. 这可能不如创建整套按钮那么快,就像上面描述的插座集合那样,但这确实意味着少了一件需要维护的事情。

回答by Krunal

Follow these steps to create an array of outlets an connect it with IB Elements:

按照以下步骤创建一组插座并将其与 IB Elements 连接:

  • Create an array of IBOutlets
  • Add multiple UIElements (Views) in your Storyboard ViewController interface
  • Select ViewController (In storyboard) and open connection inspector
  • There is option 'Outlet Collections' in connection inspector (You will see an array of outlets there)
  • Connect if with your interface elements
  • 创建一个 IBOutlets 数组
  • 在 Storyboard ViewController 界面中添加多个 UIElements(视图)
  • 选择 ViewController(在故事板中)并打开连接检查器
  • 连接检查器中有“出口集合”选项(您将在那里看到一系列出口)
  • 如果与您的界面元素连接

-

——

class ViewController2: UIViewController {


    @IBOutlet var collection:[UIView]!


    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

enter image description here

enter image description here