如果未编辑图像,如何禁用按钮?

How can I disable a button if an image has not been edited?

我想尝试禁用 post 按钮,直到用户选择了图像。

但是,我将默认图像设置为 placeholder.I 已经设法在文本字段未填充信息时禁用 post 按钮。但是,我还没有找到禁用按钮的方法 immediately.Hopefully 我已经解释过了。

Here's an image of my uiview

func handleBlancInformation(){
    address.addTarget(self, action: #selector(PostViewController.textFieldDidChange), for: UIControlEvents.editingChanged)
    breed.addTarget(self, action: #selector(PostViewController.textFieldDidChange), for: UIControlEvents.editingChanged)
    phone.addTarget(self, action: #selector(PostViewController.textFieldDidChange), for: UIControlEvents.editingChanged)

}


@objc func textFieldDidChange() {
    guard let address = address.text, !address.isEmpty,
            let breed = breed.text, !breed.isEmpty,
            let phone = phone.text, !phone.isEmpty,
            let image = imagePosted.image, !imagePosted.isEqual("placeholder.png")
        else {
        postButton.setTitleColor(UIColor.lightText, for: UIControlState.normal)
            postButton.isEnabled = false
            return
    }
    postButton.setTitleColor(UIColor.white, for: UIControlState.normal)
    postButton.isEnabled = true
}

据我了解,用户可以在加载视图时单击,而您想阻止这种情况对吗?

but when they first enter the uiview they can click the post button

如果是这样,只需禁用 viewDidLoad() 或情节提要中的按钮即可。

override func viewDidLoad() {
    postButton.isEnabled = false
}

或取消选中 "Enabled" 字段

要验证用户是否确实选择了图像(在视图加载后)检查 Greg 的

我需要更多代码才能在这里给出准确的解决方案,但请这样考虑:

无论您使用何种方法让用户选择新图像,您都应该在此处启用 "post" 按钮。

例如,假设您使用 UIImagePickerViewController 来处理选择图像,您需要启用此方法内的 "post" 按钮,它是 UIImagePickerControllerDelegate[= 的一部分18=]:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
     if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
          //Enable button here
     } else {
          //Disable button here
     }
}

试试这个。使用 bool 作为状态来检查天气图像是否被选中。

var isImageSelected = false

override func viewDidLoad() { postButton.isEnabled = false }

检查图像是否被选中。选择图像时更改按钮启用 属性。

if isImageSelected{
// Do Your Work
}

这就是我设法使按钮从用户进入此选项卡的那一刻起消失并在 uiimage 和文本字段都已填写后显示的方法。

 override func viewDidLoad() {

        handleBlancInformation()
        postButton.isHidden = true

    }


func handleBlancInformation(){
            address.addTarget(self, action: #selector(PostViewController.textFieldDidChange), for: UIControlEvents.editingChanged)
            breed.addTarget(self, action: #selector(PostViewController.textFieldDidChange), for: UIControlEvents.editingChanged)
            phone.addTarget(self, action: #selector(PostViewController.textFieldDidChange), for: UIControlEvents.editingChanged)
            changeImageLostButton.addTarget(self, action: #selector(PostViewController.textFieldDidChange), for: UIControlEvents.editingChanged )
        }

        @objc func textFieldDidChange() {
            guard let address = address.text, !address.isEmpty,
                    let breed = breed.text, !breed.isEmpty,
                    let phone = phone.text, !phone.isEmpty
                else {
                    postButton.isHidden = true
                    return
            }
            postButton.isHidden = false
        }


        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])

        {
            print("did Finish Picking Media")
            if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage {
                selectedImage = image
                imagePosted.image = image
            }

            else {

                postButton.isHidden = true

            }
            self.dismiss(animated: true, completion: {

                self.handleBlancInformation()

            })
        }