Dodałem więc cele do moich IBActions, które stworzyłem, które występują, gdy wartość pola tekstowego się zmienia. Gdy te działania wystąpią, system powinien sprawdzić, czy oba pola tekstowe są liczbami całkowitymi. Ustawiłem dwie zmienne na false i ustawiono je na true, gdy oba są int. W IBActions mam instrukcje if, które mówią, że przycisk ma być włączony, jeśli obie zmienne zawierają liczby całkowite. Po uruchomieniu symulatora ten przycisk nie włącza się, gdy oba pola tekstowe zawierają liczbę całkowitą.Swift: Wartość Zmiana zdarzeń kontrolnych nie wywoływanie?
Jestem nowy na szybkie, więc jeśli to możliwe, proszę napisać cały kod i gdzie powinien znajdować się mój kod. Oto co mam do tej pory:
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
}
}
}
W drugiej próbce kodu zapomniałeś "n" o "EditingChanged". – adheus
@adheus - poprawiony, dzięki. – Piotr
@Piotr, ale to działa, gdy edytuję pole tekstowe za pomocą klawiatury. co powinienem zrobić, gdy muszę zmienić pole tekstowe za pomocą kliknięcia przycisku? – MHSFisher