本文介绍了如何突出一个UIButton的背景,当它被点击和" unhighlight"它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建4 UIButtons 的突出和强调保持被点击时。唯一的问题是我只需要一个的UIButton在一个时间内突出。所以,如果有一个的UIButton 突出了,我需要它是未突出显示,并突出了的UIButton 我点击。我曾经尝试过这样做,失败了。请帮我解决这个问题。
我现在用的是雨燕的编码语言来做到这一点。

I am trying to create 4 UIButtons that highlight and stay highlighted when they are clicked. The only problem is I need only one UIButton to be Highlighted at a time. So, if there is a UIButton highlighted already, I need it to be "unhighlighted" and highlight the UIButton I clicked. I have tried to do this before and failed. Please help me with this problem.I am using the Swift coding language to do this.

任何输入或建议将不胜AP preciated。

Any input or suggestions would be greatly appreciated.

推荐答案

如果你给这个答案的给予好评,记得要给予好评dasblikenlight的回答也是如此。

If you give this answer an upvote, remember to upvote dasblikenlight's answer as well.

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
    }

}

这篇关于如何突出一个UIButton的背景,当它被点击和" unhighlight"它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:44