【发布时间】:2015-02-21 18:26:43
【问题描述】:
所以我试图允许用户通过单击一个调用方法来更改所有按钮颜色的按钮来更改我的应用程序的配色方案。我的按钮调用了这个方法:
@IBAction func changeColor(sender: UIButton) {
if sender.currentTitle == "Green" {
buttonOne.backgroundColor = UIColor.greenColor()
buttonTwo.backgroundColor = UIColor.greenColor()
buttonThree.backgroundColor = UIColor.greenColor()
buttonFour.backgroundColor = UIColor.greenColor()
buttonFive.backgroundColor = UIColor.greenColor()
buttonSix.backgroundColor = UIColor.greenColor()
buttonSeven.backgroundColor = UIColor.greenColor()
buttonEight.backgroundColor = UIColor.greenColor()
buttonNine.backgroundColor = UIColor.greenColor()
buttonClear.backgroundColor = UIColor.greenColor()
buttonPlus.backgroundColor = UIColor.greenColor()
buttonMin.backgroundColor = UIColor.greenColor()
buttonEq.backgroundColor = UIColor.greenColor()
buttonX.backgroundColor = UIColor.greenColor()
buttonDiv.backgroundColor = UIColor.greenColor()
buttonSet.backgroundColor = UIColor.greenColor()
}
}
但是当我运行应用程序并单击按钮更改颜色时,它给了我一个“exc bad instruction”错误并将其打印到控制台:“致命错误:在展开可选值 (lldb) 时意外发现 nil”
如何更改按钮的颜色而不导致此错误?如果我添加一个“!”到最后解开也不起作用的可选项。有什么建议吗?
【问题讨论】:
-
您可能需要重新连接您的 ibaction