我正在尝试更改textColor。虽然我正确地将数字输入到文本字段中,但它仅适用于“其他”条件但不适用于“if”。这是我的代码。
任何人都可以解决此问题。谢谢
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var questionOne: UILabel!
@IBOutlet weak var fieldOne: UITextField! // textFields should be set as labels
@IBOutlet weak var questionTwo: UILabel!
@IBOutlet weak var fieldTwo: UITextField!
@IBOutlet weak var questionThree: UILabel!
@IBOutlet weak var fieldThree: UITextField!
@IBOutlet weak var questionFour: UILabel!
@IBOutlet weak var fieldFour: UITextField!
@IBAction func submitButton(sender: UIButton) {
fieldOneColorChange()
fieldTwoColorChange()
fieldThreeColorChange()
fieldFourColorChange()
}
@IBAction func resetButton(sender: UIButton) {
}
// Text color changes
func fieldOneColorChange() {
if fieldOne == 1 {
questionOne.textColor = UIColor.redColor()
} else {
questionOne.textColor = UIColor.blueColor()
}
}
func fieldTwoColorChange() {
if fieldTwo == 2 {
questionTwo.textColor = UIColor.brownColor()
} else {
questionTwo.textColor = UIColor.redColor()
}
}
func fieldThreeColorChange(){
if fieldThree == 3 {
questionThree.textColor = UIColor.blueColor()
} else {
questionThree.textColor = UIColor.purpleColor()
}
}
func fieldFourColorChange(){
if fieldFour == 4 {
questionFour.textColor = UIColor.blueColor()
} else {
questionFour.textColor = UIColor.redColor()
}
}
答案 0 :(得分:1)
像这样更改你的if语句
if fieldOne.text=="1"
分别为每个if语句