Swift:按钮启用不起作用?

时间:2014-11-17 03:30:35

标签: swift uibutton int uitextfield boolean

我的视图控制器中禁用了一个按钮。当编辑两个文本字段时,我有IBActions。当两个文本字段都包含整数时,我试图启用该按钮。我试图这样做,但每当我运行ios模拟器时,即使我将整数放入每个文本字段,该按钮也会保持禁用状态。为什么要保持残疾?我是新手,所以请帮助我。这是我整个项目的代码:

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
}

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
}

@IBAction func yourWeightEditingDidEnd(sender: AnyObject) {
    yourWeightTextField.resignFirstResponder()
}

@IBAction func calorieNumberEditingDidEnd(sender: AnyObject) {
    calorieNumberTextField.resignFirstResponder()
}

var yourWeightFilled = false
var calorieNumberFilled = false

func yourWeightTextFieldValueValidInt(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    // Find out what the text field will be after adding the current edit
    let text = (yourWeightTextField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)

    if let intVal = text.toInt() {
        self.yourWeightFilled = true
    } else {
        self.yourWeightFilled = false
    }
    return true
}
func calorieNumberTextFieldValueValidInt(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    // Find out what the text field will be after adding the current edit
    let text = (calorieNumberTextField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)

    if let intVal = text.toInt() {
        self.calorieNumberFilled = true
    } else {
        self.calorieNumberFilled = false
    }
    return true
}

@IBAction func yourWeightTextFieldEdited(sender: AnyObject) {
    if self.yourWeightFilled && self.calorieNumberFilled {
        self.calculateButton.enabled = true
    }
}

@IBAction func calorieNumberTextFieldEdited(sender: AnyObject) {
    if self.yourWeightFilled && self.calorieNumberFilled {
        self.calculateButton.enabled = true
    }
}

}

1 个答案:

答案 0 :(得分:0)

您的委托方法有点混淆 - 它们必须完全按照调用者的预期命名,否则将无法找到它们,因此yourWeightTextFieldValueValidInt()calorieNumberTextFieldValueValidInt()不会被调用所有。相反,您需要在单个方法中处理两个文本字段的编辑:

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 textField == yourWeightTextField {
        yourWeightFilled = text.toInt() != nil
    } else if textField == calorieNumberTextField {
        calorieNumberFilled = text.toInt() != nil
    }

    return true
}