一次只激活一个按钮

问题描述:

我基于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

我相信你需要的是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

建议的方法保持对当前按钮的引用。

好处是:没有按钮阵列,也没有重复循环。

  • 创建一个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