ios Swift 将多个 IBOutlets 放入一个数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24805180/
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
Swift put multiple IBOutlets in an Array
提问by CorPruijs
I made these (marked with red border) IBOutlets using ctrl + drag
我使用这些(标有红色边框)IBOutlets ctrl + drag
But i don't like to have the exact same line 9 times (DRY)
但我不喜欢完全相同的行 9 次(干)
How do i put these IBOutlets in an Array?
我如何将这些 IBOutlets 放在一个数组中?
回答by holex
you can define a generic outlet collection in Swiftlike this:
你可以像这样在Swift 中定义一个通用的插座集合:
@IBOutlet var collectionOfViews: Array<UIView>? // = [UIView]?
or for e.g. UIButton
objects:
或例如UIButton
对象:
@IBOutlet var collectionOfButtons: Array<UIButton>? // = [UIButton]?
you can find your collections under the Outlet Collectionsgroup as usually are in the File's Owner:
您可以在Outlet Collections组下找到您的收藏,就像通常在File's Owner 中一样:
it would look on my console after connecting 5 random buttons:
连接 5 个随机按钮后,它会显示在我的控制台上:
回答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 (Views)
- 选择 ViewController(在故事板中)并打开连接检查器
- 连接检查器中有“出口集合”选项(您将在那里看到一系列出口)
- 如果与您的界面元素连接
-
——
class ViewController2: UIViewController {
@IBOutlet var collection:[UIView]!
override func viewDidLoad() {
super.viewDidLoad()
}
}
回答by hannesr
Solution here Swift - IBOutletCollection equivalent
此处的解决方案Swift - IBOutletCollection 等效
@IBOutlet var objectCollection: [Object]
@IBOutlet var objectCollection: [对象]
回答by Mark Ebert
Start with the two view pane where you see both your code and the storyboard. When you make your first IBOutlet connection from the UI to your code, just look carefully at the Connection drop down field and select the option called "Outlet Collection". This will automatically create an array of IBOutlets. Next just look for the little black circle within a circle that is placed in your code where the array is created. Just drag from this circle to all the other UI objects you want to connect to that same collection (not sure if you can mix types). Similarly you can connect all the objects to one Action by dragging from the first black dot created to all the other objects you want to wire up to that action. Also consider EnumerateSequence() to help in working with this Collection. Sweet right?
从两个视图窗格开始,您可以在其中看到代码和故事板。当您从 UI 到您的代码建立第一个 IBOutlet 连接时,只需仔细查看 Connection 下拉字段并选择名为“Outlet Collection”的选项。这将自动创建一个 IBOutlets 数组。接下来只需在放置在创建数组的代码中的圆圈内寻找小黑圆圈。只需从这个圆圈拖动到您想要连接到同一个集合的所有其他 UI 对象(不确定是否可以混合类型)。同样,您可以通过从创建的第一个黑点拖动到要连接到该动作的所有其他对象,将所有对象连接到一个动作。还可以考虑 EnumerateSequence() 来帮助处理此集合。甜吧?