0

I am trying to make a button inactive until buttons have been pressed and their background image has been changed. I started to use the same code from a similar question in regards to textfields. I am getting a lot of errors as I expected since I don't know what to change certain code to but here it is:

override func viewDidLoad() {
    super.viewDidLoad()
                     
    requestPaymentButton.layer.cornerRadius = 20
    requestPaymentButton.isEnabled = false
    [story1Button, story2Button].forEach({ $0.addTarget(self, action: #selector(imageChanged), for: .editingChanged) })
    
                         }

@objc func imageChanged(_ button: UIButton) {
    if button.currentBackgroundImage == nil {
        if button.currentBackgroundImage ==  {
            textField.text = ""
            return
        }
    }
    guard
        let story1 = story1Button.currentBackgroundImage, // (story1 has no background image),
        let story2 = story2Button.currentBackgroundImage, (//story2 has no background image)
    else {
        requestPaymentButton.isEnabled = false
        return
    }
     requestPaymentButton.isEnabled = true
}

I have code that changes each of the buttons and removes their initial image so it just shows the image. I want the button to become active after each of these button has their background image changed. Any help would be awesome.

EDIT:

Here is the code for how the images are getting picked:

 internal func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    
    
    if let image = info[UIImagePickerController.InfoKey(rawValue: "UIImagePickerControllerOriginalImage")] as? UIImage
    {
        
        if flag == 1
        {
            
            story1Button.setBackgroundImage(image, for: .normal)
            story1Button.setImage(UIImage.init(named: "Default"), for: .normal)
        }
        else if flag == 2
        {
            
            story2Button.setBackgroundImage(image, for: .normal)
            story2Button.setImage(UIImage.init(named: "Default"), for: .normal)
        }
        else if flag == 3
        {
            
            story3Button.setBackgroundImage(image, for: .normal)
            story3Button.setImage(UIImage.init(named: "Default"), for: .normal)
            }
        else if flag == 4
        {
            
            analytics1Button.setBackgroundImage(image, for: .normal)
            analytics1Button.setImage(UIImage.init(named: "Default"), for: .normal)
            }
        else if flag == 5
        {
            
            analytics2Button.setBackgroundImage(image, for: .normal)
            analytics2Button.setImage(UIImage.init(named: "Default"), for: .normal)
            }
        else if flag == 6
        {
            
            analytics3Button.setBackgroundImage(image, for: .normal)
            analytics3Button.setImage(UIImage.init(named: "Default"), for: .normal)
            }
        }
    
         self.dismiss(animated: true, completion: nil)
    
}
CoderCoop
  • 79
  • 5
  • Its hard to follow what you exactly want. You show a lot of code with variables that don't even exist (`textField`, `habit`, `goal` etc.) Can you reduce your code to two buttons and two text fields or whatever and describe how they interact (why `isEmpty` etc.)? – Andreas Oetjen Jul 29 '20 at 21:03
  • I edited it. I want a button to be inactive until a few buttons have an image uploaded to their background image. – CoderCoop Jul 29 '20 at 21:24
  • Here is a link to the same concept but with textfields: https://stackoverflow.com/questions/34941069/enable-a-button-in-swift-only-if-all-text-fields-have-been-filled-out – CoderCoop Jul 29 '20 at 21:35
  • So where is the code that changes the background image? That is the place where you need to check if all buttons have one. – Andreas Oetjen Jul 30 '20 at 05:15
  • I edited the code to show the function. I can show when the buttons are tapped but I don't think it applies. – CoderCoop Jul 30 '20 at 17:16

1 Answers1

1

I would suggest the following:

  • Remove all the forEach / addTarget handler stuff from viewDidLoad
  • Remove func imageChanged

In imagePickerController, call a checking method to check if all buttons have an image, like the following:

internal func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    if let image = info[UIImagePickerController.InfoKey(rawValue: "UIImagePickerControllerOriginalImage")] as? UIImage
    {
       // same as above, if if if ...
        
        checkImages()
    }
    self.dismiss(animated: true, completion: nil)
}

private func checkImages() {
    if story1Button.currentBackgroundImage != nil &&
        story2Button.currentBackgroundImage != nil 
        /* ... */ {
        requestPaymentButton.isEnabled = true
    } else {
         requestPaymentButton.isEnabled = false
    }
}
Andreas Oetjen
  • 9,889
  • 1
  • 24
  • 34