目前,当我在数组中选择一个按钮时,它将从灰色变为黑色,如果再次选择,则返回灰色,这是我想要的。问题是,当我选择一个按钮并选择另一个按钮时,它们都是黑色的。当我选择一个按钮然后选择另一个按钮时,我怎么能这样做呢?前一个按钮会变回灰色?
let subjectArray = ["Button1", "Button2", "Button3", "Button4"]
for title in subjectArrary {
let button = UIButton()
button.backgroundColor = UIColor.white
button.setTitle("\(title)", for: .normal)
button.setTitleColor(UIColor.gray, for: .normal)
button.heightAnchor.constraint(equalToConstant: 60).isActive = true
button.widthAnchor.constraint(equalToConstant: 140).isActive = true
button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
stack.addArrangedSubview(button)
}
func buttonPressed(sender:AnyObject) {
guard let button = sender as? UIButton else { return }
if !button.isSelected {
button.isSelected = true
button.setTitleColor(UIColor.black, for: .normal)
} else {
button.isSelected = false
button.setTitleColor(UIColor.gray, for: .normal)
}
}
答案 0 :(得分:0)
使用NotificationCenter.default.addObserver(...
。
object
。 self
(即通知的发件人)设置为object
。self
属性。如果不等于SELECT *
FROM tableA a
JOIN tableB b on a.itemId = ISNULL(b.idX, b.idY)
,请取消选择当前按钮。答案 1 :(得分:0)
如果您有插座系列:
func buttonPressed(sender:AnyObject) {
guard let currentButton = sender as? UIButton else { return }
buttons.forEach{ (element) in
if element == currentButton {
//change state and params for current button
}else{
//change other buttons
}
}
}
您可以检测选择了哪个
{{1}}
答案 2 :(得分:0)
当你创建按钮时,你可以将它们添加到像
这样的数组中let bs = [UIButton]()
for title in subjectArrary {
let b = .......
bs.append(b)
}
并在动作方法中
for b in bs {
b.setTitleColor(.gray, for: .normal)
}
(sender as? UIButton).setTitleColor(.black, for: .normal)
答案 3 :(得分:0)
尝试创建按钮数组
var buttonArray = [UIButton]()
func buttonPressed(sender:AnyObject) {
let _ = buttonArray.map({$0.isSelected = false})
sender.isSelected = true
}
这将解决问题
答案 4 :(得分:0)
如果要在按下新按钮时重置其他按钮的状态,则可以使用以下代码,
awk 'NR==1{ m = $4; n = $5; l= $6; o = $7; p= $8}{ print $1,$2*m+$3*n+p+o-l }' yourfile
答案 5 :(得分:0)
// Create an array of buttons which will hold all of your buttons
// This is a class property, we can already initialise this with empty array so we don't need to set it during class init()
let buttons: [UIButton] = []
func createButton(withTitle title: String) -> UIButton {
let button = UIButton()
button.setTitle(title, for: .normal)
button.setTitleColor(.gray, for: .normal)
button.setTitleColor(.black, for: .selected) // Please take note I've added this line
button.backgroundColor = .white
button.widthAnchor.constraint(equalToConstant: 140).isActive = true
button.heightAnchor.constraint(equalToConstant: 60).isActive = true
button.addTarget(self, action: #selector(didTapButton(_:)), for: .touchUpInside)
return button
}
func setupButtons() {
let subjectArray = ["Button1", "Button2", "Button3", "Button4"]
for title in subjectArrary {
let button = createButton(withTitle: title)
buttons.append(button) // Append all your buttons
stack.addArrangedSubview(button)
}
}
func didTapButton(_ button: UIButton) {
deselectAllButtons()
button.isSelected = !button.isSelected // set/unset of button state happens here
}
func deselectAllButtons() {
buttons.forEach { $0.isSelected = false }
}
选中此行后,会为按钮设置黑色标题颜色。
button.setTitleColor(.black, for: .selected)
对于更干净的代码,我已经在for循环中提取了代码,制作了一小块方法。 另外,无需将我的代码注释添加到您的实际代码中;)