gpt4 book ai didi

ios - 单选某段 `UIButton`

转载 作者:搜寻专家 更新时间:2023-11-01 05:49:53 24 4
gpt4 key购买 nike

假设我有 10 个 UIButton,如下所示

enter image description here

我想实现单选,但我不知道该怎么做。条件如下:

首先,ALL 按钮会在选定的位置(突出显示等)。当我单击另一个按钮,如 VCU 按钮时,VCU 按钮将突出显示,而 ALL 按钮将处于正常状态(未突出显示)。

代码片段:

@IBAction func buttonDisplayAction(_ sender: UIButton) {
if sender == self.buttonAll {
self.currNr = -1
self.filteredDataRoomStatus = self.dataRoomStatus
} else if sender == self.buttonVCU {
self.currNr = 1
self.isButtonSelected = true
self.filteredDataRoomStatus = filterArray(dataRoomStatus, keywords: "zistatus", searchStr: "\(self.currNr)", equalFlag: true)
} else if sender == self.buttonOC {
self.currNr = 5
self.isButtonSelected = true
self.filteredDataRoomStatus = filterArray(dataRoomStatus, keywords: "zistatus", searchStr: "\(self.currNr)", equalFlag: true)
} else if sender == self.buttonVCC {
self.currNr = 0
self.isButtonSelected = true
self.filteredDataRoomStatus = filterArray(dataRoomStatus, keywords: "zistatus", searchStr: "\(self.currNr)", equalFlag: true)
} else if sender == self.buttonVD {
self.currNr = 2
self.isButtonSelected = true
self.filteredDataRoomStatus = filterArray(dataRoomStatus, keywords: "checkout zistatus", searchStr: "0 2", equalFlag: false)
} else if sender == self.buttonED {
self.currNr = 3
self.isButtonSelected = true
self.filteredDataRoomStatus = filterArray(dataRoomStatus, keywords: "zistatus", searchStr: "\(self.currNr)", equalFlag: true)
} else if sender == self.buttonOD {
self.currNr = 4
self.isButtonSelected = true
self.filteredDataRoomStatus = filterArray(dataRoomStatus, keywords: "zistatus", searchStr: "\(self.currNr)", equalFlag: true)
} else if sender == self.buttonCO {
self.currNr = 12
self.isButtonSelected = true
self.filteredDataRoomStatus = filterArray(dataRoomStatus, keywords: "checkout", searchStr: "1", equalFlag: true)
} else if sender == self.buttonDnD {
self.currNr = 8
self.isButtonSelected = true
self.filteredDataRoomStatus = filterArray(dataRoomStatus, keywords: "zistatus", searchStr: "\(self.currNr)", equalFlag: true)
} else if sender == self.buttonOOO {
self.currNr = 6
self.isButtonSelected = true
self.filteredDataRoomStatus = filterArray(dataRoomStatus, keywords: "zistatus", searchStr: "\(self.currNr)", equalFlag: true)
}

do {
let currButton: UIButton = (sender as UIButton)
currButton.isSelected = !currButton.isSelected

if currButton.isSelected {
currButton.backgroundColor = UIColor.darkGray
} else {
currButton.backgroundColor = UIColor.clear
}
}
}

上面的代码进行了多选,但我需要单选。我读过thisthis但我还是卡住了。

最佳答案

我觉得很简单,试试吧

@IBAction func onClickButton(_ sender: UIButton) {
deselectAllButtons()
sender.isSelected = true
}
func deselectAllButtons(){
for subView in view.subviews
{
// Set all the other buttons as normal state
if let button = subView as? UIButton {
button.isSelected = false
}
}
//Or you can simply do write above for loop code with one line
/*
view.subviews.forEach { ($0 as? UIButton)?.isSelected = false }
*/
}

关于ios - 单选某段 `UIButton`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45204535/

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