2016-10-17 146 views
0

我可以用按钮操纵各种标签质量,但是我想通过按下重置按钮来更改多个按钮的颜色。不能为我的生活得到任何代码工作。我的示例: firstButton是白色文本。 secondButton是白色文本。 当我按下resetButton时,我想firstButton和secondButton将文本颜色更改为红色。Swift 3通过按下另一个按钮来更改按钮的颜色

Swift 3,xcode 8.

PS;使用IBAction时,按下按钮的颜色时不会改变颜色。

+0

你有没有试图使按钮成一组,然后遍历通过他们改变按钮时,按下重置? –

+0

我可以,但setTitleColor命令不起作用,除了使用它自己的IBAction上的发件人。 – rodderco

回答

2

通过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) 
     ... 
    } 
} 
+0

这个例程是否能用setTitleColor工作? – rodderco

+0

也就是说。 resetButton.setTitleColor(UIColor.red,用于:UIControlState.normal) – rodderco

+0

谢谢shallowThought,你肯定让我朝着正确的方向发展。我有代码工作。我发现有多个错误。 Swift 3新手,感谢您的大力帮助。 – rodderco

1

这工作:

//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) 
     } 
    } 
+0

这对你的问题有帮助吗? –

+0

我可能会在该程序的第二次迭代中尝试此操作。为了提高效率,我真的应该使用循环遍历命令。目前我已经分别编制了每一步;不是最好的,但我得到它的工作。非常感谢您的投入。 – rodderco

相关问题