1

I have found this answer How to check text field input at real time?

This is what I am looking for. However I am having trouble actually implementing this code. Also my current geographical location makes googling almost impossible.

I want to be able to change the background color of the next text field if the correct number is entered into the previous text field. textfieldTwo background color will change to green if the correct value is entered in textFieldOne. If the value is incorrect then nothing will happen. Please help me out. I have two text fields called textFieldOne and textFieldTwo and nothing else in the code.

Community
  • 1
  • 1
Jonathan
  • 11
  • 2

1 Answers1

0

Just pop this in your main view controller in an empty project (try using iphone 6 on the simulator)

import UIKit    

class ViewController: UIViewController {

    var txtField:UITextField!
    var txtFieldTwo:UITextField!
    var rightNumber = 10

    override func viewDidLoad() {
        super.viewDidLoad()

        //txtFieldOne
        var txtField = UITextField()
        txtField.frame = CGRectMake(100, 100, 200, 40)
        txtField.borderStyle = UITextBorderStyle.None
        txtField.backgroundColor = UIColor.blueColor()
        txtField.layer.cornerRadius = 5
        self.view.addSubview(txtField)

        //txtFieldTwo
        var txtFieldTwo = UITextField()
        txtFieldTwo.frame = CGRectMake(100, 150, 200, 40)
        txtFieldTwo.borderStyle = UITextBorderStyle.None
        txtFieldTwo.backgroundColor = UIColor.blueColor()
        txtFieldTwo.layer.cornerRadius = 5
        self.view.addSubview(txtFieldTwo)

        txtField.addTarget(self, action: "checkForRightNumber", forControlEvents: UIControlEvents.AllEditingEvents)

        self.txtField = txtField
        self.txtFieldTwo = txtFieldTwo

    }

    func checkForRightNumber() {
        let number:Int? = self.txtField.text.toInt()
        if number == rightNumber {
            self.txtFieldTwo.backgroundColor = UIColor.greenColor()
        } else {
            self.txtFieldTwo.backgroundColor = UIColor.blueColor()
        }
    }

}

EDIT: Adding a version with IBOutlets and IBActions

Note that in this example the IBAction is connected to txtFieldOne on Sent Events / Editing Changed

Also, make sure your Text Fields border colors are set to None. In the storyboard, the way to do this is to choose the left most option with the dashed border around it. That's so you can color the backgrounds. You can use layer.cornerRadius to set the roundness of the border's edges.

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var txtField: UITextField!
    @IBOutlet weak var txtFieldTwo: UITextField!

    var rightNumber = 10

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func checkForRightNumber(sender: AnyObject) {
        let number:Int? = self.txtField.text.toInt()
        if number == rightNumber {
            self.txtFieldTwo.backgroundColor = UIColor.greenColor()
        } else {
            self.txtFieldTwo.backgroundColor = UIColor.blueColor()
        }

    }

}
Usernumbernine
  • 276
  • 1
  • 8
  • I wanted to do this inside @IBAction func textFieldOne(sender: UItextField) { – Jonathan Aug 20 '15 at 07:28
  • Sorry can't access stackoverflow via OSX, only able to use via IOS. In China now. – Jonathan Aug 20 '15 at 07:29
  • Jonathan I did everything there programmatically so you could just copy and past to see how it works. I just posted and edited version with the code for how to do it with IBOutlets and IBActions – Usernumbernine Aug 20 '15 at 13:30