[英]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 ). UIButton 有一个名为
isEnabled
的属性(请参阅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.更详细的解释:首先你需要禁用按钮,当 ViewController 加载时。 Ideally you do this in the
viewWillAppear
function:理想情况下,您在
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.