Swift: value Change management events does not cause?

So, I have added goals to my IBActions that I created, which occur when the value of the text field changes. When these actions occur, the system must check whether both text fields are integer. I set the two variables to false and they are set to true when both of them are int. In IBActions, I have instructions that say that a button will be enabled if both variables contain integers. When I run the simulator, this button does not turn on when both text fields contain an integer.

I am new to fast, so if possible, write all the code and where it should be in my code. Here is what I still have:

import UIKit class ViewController: UIViewController, UITextFieldDelegate { @IBOutlet weak var calculatorButton: UIButton! @IBOutlet weak var inspirationLabel: UILabel! @IBOutlet weak var beginningLabel: UILabel! @IBOutlet weak var calculatorContainer: UIView! @IBOutlet weak var answer1Label: UILabel! @IBOutlet weak var doneButton: UIButton! @IBOutlet weak var yourWeightTextField: UITextField! @IBOutlet weak var calorieNumberTextField: UITextField! @IBOutlet weak var menuExampleButton: UIButton! @IBOutlet weak var aboutButton: UIButton! @IBOutlet weak var calculateButton: UIButton! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib yourWeightTextField.delegate = self calorieNumberTextField.delegate = self calculateButton.enabled = false // Calling the textfield valueChanged Methods yourWeightTextField.addTarget(self, action:"yourWeightValueChanged:", forControlEvents:.ValueChanged); calorieNumberTextField.addTarget(self, action:"calorieNumberValueChanged:", forControlEvents:.ValueChanged); } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func calculatorButtonTapped(sender: AnyObject) { calculatorContainer.hidden = false inspirationLabel.hidden = true beginningLabel.hidden = true menuExampleButton.hidden = true aboutButton.hidden = true } var yourWeightFilled = false var calorieNumberFilled = false func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { // Find out what the text field will be after adding the current edit let text = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string) // If the textfields have the properties of the function if textField == yourWeightTextField { yourWeightFilled = text.toInt() != nil } else if textField == calorieNumberTextField { calorieNumberFilled = text.toInt() != nil } return true } func textFieldShouldReturn(textField: UITextField) -> Bool { textField.resignFirstResponder(); return true; } // The methods to close the keyboard when editing is finished @IBAction func yourWeightEditingDidEnd(sender: AnyObject) { yourWeightTextField.resignFirstResponder() } @IBAction func calorieNumberEditingDidEnd(sender: AnyObject) { calorieNumberTextField.resignFirstResponder() } @IBAction func yourWeightValueChanged(sender: AnyObject) { // If both variables are true and the text fields contain integers, enable button if self.yourWeightFilled && self.calorieNumberFilled { self.calculateButton.enabled = true } } @IBAction func calorieNumberValueChanged(sender: AnyObject) { // If both variables are true and the text fields contain integers, enable button if self.yourWeightFilled && self.calorieNumberFilled { self.calculateButton.enabled = true } } } 
+5
source share
2 answers

You should look for the EditingChaged event, not ValueChanged

EDIT: I mean moving from:

 yourWeightTextField.addTarget(self, action:"yourWeightValueChanged:", forControlEvents:.ValueChanged); calorieNumberTextField.addTarget(self, action:"calorieNumberValueChanged:", forControlEvents:.ValueChanged); 

to:

 yourWeightTextField.addTarget(self, action:"yourWeightValueChanged:", forControlEvents:.EditingChanged); calorieNumberTextField.addTarget(self, action:"calorieNumberValueChanged:", forControlEvents:.EditingChanged); 

You are just looking for the wrong event.

+9
source

If you are looking for an event changed the text , then right-click in the text box Select Editing ended from Sent Events . You can see the circle on the right end, click on the circle Hold Down Ctrl and drag it into the ViewController file . What is the action you want and Click connect . For this, I have provided several screenshots. Here I call Action TextChanged

I am using Xcode 7 Swift 2 here

Right click on the text box and you will see something like this enter image description here enter image description here

Finally, you can see the TextChanged Created event. when you enter something into the text box and click on the return of this event.

+2
source

Source: https://habr.com/ru/post/1207163/


All Articles