且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

斯威夫特:按钮启用不工作?

更新时间:2023-10-26 13:15:40

您委托方法是有点混合起来 - 他们必须精确地命名为呼叫者所期待的,否则他们不会被人发现,所以 yourWeightTextFieldValueValidInt() calorieNumberTextFieldValueValidInt()不被称为可言。相反,你需要处理编辑的两个的单一方法文本字段:

Your delegate methods are a bit mixed up -- they have to be named exactly what the caller expects, or they won't be found, so yourWeightTextFieldValueValidInt() and calorieNumberTextFieldValueValidInt() aren't being called at all. Instead you'll need to handle the edits to both text fields in a single method:

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
}