2017-07-20 61 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