繁体   English   中英

一次只激活一个按钮

[英]Make only one button active at a time

我正在基于x提取的数据数量动态创建x个按钮。

我想一次只激活一个按钮。 假设有3个名为ABC的按钮。 当用户在A处于活动状态时轻按B时,我想停用A并使B处于活动状态。

像这样在jQuery中很容易实现

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

如何在Swift中执行此操作?

我正在创建具有以下内容的按钮:

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()添加所有按钮标题

其他人如何在Swift中执行此按钮切换效果?

我相信您需要的是在iOS中实现单选按钮。

可以使用UITableView轻松完成。 如果您的要求是在UIScrollView中使用UIButton,则可以将这些UIButton对象存储在Array中并访问它们以达到效果。

假设您有三个UIButton A,B,C。在将它们添加到数组中之前,将其属性设置为.Selected和.Normal状态。 您可以将其中之一设置为初始状态。 另外,为其设置唯一标签以供参考

在创建这些按钮时,将它们添加到一个数组中(假设按钮数组)。 然后在func filterByCategory(sender:UIButton)中,执行以下操作

func filterByCategory(sender:UIButton) {

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

PS:如果您想了解使用UITableView进行此操作的方法,可以在评论中ping我

建议使用变量保存对当前按钮的引用。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM