当选择一个按钮时,如何取消选择阵列中的所有其他按钮?

时间:2017-09-29 04:07:14

标签: ios arrays swift if-statement func

目前,当我在数组中选择一个按钮时,它将从灰色变为黑色,如果再次选择,则返回灰色,这是我想要的。问题是,当我选择一个按钮并选择另一个按钮时,它们都是黑色的。当我选择一个按钮然后选择另一个按钮时,我怎么能这样做呢?前一个按钮会变回灰色?

let subjectArray = ["Button1", "Button2", "Button3", "Button4"]   
for title in subjectArrary {
                let button = UIButton()
                button.backgroundColor = UIColor.white
                button.setTitle("\(title)", for: .normal)
                button.setTitleColor(UIColor.gray, for: .normal)
                button.heightAnchor.constraint(equalToConstant: 60).isActive = true
                button.widthAnchor.constraint(equalToConstant: 140).isActive = true
                button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)

                stack.addArrangedSubview(button)
            }



    func buttonPressed(sender:AnyObject) {

            guard let button = sender as? UIButton else { return }

            if !button.isSelected {
                button.isSelected = true
                button.setTitleColor(UIColor.black, for: .normal)
            } else {
                button.isSelected = false
                button.setTitleColor(UIColor.gray, for: .normal)
            }
        }

6 个答案:

答案 0 :(得分:0)

使用NotificationCenter.default.addObserver(...

  1. 定义自定义通知名称。
  2. 使用以下方法订阅所有按钮以收听通知:object
  3. 在按钮类的控制操作方法中,将通知self(即通知的发件人)设置为object
  4. 在通知中注册的通知处理程序方法中,检查通知的self属性。如果等于SELECT * FROM tableA a JOIN tableB b on a.itemId = ISNULL(b.idX, b.idY) ,请取消选择当前按钮。

答案 1 :(得分:0)

如果您有插座系列:

func buttonPressed(sender:AnyObject) {

        guard let currentButton = sender as? UIButton else { return }

        buttons.forEach{ (element) in
                if element == currentButton {
                      //change state and params for current button
                }else{
                     //change other buttons
                }
        }
    }

您可以检测选择了哪个

{{1}}

答案 2 :(得分:0)

当你创建按钮时,你可以将它们添加到像

这样的数组中
let bs = [UIButton]()    
for title in subjectArrary {
    let b = .......
    bs.append(b)
}

并在动作方法中

for b in bs {
    b.setTitleColor(.gray, for: .normal)
}
(sender as? UIButton).setTitleColor(.black, for: .normal)

答案 3 :(得分:0)

尝试创建按钮数组

var buttonArray = [UIButton]()


func buttonPressed(sender:AnyObject) {
    let _ = buttonArray.map({$0.isSelected = false})
    sender.isSelected = true
}

这将解决问题

答案 4 :(得分:0)

如果要在按下新按钮时重置其他按钮的状态,则可以使用以下代码,

awk 'NR==1{ m = $4; n = $5; l= $6; o = $7; p= $8}{ print $1,$2*m+$3*n+p+o-l }' yourfile

答案 5 :(得分:0)

// Create an array of buttons which will hold all of your buttons
// This is a class property, we can already initialise this with empty array so we don't need to set it during class init()
let buttons: [UIButton] = [] 

func createButton(withTitle title: String) -> UIButton {
    let button = UIButton()
    button.setTitle(title, for: .normal)
    button.setTitleColor(.gray, for: .normal)
    button.setTitleColor(.black, for: .selected) // Please take note I've added this line
    button.backgroundColor = .white
    button.widthAnchor.constraint(equalToConstant: 140).isActive = true
    button.heightAnchor.constraint(equalToConstant: 60).isActive = true
    button.addTarget(self, action: #selector(didTapButton(_:)), for: .touchUpInside)
    return button
}

func setupButtons() {
    let subjectArray = ["Button1", "Button2", "Button3", "Button4"]   

    for title in subjectArrary {
        let button = createButton(withTitle: title)
        buttons.append(button) // Append all your buttons
        stack.addArrangedSubview(button)
    }
}

func didTapButton(_ button: UIButton) {
    deselectAllButtons()
    button.isSelected = !button.isSelected // set/unset of button state happens here
}

func deselectAllButtons() {
    buttons.forEach { $0.isSelected = false }
}

选中此行后,会为按钮设置黑色标题颜色。

button.setTitleColor(.black, for: .selected)

对于更干净的代码,我已经在for循环中提取了代码,制作了一小块方法。 另外,无需将我的代码注释添加到您的实际代码中;)