gpt4 book ai didi

ios - 如何使用 Swift 从多个按钮中一次仅突出显示一个按钮

转载 作者:行者123 更新时间:2023-12-02 04:45:25 25 4
gpt4 key购买 nike

就我而言,我正在尝试使用单个操作IBOutlet和标签选项创建多个按钮。在这里,我需要一次仅从多个按钮中选择一个按钮来突出显示单击的按钮。如何实现这一目标?

我的代码

@IBAction private func buttonAction(_ sender: UIButton) {

if let button = sender as? UIButton {
// here I need to do selection highlights at a time only one button from four buttons
}

// To differentiate different buttons
switch (sender.tag) {
case 0:
print(sender.title(for: .normal)!)
case 1:
print(sender.title(for: .normal)!)
case 2:
print(sender.title(for: .normal)!)
case 3:
print(sender.title(for: .normal)!)
default:
print(sender.title(for: .normal)!)
}
}

最佳答案

如果您知道所有按钮标签,那么您可以像这样实现,

@IBAction private func buttonAction(_ sender: UIButton) {
// Create a list of all tags
let allButtonTags = [1, 2, 3, 4, 5]
let currentButtonTag = sender.tag

allButtonTags.filter { $0 != currentButtonTag }.forEach { tag in
if let button = self.view.viewWithTag(tag) as? UIButton {
// Deselect/Disable these buttons
button.backgroundColor = #colorLiteral(red: 0.80803, green: 0.803803, blue: 0.805803, alpha: 1)
button.setTitleColor(UIColor.darkGray, for: .normal)
button.isSelected = false
}
}
// Select/Enable clicked button
sender.backgroundColor = #colorLiteral(red: 0.1843137255, green: 0.6823529412, blue: 0.9764705882, alpha: 1)
sender.setTitleColor(UIColor.white, for: .normal)
sender.isSelected = !sender.isSelected
}

关于ios - 如何使用 Swift 从多个按钮中一次仅突出显示一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58538249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com