Swift3 Swift 3通过按下另一个按钮来更改按钮的颜色

Swift3 Swift 3通过按下另一个按钮来更改按钮的颜色,swift3,Swift3,我可以通过按钮操纵各种标签质量,但我想通过按下重置按钮来更改多个按钮的颜色。我一辈子都不能用任何代码 我的例子是: 第一个按钮是白色文本。 第二个按钮是白色文本。 当我按下resetButton时,我希望firstButton和secondButton将文本颜色更改为红色 Swift 3,xcode 8 聚苯乙烯;当按下按钮时,我可以使用iAction更改按钮颜色。通过IBOutlet使用每个按钮的属性,这样您就可以随时访问它们: class myClass { @IBOutlet we

我可以通过按钮操纵各种标签质量,但我想通过按下重置按钮来更改多个按钮的颜色。我一辈子都不能用任何代码

我的例子是: 第一个按钮是白色文本。 第二个按钮是白色文本。 当我按下resetButton时,我希望firstButton和secondButton将文本颜色更改为红色

Swift 3,xcode 8


聚苯乙烯;当按下按钮时,我可以使用iAction更改按钮颜色。

通过IBOutlet使用每个按钮的属性,这样您就可以随时访问它们:

class myClass {
    @IBOutlet weak var resetButton: UIButton!
    @IBOutlet weak var myButton: UIButton!
    @IBOutlet weak var my2ndButton: UIButton!

    @IBAction func didPressResetButton(_ sender: AnyObject) {
        myButton.backgroundColor = UIColor.green
        myButton.setTitle("Hello Tint", for: .normal)
        ...
        resetButton.setTitleColor(UIColor.red, for: .normal)
        myButton.setTitleColor(UIColor.red, for: .normal)
        ...
    }
}

通过
IBOutlet
s为每个按钮使用属性,以便您可以随时访问它们:

class myClass {
    @IBOutlet weak var resetButton: UIButton!
    @IBOutlet weak var myButton: UIButton!
    @IBOutlet weak var my2ndButton: UIButton!

    @IBAction func didPressResetButton(_ sender: AnyObject) {
        myButton.backgroundColor = UIColor.green
        myButton.setTitle("Hello Tint", for: .normal)
        ...
        resetButton.setTitleColor(UIColor.red, for: .normal)
        myButton.setTitleColor(UIColor.red, for: .normal)
        ...
    }
}
这项工作:

    //link the buttons to change here
    @IBOutlet var buttonsToChange: [UIButton]!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func resetPressed(_ sender: UIButton)
    {

        for b in buttonsToChange
        {
            b.setTitleColor(UIColor.red, for: .normal)
        }
    }
这项工作:

    //link the buttons to change here
    @IBOutlet var buttonsToChange: [UIButton]!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func resetPressed(_ sender: UIButton)
    {

        for b in buttonsToChange
        {
            b.setTitleColor(UIColor.red, for: .normal)
        }
    }

你有没有试过将按钮组合成一组,然后在按下重置键时反复更改按钮?我可以这样做,但setTitleColor命令不起作用,除了在自己的iAction上使用sender之外。您是否尝试过将按钮组成一个组,然后在按下重置按钮时对其进行迭代更改?我可以这样做,但setTitleColor命令不起作用,除了在自己的iAction上使用sender之外。resetButton.setTitleColor(UIColor.red,用于:UIControlState.normal)谢谢你,我想,你肯定把我带到了正确的方向。我有密码要用。我发现了多个错误。Swift 3新手,非常感谢您的大力帮助。不客气。与其在评论中说“谢谢”,不如接受答案。resetButton.setTitleColor(UIColor.red,用于:UIControlState.normal)谢谢你,我想,你肯定把我带到了正确的方向。我有密码要用。我发现了多个错误。Swift 3新手,非常感谢您的大力帮助。不客气。与其在评论中说“谢谢”,你最好接受答案。我可能会在程序的第二次迭代中尝试这一点。为了提高效率,我真的应该使用循环来迭代命令。目前,我已经分别对每个步骤进行了编程;不是最好的,但我成功了。非常感谢您的输入。我可能会在程序的第二次迭代中尝试这个。为了提高效率,我真的应该使用循环来迭代命令。目前,我已经分别对每个步骤进行了编程;不是最好的,但我成功了。非常感谢你的投入。