-1

I am trying to disable my post button if the user has not selected a picture to post, so far, I've only managed to disable it if the textfields have not been edited. I also have two buttons, button one: lost, button two: found. The user needs to tap one or the other as well. Is it possible to also disable the post button if neither of these two buttons have been tapped?

Here's my code so far!

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
}
  • show the code for selecting an image – Malleswari Aug 07 '18 at 03:39
  • Buttons are not "selectable" so what exactly are we talking about? – matt Aug 07 '18 at 04:14
  • @objc func handleSelectPhoto() { let pickerController = UIImagePickerController() pickerController.delegate = self pickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary pickerController.allowsEditing = true self.present(pickerController, animated: true, completion: nil) } – Ximena Flores de la Tijera Aug 07 '18 at 16:05
  • extension PostViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate { func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { print("did Finish Picking Media") if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage { selectedImage = image imagePosted.image = image } self.dismiss(animated: true, completion: nil) } } – Ximena Flores de la Tijera Aug 07 '18 at 16:05
  • hey matt! sorry, tapped. But my question remains, is it possible to disable the button until one of the two buttons is tapped? – Ximena Flores de la Tijera Aug 07 '18 at 16:06

1 Answers1

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

try this code i have changed addTarget event so it will work :)

Mahesh Dangar
  • 806
  • 5
  • 11
  • Hey Mahesh! Thanks for the reply. The code i had already worked, but my question was how to also disable the post button until both a picture has been selected, and a button has been tapped? Best regards :) – Ximena Flores de la Tijera Aug 07 '18 at 16:09