2014-11-18 11 views
5

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 
    } 
} 
} 

Odpowiedz

9

Należy szukać EditingChaged razie nie ValueChanged

EDIT: Chodzi mi o to, aby zmienić:

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

do:

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

Po prostu szukasz niewłaściwej wigilii nt.

+0

W drugiej próbce kodu zapomniałeś "n" o "EditingChanged". – adheus

+0

@adheus - poprawiony, dzięki. – Piotr

+0

@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

2

Jeśli szukasz Zmieniono tekst imprezy, następnie Right Click w polu tekstowymwybrać Montaż skończyła z Wysłane Wydarzenia. Możesz zobaczyć okrąg po prawej stronie koniec kliknij kółko Przytrzymaj Ctrl i przeciągnij go do pliku ViewController. Nazwij działanie, którego chcesz: i Click connect. Dostarczyłem do tego kilka zrzutów ekranu. Tu i nazwa działania TextChanged

Używam Xcode 7 Swift 2 tutaj

prawym przyciskiem myszy na polu tekstowym i można zobaczyć coś takiego enter image description hereenter image description here

Wreszcie można zobaczyć Utworzono zdarzenie TextChanged. kiedy wpisujesz coś w polu tekstowym i klikniesz return to zdarzenie zostanie wywołane.

Powiązane problemy