答案:
button.setTitle("All", for: .normal)
或
button.setTitle("All", for: [])
通常是不正确的,因为它仅在某些状态的标题尚未设置时才有效。例如:
button.setTitle("N", for: .normal)
button.setTitle("HL", for: .highlighted)
button.setTitle("All", for: .normal)
在此代码按钮之后,突出显示状态的标题仍为“HL”。
因此,在一般情况下,要更改所有使用状态的标题,您必须遍历所有这些状态:
let states: [UIControl.State] = [.normal, .highlighted, .selected, [.highlighted, .selected]]
for state in states {
button.setTitle("All", for: state)
}
(如果您使用其他状态,例如 .disabled,您还必须将它们的组合添加到循环中)
注意:UIControl.State 是一组选项,因此将标题设置为:
button.setTitle("All", for: [.selected, .highlighted])
不为选中状态和高亮状态设置标题“All”,而是为同时选中和高亮d的组合状态设置标题“All”。