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

Use a for loop to set the titles for buttons

我想使用 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 = [=10=].titleLabel?.text {
       print(title)
    }
}

打印如下

行 钢笔 图形 救球 下一个