Swift: UITextField IBAction - EditingChanged -按钮保持启用,即使textfield值被更改?

时间:2023-01-23 14:59:38

I have two IBActions for when two different text field's EditingChanged. Inside of these methods, I have an if statement that enables a button if two UITextFields both contain integers. The problem is, when the button becomes enabled, is stays enabled, even though if I edit the text field and change them so they have characters besides integers. How can I fix it? Please be thorough and clear when you provide the code because I am new to programming. Here's the code I have so far if you were wondering:

当两个不同的文本字段的编辑发生变化时,我有两个ibaction。在这些方法中,我有一个if语句,如果两个UITextFields都包含整数,则该语句启用一个按钮。问题是,当按钮启用时,仍然保持启用状态,即使我编辑文本字段并更改它们,使它们除了整数之外还有字符。我怎样才能修好它呢?当你提供代码的时候,请彻底和清晰,因为我是编程新手。如果你想知道的话,这是我到目前为止的密码:

@IBAction func calorieNumberEditingChanged(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 yourWeightEditingChanged(sender: AnyObject) {
    // If both variables are true and the text fields contain integers, enable button
    if self.yourWeightFilled && self.calorieNumberFilled {
        self.calculateButton.enabled = true
    }
}

2 个解决方案

#1


1  

This is similar to Evan's answer, but we can simplify the code.

这类似于Evan的答案,但是我们可以简化代码。

func validateCalculateButton() {
    self.calculateButton.enabled = 
        (self.yourWeightFilled && self.calorieNumberFilled)
}

@IBAction func calorieNumberEditingChanged(sender: AnyObject) {
    self.validateCalculateButton()
}

@IBAction func yourWeightEditingChanged(sender: AnyObject) {
    self.validateCalculateButton()
}

And truly, there's no need to have two separate @IBAction methods here. You could link both UI elements into the same method unless you need to do separate actions for each that you've not included in your code posted here.

实际上,这里不需要有两个单独的@IBAction方法。您可以将两个UI元素链接到相同的方法中,除非您需要为这里的代码中没有包含的每个元素执行单独的操作。

#2


0  

Try adding else statements and changing them back to not being enabled.

尝试添加else语句并将它们更改为未启用。

@IBAction func calorieNumberEditingChanged(sender: AnyObject) {
    // If both variables are true and the text fields contain integers, enable button
    if self.yourWeightFilled && self.calorieNumberFilled {
        self.calculateButton.enabled = true
    } else {
        self.calculateButton.enabled = false
    }
}
@IBAction func yourWeightEditingChanged(sender: AnyObject) {
    // If both variables are true and the text fields contain integers, enable button
    if self.yourWeightFilled && self.calorieNumberFilled {
         self.calculateButton.enabled = true
    } else {
        self.calculateButton.enabled = false
    }
}

#1


1  

This is similar to Evan's answer, but we can simplify the code.

这类似于Evan的答案,但是我们可以简化代码。

func validateCalculateButton() {
    self.calculateButton.enabled = 
        (self.yourWeightFilled && self.calorieNumberFilled)
}

@IBAction func calorieNumberEditingChanged(sender: AnyObject) {
    self.validateCalculateButton()
}

@IBAction func yourWeightEditingChanged(sender: AnyObject) {
    self.validateCalculateButton()
}

And truly, there's no need to have two separate @IBAction methods here. You could link both UI elements into the same method unless you need to do separate actions for each that you've not included in your code posted here.

实际上,这里不需要有两个单独的@IBAction方法。您可以将两个UI元素链接到相同的方法中,除非您需要为这里的代码中没有包含的每个元素执行单独的操作。

#2


0  

Try adding else statements and changing them back to not being enabled.

尝试添加else语句并将它们更改为未启用。

@IBAction func calorieNumberEditingChanged(sender: AnyObject) {
    // If both variables are true and the text fields contain integers, enable button
    if self.yourWeightFilled && self.calorieNumberFilled {
        self.calculateButton.enabled = true
    } else {
        self.calculateButton.enabled = false
    }
}
@IBAction func yourWeightEditingChanged(sender: AnyObject) {
    // If both variables are true and the text fields contain integers, enable button
    if self.yourWeightFilled && self.calorieNumberFilled {
         self.calculateButton.enabled = true
    } else {
        self.calculateButton.enabled = false
    }
}