I want to use a for loop to set the titles for the button. What I am currently doing is not working. I thought using indexPath.row was working but it does not. The code is not compelling either. I would think there is a simple solution to implement the array.
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)
}
}
It prints as follows.
line pen graph save next