【问题标题】:Swift: Button Enabling Not Working?Swift:按钮启用不起作用?
【发布时间】:2014-11-17 03:30:34
【问题描述】:

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

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
    }
}

}

【问题讨论】:

    标签: swift uibutton int uitextfield boolean


    【解决方案1】:

    您的委托方法有点混乱——它们必须完全按照调用者的期望命名,否则将找不到它们,因此根本不会调用 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
    }
    

    【讨论】:

    • 好吧,这更有意义。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多