I want to try to disable a post button until an image has been chosen by the user.
However, I have a default image set as a placeholder.I have already managed to disable the post button when a textfield has not filled with information. I haven't, however, found a way to disable the button immediately.Hopefully I have explained myself.
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,
let image = imagePosted.image, !imagePosted.isEqual("placeholder.png")
else {
postButton.setTitleColor(UIColor.lightText, for: UIControlState.normal)
postButton.isEnabled = false
return
}
postButton.setTitleColor(UIColor.white, for: UIControlState.normal)
postButton.isEnabled = true
}
I would need more code to give an exact solution here, but think about it this way:
Whatever method you are using for the user to choose a new image, that is where you should be enabling the "post" button.
For example, assuming you are using a UIImagePickerViewController to handle picking an image, you will want to enable the "post" button inside of this method, which is part of the UIImagePickerControllerDelegate:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
//Enable button here
} else {
//Disable button here
}
}