在一些`UIButton`中单选

时间:2017-07-20 03:22:12

标签: ios swift uibutton

假设我有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,但我仍然被卡住了。

4 个答案:

答案 0 :(得分:5)

我觉得这很容易尝试

@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 }
  */
}

答案 1 :(得分:2)

当按钮数量很大时,我建议改为跟踪最后选择的按钮。在所有现有按钮的基础上创建一个

var lastSelectedButton: UIButton()

然后在你的buttonDisplayAction做类似

的事情
@IBAction func buttonDisplayAction(_ sender: UIButton) {

    lastSelectedButton.isSelected = false //Plus any deselect logic for this button
    lastSelectedButton = sender //If any buttons are not affect by this selection logic exclude them here
    sender.isSelected = true
    ...
}

无论你有多少个按钮,这都是一样的。

此外,您可以使用titleColor(for:)更改.selected状态按钮的颜色。 .selected状态按钮的背景颜色默认为按钮的tintColor

答案 2 :(得分:1)

您可以在视图中将所有子视图检查为按钮,取消高亮显示所有这些按钮,并突出显示唯一选定的按钮。在这种情况下,我会在代码示例中使用backgroundColor

     @IBAction func buttonDisplayAction(_ sender: UIButton) {
        // ...

        for subView in view.subviews {            
            // Set all the other buttons as normal state
            if let button: UIButton = subView as? UIButton {
                button.isHighlighted = false
            }
        }
        // Highlight the current selected
        sender.isHighlighted = true
    }

如果您希望在突出显示/取消突出显示后按钮背景颜色发生变化,并且您打算将其应用于一堆按钮,则可以覆盖isHighlighted变量以方便使用。

extension UIButton {
    open override var isHighlighted: Bool {
        didSet {
            if isHighlighted {
                backgroundColor = .darkGray
            } else {
                backgroundColor = .clear
            }
        }
    }
}

答案 3 :(得分:1)

1。### swift4中的单个选择中的多个按钮

   class SignUpStep1VC: UIViewController {

        @IBOutlet weak var btnPersonal: UIButton!
        @IBOutlet weak var btnPersonal1: UIButton!


        @IBOutlet weak var btnCompany: UIButton!
        @IBOutlet weak var btnICO: UIButton!


        @IBOutlet weak var btnCompany1: UIButton!
        @IBOutlet weak var btnICO1: UIButton!

        var strAccountType:String = "Personal"
        var ButtonsArr = [(btn:UIButton,text:String)]()

        override func viewDidLoad() {
            super.viewDidLoad()
            btnNext.layer.cornerRadius = 20
            btnPersonal.isSelected = true
            ButtonsArr.append((btn: btnPersonal, text: "Personal"))
            ButtonsArr.append((btn: btnICO, text: "ICO"))
            ButtonsArr.append((btn: btnCompany, text: "Company"))
            ButtonsArr.append((btn: btnPersonal1, text: "Personal"))
            ButtonsArr.append((btn: btnICO1, text: "ICO"))
            ButtonsArr.append((btn: btnCompany1, text: "Company"))
    }

   @IBAction func btnSelect_click(_ sender: UIButton) {

        ButtonsArr.forEach{
            $0.btn.isSelected = false
        }

        strAccountType = ButtonsArr[sender.tag].text
        ButtonsArr[sender.tag].btn.isSelected = true

    }

    }