如何在点击UIButton时突出显示UIButton的背景并“取消它”?

时间:2015-06-28 13:12:49

标签: swift background uibutton highlight

我正在尝试创建4 UIButtons突出显示并在点击时保持突出显示。唯一的问题是我只需要一次突出显示一个UIButton。因此,如果已经突出显示UIButton,我需要将其“突出显示”并突出显示我点击的UIButton。我之前尝试过这样做并且失败了。请帮我解决这个问题。 我使用Swift编码语言来做这件事。

非常感谢任何意见或建议。

3 个答案:

答案 0 :(得分:2)

如果你给这个答案一个upvote,请记住也支持dasblikenlight的答案。

class ViewController: UIViewController {

    // Connect all 4 buttons to this outlet
    @IBOutlet var radioGroup: [UIButton]!

    // Connect this action to all 4 buttons
    @IBAction func radioGroupClicked(sender: AnyObject) {
        // Unhighlight all buttons
        unhighlightRadioGroup()

        // Highlight the one being clicked on
        highlightRadioGroup(sender as! UIButton)
    }

    // Set all 4 buttons in unselected state
    func unhighlightRadioGroup() {
        for button in radioGroup {
            button.selected = false
        }
    }

    // Set one button in the selected state
    func highlightRadioGroup(button : UIButton) {
        button.selected = true
    }

}

答案 1 :(得分:1)

您可以使用IBOutletCollection执行此操作。按住Command键将其中一个按钮拖动到视图控制器代码中,然后选择在下拉列表中创建IBOutletCollection,并为您的收藏集命名,例如radioGroup。然后控制 - 将其余三个按钮拖动到相同的IBOutletCollection

接下来要添加一种方法来取消突出显示radioGroup中的所有按钮。这可以通过简单的循环完成。

最后,从按钮的事件处理程序添加对unhighlightRadioGroup的调用。事件处理程序应首先调用您的unhighlightRadioGroup方法,然后突出显示事件处理程序中收到的sender

答案 2 :(得分:0)

lazy var buttonsArray: [UIButton] = {
    var buttons = [UIButton]()
    let firstButton = UIButton()
    let secondButton = UIButton()
    let thirdButton = UIButton()
    let fourthButton = UIButton()

    buttons = [firstButton, secondButton, thirdButton, fourthButton]
    return buttons
}()   

private func setupButtonMethods() {
    filteredButtons[0].addTarget(self, action: #selector(firstButtonPressed(sender:)), for: .touchUpInside)
    filteredButtons[1].addTarget(self, action: #selector(secondButtonPressed(sender:)), for: .touchUpInside)
    filteredButtons[2].addTarget(self, action: #selector(thirdButtonPressed(sender:)), for: .touchUpInside)
    filteredButtons[3].addTarget(self, action: #selector(fourthButtonPressed(sender:)), for: .touchUpInside)
}

private func setupHiglightedStateOnButton(button: UIButton) {
    for btn in buttonsArray {
        btn.isSelected = false
        btn.backgroundColor = .gray
        btn.setTitleColor(.white, for: .normal)
        btn.isUserInteractionEnabled = true
    }
    button.isSelected = true
    button.backgroundColor = .yellow
    button.setTitleColor(.black, for: .normal)
    button.isUserInteractionEnabled = false
}

@objc func firstButtonPressed(sender: UIButton) {
    setupHiglightedStateOnButton(button: sender)
}

@objc func secondButtonPressed(sender: UIButton) {
    setupHiglightedStateOnButton(button: sender)
}

@objc func thirdButtonPressed(sender: UIButton) {
    setupHiglightedStateOnButton(button: sender)
}

@objc func fourthButtonPressed(sender: UIButton) {
    setupHiglightedStateOnButton(button: sender)
}
  

记得在viewDidLoad()

中调用setupButtonMethods()