Swift 4-UITextField DidBeginEditing

时间:2018-08-22 10:43:50

标签: uibutton uitextfield

我有四个文本字段(PISARadius,aliasingVelocity,AIVMax和AIVTI)和两个UIButton(calcEROAButton和regurgVolumeButton)。

仅在PISARadius,aliasingVelocity和AIVMax具有数字输入且所有四个文本字段均已输入regurgVolumeButton时,我才尝试启用calcEROAButton。

我在以下代码上取得了一些成功,但是当我清除其中一个文本字段时,有时按钮不会禁用。同样,在相关文本字段已填充并且用户单击其他位置之前,这些按钮似乎不会变为启用/禁用状态。有什么明显的补救办法?感谢任何输入!

@IBOutlet weak var PISARadius: UITextField!
@IBOutlet weak var aliasingVelocity: UITextField!
@IBOutlet weak var AIVMax: UITextField!
@IBOutlet weak var AIVTI: UITextField!
@IBOutlet weak var EROAAnswer: UILabel!
@IBOutlet weak var RegurgVolumeAnswer: UILabel!
@IBOutlet weak var calcEROAButton: UIButton!
@IBOutlet weak var regurgVolumeButton: UIButton!

@IBAction func calcEROA(_ sender: UIButton)
{
    EROAAnswer.text = String(Int(PISARadius.text!)! + Int(aliasingVelocity.text!)! + Int(AIVMax.text!)!)
}

@IBAction func calcRegurgVolume(_ sender: UIButton)
{
    RegurgVolumeAnswer.text = String(Int(PISARadius.text!)! + Int(aliasingVelocity.text!)! + Int(AIVMax.text!)! + Int(AIVTI.text!)!)
}

override func viewDidLoad()
{
    super.viewDidLoad()
    calcEROAButton.isEnabled = false
    regurgVolumeButton.isEnabled = false
    PISARadius.delegate = self
    aliasingVelocity.delegate = self
    AIVMax.delegate = self
    AIVTI.delegate = self

}

func textFieldDidBeginEditing(_ textField: UITextField) {

    if ((PISARadius.text?.isEmpty == true) || (aliasingVelocity.text?.isEmpty == true) || (AIVMax.text?.isEmpty == true)){
        if(AIVMax.text?.isEmpty == true)
        {
            self.regurgVolumeButton.isEnabled = false
        }
        self.calcEROAButton.isEnabled = false

    }

    if ((PISARadius.text?.isEmpty == false) && (aliasingVelocity.text?.isEmpty == false) && (AIVMax.text?.isEmpty == false) && (AIVTI.text?.isEmpty == false)){
        self.regurgVolumeButton.isEnabled = true

    }
    else if ((PISARadius.text?.isEmpty == false) && (aliasingVelocity.text?.isEmpty == false) && (AIVMax.text?.isEmpty == false)){
        self.calcEROAButton.isEnabled = true

    }
    return
}

1 个答案:

答案 0 :(得分:0)

我认为,如果您使用自定义方法来检测文本字段输入,则可能会解决您的问题。

  override func viewDidLoad()
    {
        super.viewDidLoad()
        calcEROAButton.isEnabled = false
        regurgVolumeButton.isEnabled = false
        PISARadius.delegate = self
        aliasingVelocity.delegate = self
        AIVMax.delegate = self
        AIVTI.delegate = self

    // Add this selector to each of your textfield
    self.your_Textfield.addTarget(self, action: #selector(self.textFieldDidChange(textField:)), for: .allEvents)

    }

    // Any of the textfield changes this method will trigger
    @objc func textFieldDidChange(textField: UITextField) {
        // Add your logic 
    }