简体   繁体   中英

How to activate a button when a condition is met?

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.

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
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM