2014-11-18 9 views
5

Quindi ho aggiunto obiettivi alle mie IBAzioni che ho creato che si verificano quando il valore di un campo di testo cambia. Quando si verificano queste azioni, il sistema dovrebbe verificare se i due campi di testo sono entrambi interi. Ho impostato due variabili impostate su false e sono impostate su true quando entrambe sono int. In IBActions, ho istruzioni if ​​che dicono che un pulsante deve essere abilitato se entrambe le variabili contengono numeri interi. Quando eseguo il simulatore, questo pulsante non si attiva quando entrambi i campi di testo contengono un numero intero.Swift: valore che modifica gli eventi di controllo che non chiamano?

Sono nuovo di swift, quindi se possibile, si prega di scrivere tutto il codice e dove dovrebbe essere nel mio codice. Ecco quello che ho finora:

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

risposta

9

Si dovrebbe cercare EditingChaged caso, non ValueChanged

EDIT: Quello che voglio dire è quello di cambiare da:

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

a:

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

Stai semplicemente cercando la vigilia sbagliata nt.

+0

Hai dimenticato un 'n' su 'ModificaChanged' nel secondo esempio di codice. – adheus

+0

@adheus - risolto, grazie. – Piotr

+0

@Piotr ma funziona solo quando sto modificando il campo di testo con la tastiera. cosa devo fare quando ho bisogno di cambiare campo di testo con un clic del pulsante? – MHSFisher

2

Se siete alla ricerca di un testo cambiato evento, quindi Right Click sul campo di testo selezionare Editing è finita dalle eventi inviati. È possibile vedere un cerchio a destra fine fare clic sul cerchio Tenere premuto Ctrl e trascinarlo sul file ViewController. Denominare l'azione desiderata e Click connect. Ho fornito alcune schermate per questo. Ecco i nome d'azione TextChanged

Sto usando Xcode 7 Swift 2 qui

tasto destro del mouse sulla casella di testo e si può vedere qualcosa di simile enter image description hereenter image description here

Infine Potete vedere il Evento TextChanged creato. quando si digita qualcosa su una casella di testo e si fa clic su restituisci questo evento si attiva.

Problemi correlati