-1

如果用户没有选择要发布的图片,我正在尝试禁用我的发布按钮,到目前为止,我只在文本字段没有被编辑的情况下才设法禁用它。我也有两个按钮,按钮一:丢失,按钮二:找到。用户也需要点击其中一个。如果这两个按钮都没有被点击,是否也可以禁用发布按钮?

到目前为止,这是我的代码!

func handleBlancInformation(){
    address.addTarget(self, action: #selector(PostViewController.textFieldDidChange), for: UIControlEvents.editingChanged)
    breed.addTarget(self, action: #selector(PostViewController.textFieldDidChange), for: UIControlEvents.editingChanged)
    phone.addTarget(self, action: #selector(PostViewController.textFieldDidChange), for: UIControlEvents.editingChanged)
}

@objc func textFieldDidChange() {
    guard let address = address.text, !address.isEmpty, let breed = breed.text, !breed.isEmpty, let phone = phone.text, !phone.isEmpty
        else {
        postButton.setTitleColor(UIColor.lightText, for: UIControlState.normal)
            postButton.isEnabled = false
            return
    }
    postButton.setTitleColor(UIColor.white, for: UIControlState.normal)
    postButton.isEnabled = true
}
4

1 回答 1

0
func handleBlancInformation(){
    address.addTarget(self, action: #selector(PostViewController.textFieldDidChange), for: .valueChanged)
    breed.addTarget(self, action: #selector(PostViewController.textFieldDidChange), for: .valueChanged)
    phone.addTarget(self, action: #selector(PostViewController.textFieldDidChange), for: .valueChanged)
}

试试这个代码我已经改变了 addTarget 事件,所以它会工作:)

于 2018-08-07T03:42:02.273 回答