简体   繁体   English

满足条件时如何激活按钮?

[英]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.

相关问题 如何在满足特定条件之前停止ios按钮的功能? - How to stop ios button from functioning until a particular condition is met? 单击按钮时如何激活UISearchDisplayController - How do you activate a UISearchDisplayController when a button is clicked SwiftUI 如果按下按钮后满足条件,如何在某处创建视图链接,否则保持在同一个视图中? - SwiftUI how to make a view link somewhere if a condition is met after a button press, otherwise stay in the same view? 满足条件时执行完成处理程序 - Execute completion handler when condition is met 满足特定条件时检测LongPress - Detect LongPress when certain condition is met 满足特定条件时,如何删除在运行时创建的对象? - How do I remove objects created at runtime when a certain condition is met? 使用 Firestore DB,当满足特定条件时,如何在快照侦听器内打破 for 循环? - Using Firestore DB, how can I break out of a for loop inside of a snapshot listener when a certain condition is met? 禁用 UIKeyboard 中的“GO”按钮,直到满足特定条件 - Disable "GO" button in UIKeyboard until a certain condition is met 满足特定条件时如何隐藏按钮? - How do i keep my button hidden when specific conditions are met? 在Swift中满足特定条件时,是否有一种方法可以结束递归方法? - Is there a way to end a recursive method when a certain condition is met in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM