2017-07-20 57 views
1

让说我有10 UIButton像下面单选择在一些`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但我仍坚持了几个小时。任何建议或答案都会对我有所帮助。在此先感谢

+0

你的意思是禁用选定的按钮,如果你按下一个按钮,它将被禁用,而其他人将是正常的? – Lawliet

+0

@Lawliet我的情况就像在UITableView中带有复选标记的单选一样。只有一个按钮可以突出显示。如果按下另一个按钮,则最后一个高亮按钮将不亮。 –

+0

@MarioMargoPradipta看到我的解决方案为您工作 –

回答

4

我认为这是很容易只是尝试

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

谢谢回答,但isSelected不会让我的按钮突出,禁用或等 –

+1

你也可以使用的IsEnabled,isHighlighted代替isSelected –

+0

@MarioMargoPradipta尝试,现在我编辑我的答案 –

1

您可以检查所有子视图按钮在你看来,unhighlight所有这些,亮点唯一的选择按钮。在这种情况下,我使用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 
    } 

如果你想突出/ unhighlighting后您的按钮的背景颜色改变,你打算申请本作一堆按钮,你可以重写为方便isHighlighted变量。

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

感谢您的回答,它不起作用。它仍然作出多选 –

+0

不用担心,请检查我的修改答案,以防您想要突出显示/不亮灯按钮。 – Lawliet

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