如何使用 Swift 在 UIViewController 中循环视图出口? [英] How to loop through view outlets in a UIViewController with Swift?
问题描述
我想知道,是否可以使用 Swift 遍历 UIViewController
的所有出口.
I'm wondering, if it's possible to loop through all the outlets of a UIViewController
with Swift.
具体来说,我想检查是否每个文本字段都由用户填写.
Specifically, I want to check if every textfield is filled by the user.
推荐答案
这就是 Outlet Collections 的用途.在 InterfaceBuilder 的同一个 Outlet 集合中拖动所有文本字段,并在类文件中创建一个 @IBOutlet
到该集合:
This is what Outlet Collections are for. Drag all your textfields in the same Outlet Collection in InterfaceBuilder and create an @IBOutlet
to that collection in your class file:
要在 InterfaceBuilder 中创建插座集合,请在辅助编辑器中从第一个 UITextField
按住 ctrl 拖动到您的类文件.然后选择Outlet Collection:
To create the outlet collection in InterfaceBuilder, ctrl-drag from the first UITextField
to your class file in the assistant editor. Then choose Outlet Collection:
ctrl 拖动下一个 UITextField
在那个 @IBOutlet
上将它添加到集合中:
ctrl-drag the next UITextField
on that @IBOutlet
to add it to the collection:
对所有 textField 重复此操作.
Repeat that for all your textFields.
@IBOutlet var textFields: [UITextField]!
func checkTextFields() {
for textField in self.textFields {
... // do your checks
}
}
这篇关于如何使用 Swift 在 UIViewController 中循环视图出口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!