Home > Blockchain >  How to activate a button when a condition is met?
How to activate a button when a condition is met?

Time:08-29

I have a button that should be activated after the user selects a photo from library or take a photo. But as long as there is no photo the button should not work.

CodePudding user response:

Before the user selects a photo from library or take a photo

button.isEnabled = false

After the user selects a photo from library or take a photo

button.isEnabled = true

CodePudding user response:

UIButton has a property called isEnabled (refer to Apple Docs).

To enable the button you can write:

myButton.isEnabled = true

More detailed explanation: First of all you need to disable the button, when the ViewController is loaded. Ideally you do this in the viewWillAppear function:

override func viewWillAppear(_ animated: Bool) {
  myButton.isEnabled = false
  super.viewWillAppear(animated)
}

And when the photo is loaded you can enable the button:

func selectPhoto() {
  // your code to select the photo
  myButton.isEnabled = true
}
  • Related