Swift中的IBOutlets数组

时间:2015-08-23 09:49:18

标签: swift

我有像

这样的代码
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var countryField: UITextField!
@IBOutlet weak var cityField: UITextField!

如何将所有这些项目放入数组中,以便我可以循环访问每个项目?

1 个答案:

答案 0 :(得分:1)

@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var countryField: UITextField!
@IBOutlet weak var cityField: UITextField!

...

let array = [emailField, countryField, cityField]

查看documentation on collection types

编辑:您可以拆分该数组的声明/ init

// outside any method
var array = [UITextField]?

// in viewDidLoad
self.array = [emailField, countryField, cityField]