Swift:按钮启用不起作用?

424

我的视图控制器中有一个禁用的按钮。当两个文本字段被编辑时,我具有IBAction。当两个文本字段都包含整数时,我正在尝试启用按钮。我曾尝试这样做,但是每当我运行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
    }
}

}

内特·库克(Nate Cook)

你的委托方法是有点混合起来-他们必须严格命名什么呼叫者预期,否则他们将不会被发现,所以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
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章