2016-04-28 82 views
1

我基於x個獲取的數據動態創建x個按鈕數。一次只激活一個按鈕

而且我想一次只激活一個按鈕。 假設有3個按鈕,分別命名爲A,B,C。 當用戶點擊一個是積極的,我想停用一個,使活躍。

這是容易實現的jQuery中這樣

$(this).toggleClass('checked').siblings().removeClass('checked');

我怎樣才能在斯威夫特做到這一點?

我創建的按鈕具有以下:

let frame1 = CGRect(x: 10, y: 6, width: 50, height: 30) 
let button1 = UIButton(frame: frame1) 
let attributedTitle = NSAttributedString(string: "\(distinctCategories[i])".uppercaseString, attributes: [NSKernAttributeName: 1.0, NSForegroundColorAttributeName: UIColor.blackColor()]) 
button1.setAttributedTitle(attributedTitle, forState: .Normal) 
button1.titleLabel!.font = UIFont(name: "HelveticaNeue", size: 13.0) 
button1.layer.borderWidth = 2.0 
button1.layer.borderColor = UIColor.blackColor().CGColor 
button1.backgroundColor = UIColor.whiteColor() 
button1.addTarget(self, action: "filterByCategory:", forControlEvents: UIControlEvents.TouchUpInside) 
self.categoryScrollView.addSubview(button1) 

和目標的行動,我有:

func filterByCategory(sender:UIButton) { 

    if sender.backgroundColor != UIColor.blackColor() { 

     let attributedTitle = NSAttributedString(string: "\((sender.titleLabel?.text)!)".uppercaseString, attributes: [NSKernAttributeName: 1.0, NSForegroundColorAttributeName: UIColor.whiteColor()]) 
     sender.setAttributedTitle(attributedTitle, forState: .Selected) 
     sender.backgroundColor = UIColor.blackColor() 
    } else { 

     let attributedTitle = NSAttributedString(string: "\((sender.titleLabel?.text)!)".uppercaseString, attributes: [NSKernAttributeName: 1.0, NSForegroundColorAttributeName: UIColor.blackColor()]) 
     sender.setAttributedTitle(attributedTitle, forState: .Normal) 
     sender.backgroundColor = UIColor.whiteColor() 
    } 
} 

我只顯示在這裏爲簡潔風格的變化。 目前我只處理髮件人按鈕樣式的切換,而不是切換同級按鈕。

要反轉兄弟按鈕的樣式,我試過在上面的if里加入這個。

for button in categoryScrollView.subviews { 
    if let button = button as? UIButton { 
     let attributedTitle = NSAttributedString(string: " ", attributes: [NSKernAttributeName: 1.0, NSForegroundColorAttributeName: UIColor.blackColor()])  
     button.setAttributedTitle(attributedTitle, forState: .Normal) 
     button.backgroundColor = UIColor.whiteColor() 
    } 
} 

但我不知道如果我這樣做的權利,因爲我需要在這個目標函數再次添加所有按鈕標題爲setAttributedTitle()

如何其他人在做的雨燕這個按鈕切換效果?

+4

第一:存儲所創建的按鈕的陣列。然後:在'filterByCategory'裏面遍歷數組,「停用」每個不是「sender」的按鈕,並「激活」sender。 – luk2302

回答

7

我相信你需要的是iOS中單選按鈕的實現。

這可以使用UITableView輕鬆完成。如果您的要求是在UIScrollView中使用UIButtons,那麼您可以將這些UIButton對象存儲在一個數組中並訪問它們以實現該效果。

假設您有三個UIButton A,B,C。在將它們添加到數組中之前,將它們的屬性設置爲.Selected和.Normal狀態。您可以將其中一個Active設置爲初始狀態。另外,爲他們設置獨特的標籤

在創建這些按鈕時,將它們添加到一個數組(比方說按鈕數組)。然後在FUNC filterByCategory(發件人:UIButton的),請執行下列操作

func filterByCategory(sender:UIButton) { 

    sender.selected = true 
    for item in array { 
     if item.tag != sender.tag { 
      item.selected = false 
     } 
    } 
} 

PS:您可以ping我的評論,如果你想知道使用一個變量做這個用的UITableView

+0

您也可以在迭代後移動第一行來使用它,而不使用標籤:'arrayOfButtons.forEach {$ 0.selected = false}; sender.selected = true'。 – Eendje

+0

謝謝。我不知道設置.Selected和.Normal狀態會自動應用適當的樣式。 –

+0

如果您設置了您想要的屬性.Selected和.Normal狀態正確,那麼它將自動應用。您也可以通過在循環中設置item.backgroundcolor來更改背景顏色 – KrishnaCA

2

建議的方法保持對當前按鈕的引用。

好處是:沒有按鈕陣列,也沒有重複循環。

  • 創建一個UIButton變量currentButton

    var currentButton : UIButton! 
    
  • 編寫一個方法來重置當前屬性並設置所選按鈕的屬性。

    func selectButton(button: UIButton) { 
        if button === currentButton { return }   
    
        // add the code to reset the attributes of 'currentButton' 
    
        // add the code to set the attributes of 'button' 
    
        currentButton = button 
    } 
    
  • 創建按鈕之後加上選擇屬性其中之一,並設置相應currentButton

  • 當一個按鈕被點擊調用該方法

    func filterByCategory(sender:UIButton) { 
        selectButton(sender) 
    } 
    
+0

謝謝。這看起來像另一種可靠的方法。如果狀態(.Normal和.Selected)被設置,UIButton會自動應用不同的樣式嗎?如果需要額外的工作來定義.Normal和.Selected樣式,涉及的過程是什麼? –

+0

試一試。在函數中,兩個註釋行是佔位符,用於將「未選定」屬性應用於當前按鈕,將「已選擇」屬性應用於剛纔點擊的按鈕,如問題中的「if」 - 「else」子句。 – vadian