0

我有两个 TextField,我想检查它们是否为空。一个是通过键盘输入获取数据,另一个是从 PickerView 获取数据。

在将第二个 TextField 从键盘输入更改为 PickerView 输入之前,我使用了它:

func setupAddTargetIsNotEmptyTextFields() {
    saveCupboardButton.isHidden = true //hidden okButton
    saveViewButtonBorder.isHidden = true
    addCupboardTextField.addTarget(self, action: #selector(textFieldsIsNotEmpty),
                                for: .editingChanged)
    addCupboardTypeTextField.addTarget(self, action: #selector(textFieldsIsNotEmpty),
                                 for: .editingChanged)

}

@objc func textFieldsIsNotEmpty(sender: UITextField) {
    sender.text = sender.text?.trimmingCharacters(in: .whitespaces)

    guard
        let name = addCupboardTextField.text, !name.isEmpty,
        let type = addCupboardTypeTextField.text, !type.isEmpty
        else
    {
        self.saveCupboardButton.isHidden = true
        self.saveViewButtonBorder.isHidden = true
        return
    }
    // enable okButton if all conditions are met
    saveCupboardButton.isHidden = false
    saveViewButtonBorder.isHidden = false
}

这很好用,但后来我添加了 PickerView:

func createPicker() {

    //        toolbar
    let toolbar = UIToolbar()
    toolbar.sizeToFit()

    //        doneButton
    let done = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
    toolbar.setItems([done], animated: false)

    addCupboardTypeTextField.inputAccessoryView = toolbar
    addCupboardTypeTextField.inputView = picker
}


@objc func donePressed() {
    addCupboardTypeTextField.text = pick
    self.view.endEditing(true)
}

因为我的原因,addTarget .editing changed当 PickerView 设置数据时,它不会检查是否有任何更改。有没有办法解决这个问题?提前致谢

4

0 回答 0