【发布时间】:2015-01-15 03:08:14
【问题描述】:
当文本字段包含整数时,我有两个标志属性应该更改,并且我有 IBActions,当文本字段编辑结束时,会更改标志。当两个变量都为真时,这些方法应该启用一个按钮。我运行了 iOS 模拟器,但按钮未启用。我还为两个文本字段声明了文本字段委托。
我是 swift 新手,所以请清楚你的答案。另外,我没有设置任何断点。这是我目前所拥有的代码:
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 textField == yourWeightTextField {
yourWeightFilled = text.toInt() != nil
} else if textField == calorieNumberTextField {
calorieNumberFilled = text.toInt() != nil
}
return true
}
@IBAction func yourWeightEditingDidEnd(sender: AnyObject) {
if self.yourWeightFilled && self.calorieNumberFilled {
self.calculateButton.enabled = true
}
yourWeightTextField.resignFirstResponder()
}
@IBAction func calorieNumberEditingDidEnd(sender: AnyObject) {
if self.yourWeightFilled && self.calorieNumberFilled {
self.calculateButton.enabled = true
}
calorieNumberTextField.resignFirstResponder()
}
【问题讨论】:
标签: variables swift int uitextfield boolean