0

我有一个 UIButtons 出口集合:

@IBOutlet var categoriesButtonLabels: [UIButton]!

每个按钮都有一个不同的标签(在故事板中设置)。

我想用一个字符串数组(我从我的 FireBase 数据库的代码中的其他地方检索到的类别)来更改他们的标题。

我试过这样的事情:

override func viewDidLoad() {
    super.viewDidLoad()
// Setting Category buttons labels
    for button in categoriesButtonLabels {
        for i in categories {
            button.setTitle("\(i)", for: .normal)
        }
    }
}

但它只获取类别数组的最后一个值并将所有按钮的标题设置为相同......我做错了什么?

为了完整起见:这是我的类别数组:

for (index, value) in categories.enumerated() {
print("\(index) = \(value)")
}

和出口收集:

for (index, value) in categoriesButtonLabels.enumerated() {
print("\(index) = \(value)")
}

输出:

类别字符串数组是:0 = 体育类别字符串数组是:1 = 科学类别字符串数组是:2 = 电影类别字符串数组是:3 = 音乐类别字符串数组是:4 = 历史

Outlet UIButtons Collection 为:0 => Outlet UIButtons Collection 为:1 => Outlet UIButtons Collection 为:2 => Outlet UIButtons Collection 为:3 => Outlet UIButtons Collection 为:4 = >

4

1 回答 1

1

删除内循环:

for (i, button) in categoriesButtonLabels.enumerated() {
    button.setTitle("\(categories[i])", for: .normal)
}
于 2016-12-16T23:10:28.377 回答