Terril320
Terril320

Reputation: 9

Change textcolor for UITextField

I am trying to change the textColor.Though I enter the numbers correctly into the textfields,it is working only for "else" condition but not for 'if'. Here is my code.

Can anyone fix this issue. Thanks

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()

    } 
}

Upvotes: 0

Views: 90

Answers (1)

UdayM
UdayM

Reputation: 1783

Change your if statement like this

if fieldOne.text=="1"

so on for every if statement respectively

Upvotes: 1

Related Questions