Swift - 相当于IBOutletCollection [英] Swift - IBOutletCollection equivalent
问题描述
我正试图在Swift的iTunesU中为为iphone和ipad开发ios7应用程序复制斯坦福Matchismo游戏。
I'm trying to replicate the Stanford Matchismo game from "Developing ios7 apps for iphone and ipad" in iTunesU in Swift.
在第3讲的第77页幻灯片,它显示使用 IBOutletCollection
这不是Swift的选项。 Swift doc示例显示了一个包含 IBOutlet
数组的示例,但我无法弄清楚如何使Interface Builder将多个出口连接到相同的 IBOutlet
/ IBOutlet
数组。
On page 77 of the 3rd lecture slides, it shows using an IBOutletCollection
which isn't an option on Swift. The Swift doc example shows one example that has an array of IBOutlet
, but I can't figure out how to make Interface Builder connect multiple outlets to the same IBOutlet
/IBOutlet
Array.
有没有人想出如何做到这一点?
Has anyone figured out how to do this yet?
我知道我可以创建12个插座并以这种方式处理它,但我想尽可能地使这个工作尽可能地与演讲幻灯片中的示例相关。
I know that I can create 12 outlets and deal with it that way, but I'd like to make this work as closely as possible to the example in the lecture slides.
推荐答案
编辑
这已在Swift的后续Beta版本中修复 - 现在在界面构建器中的
IBCollection选项中。
This was fixed in a later Beta release of Swift - there's now in IBCollection option in the interface builder.
对于Swift的早期Beta版本:
我遇到了同样的问题:在Beta的发行说明中2您找到以下声明:
I came across the same problem: in the release notes of Beta 2 you find the following statement:
Interface Builder不支持在Swift类中声明插座集合
Interface Builder does not support declaring outlet collections in Swift classes
我通过以下方式解决了这个问题(易于定制):
I solved this the following way (easy to customize):
class CardGameViewController: UIViewController {
@lazy var cardButtons : UIButton[] = {
var tempBtn: UIButton[] = []
for v:AnyObject in self.view.subviews {
if v is UIButton {
tempBtn.append(v as UIButton)
}
}
return tempBtn
}()
...
基本上,它遍历所有子视图并检查是否有一个是一个UIButton。在这种情况下,它会被添加到临时数组中。然后,此临时数组用于延迟实例化cardButtons数组。有关所有详细信息,请查看: Matchismo:Objective-C to Swift
Basically, it loops through all the subviews and checks if one is a UIButton. In that case it gets added to a temporary array. This temporary array is then used to lazy instantiate the cardButtons array. For all details, check: Matchismo: Objective-C to Swift
这篇关于Swift - 相当于IBOutletCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!