4

I'm currently having problems for my label to read the addition of 3 textfield values automatically, without a button function action. As such i only want my textfield to be an Int input only. There's a screenshot attached below for better reference. Appreciate those who can help me with this. Thanks!

ViewController

import UIKit

class TryingoutController: UIViewController {
    @IBOutlet var impact: UITextField!
    @IBOutlet var rigour: UITextField!
    @IBOutlet var response: UITextField!
    @IBOutlet var total: UILabel!

enter image description here

rmaddy
  • 298,130
  • 40
  • 468
  • 517
Kinja
  • 433
  • 5
  • 16

1 Answers1

5

One way is to add self as the target to the text fields, for the control event .editingChanged

impact.addTarget(self, action: #selector(textChanged), for: .editingChanged)
// do the same for other textfields 

Then declare a textChanged method. This should handle what happens when the texts in the text fields change. One implementation would be to add up all the values in the text fields (if any, and is valid) and display it in the label.

func textChanged() {
    let impactValue = Int(impact.text!)
    let rigourValue = Int(rigour.text!)
    let responseValue = Int(response.text!)
    total.text = String(describing: 
        (impactValue ?? 0) + (rigourValue ?? 0) + (responseValue ?? 0)
    )
}

Optionally, you can conform to UITextFieldDelegate:

class TryingoutController: UIViewController, UITextFieldDelegate {

}

and implement shouldChange according to this answer by Thuggish Nuggets. Then, set the delegates of the text fields to self:

impact.delegate = self
// do the same for other text fields.
Sweeper
  • 145,870
  • 17
  • 129
  • 225
  • `response.text ?? "0"` does that help ? Also may be its helpful if `shouldChange` delegate is implemented to restrict character set to numbers. – GoodSp33d Jan 05 '18 at 06:53
  • you can do that with this answer - https://stackoverflow.com/questions/12944789/allow-only-numbers-for-uitextfield-input/12944946#12944946 – Skywalker Jan 05 '18 at 06:55
  • @Skywalker Thanks, added a link to that answer. – Sweeper Jan 05 '18 at 07:03
  • Thanks it worked! but 1 last thing, how can i validate my "impact" textfield? so that the user can only input from 1 - 80? – Kinja Jan 05 '18 at 07:08
  • @Kinja You will have to use if statements to check whether the input is in range. And output an appropriate message using the label if any of them is out of range. – Sweeper Jan 05 '18 at 07:10
  • and what would be the appropriate code for the if statement to check? i tried if (impact.text != 1..80) { } but it doesn't work. – Kinja Jan 05 '18 at 07:15
  • @Kinja You should check `impactValue` instead. e.g. `impactValue >= 1 && impactValue <= 80`. – Sweeper Jan 05 '18 at 07:16
  • @Sweeper btw mate, there's an error "Use of unresolved identifier 'impactValue'" when using impactValue – Kinja Jan 08 '18 at 03:11
  • @Kinja I think you should do some research first, then ask another question. – Sweeper Jan 08 '18 at 06:43