gpt4 book ai didi

swift - 如何在不同情况下快速启用按钮?

转载 作者:可可西里 更新时间:2023-11-01 01:52:25 28 4
gpt4 key购买 nike

我想在不同情况下禁用和启用按钮:

如果用户来到我的共享 viewController 按钮将被禁用:

func handleShareAndAbortButton() {
shareButton.isEnabled = false
abortButton.isEnabled = false

let attributedShareButtonText = NSAttributedString(string: shareButton.currentTitle!, attributes: [NSAttributedString.Key.foregroundColor: UIColor(red: 0, green: 0, blue: 0, alpha: 0.2) ])
let attributedabortButtonText = NSAttributedString(string: abortButton.currentTitle!, attributes: [NSAttributedString.Key.foregroundColor: UIColor(red: 0, green: 0, blue: 0, alpha: 0.2) ])
shareButton.setAttributedTitle(attributedShareButtonText, for: .normal)
abortButton.setAttributedTitle(attributedabortButtonText, for: .normal)
}

handleShareAndAbortButton 将在 viewDidLoad 中调用

如果有什么改变(图片或文字)我想启用按钮:

func imageDidChange() {
selectedText = postTextView.text!

let isText = selectedText
let isImage = selectedImage != nil

if isImage || isText != "Schreibe etwas..." && isText != nil {
shareButton.isEnabled = true
abortButton.isEnabled = true

let attributedShareButtonText = NSAttributedString(string: shareButton.currentTitle!, attributes: [NSAttributedString.Key.foregroundColor: UIColor(red: 255, green: 255, blue: 255, alpha: 1.0) ])
let attributedabortButtonText = NSAttributedString(string: abortButton.currentTitle!, attributes: [NSAttributedString.Key.foregroundColor: UIColor(red: 0, green: 0, blue: 0, alpha: 1.0) ])
shareButton.setAttributedTitle(attributedShareButtonText, for: .normal)
abortButton.setAttributedTitle(attributedabortButtonText, for: .normal)
}
}

我在 viewWillAppear 中调用了 imageDidChange

这里我在开始编辑后删除了占位符:

   extension ShareViewController: UITextViewDelegate {

func textViewDidBeginEditing(_ textView: UITextView) {
if postTextView.textColor == UIColor.lightGray {
postTextView.text = nil
postTextView.textColor = UIColor.black
}
}

func textViewDidEndEditing(_ textView: UITextView) {
if postTextView.text.isEmpty {
postTextView.text = "Schreibe etwas..."
postTextView.textColor = UIColor.lightGray
}
}
}

当用户现在输入一些文本时,占位符“Schreibe etwas...”将消失,文本也将是 != nil。但它不起作用。

更新

我发现了问题。当我删除 isText != "Schreibe etwas..."有用。但我需要这个。我如何与字符串进行比较?如果我没有此代码,用户可以始终使用占位符文本上传。

预先感谢您的帮助!

最佳答案

您永远不会在更改文本后调用 imageDidChange

使用:

func textViewDidEndEditing(_ textView: UITextView) {
if postTextView.text.isEmpty {
postTextView.text = "Schreibe etwas..."
postTextView.textColor = UIColor.lightGray
}

imageDidChange()
}

关于swift - 如何在不同情况下快速启用按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53850023/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com