使用for循环来设置按钮的标题



我想使用for循环来设置按钮的标题。我目前正在做的事情不起作用。我认为使用indexPath.row是有效的,但它没有。该准则也没有说服力。我认为有一个简单的解决方案可以实现数组。

var line = UIButton()
var pen = UIButton()
var graph = UIButton()
var save = UIButton()
var nxt = UIButton()
let btnTitles = ["line","pen","graph","save","next"]
var increase = 0.1
for b in [line, pen, graph,save,nxt] {
b.setTitle(btnTitles(IndexPath.row), for: .normal)
}

let line = UIButton()
let pen = UIButton()
let graph = UIButton()
let save = UIButton()
let nxt = UIButton()
let buttons: [UIButton] = [line, pen, graph, save, nxt]
let btnTitles = ["line", "pen", "graph", "save", "next"]
for i in 0..<buttons.count {
let button = buttons[i]
let title = btnTitles[i]
button.setTitle(title, for: .normal)
}
// probing code //
buttons.forEach {
if let title = $0.titleLabel?.text {
print(title)
}
}

它打印如下。

线路笔图表拯救下一个

相关内容

  • 没有找到相关文章

最新更新