如何在Swift中实现在不同情况下启用按钮?

3

我希望在不同的情况下禁用或启用一个按钮:

如果用户进入我的共享视图控制器,则按钮将被禁用:

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…”将会消失,文本也将变为非零。但它没有起作用。
更新
我找到了问题所在。当我删除isText!=“Schreibe etwas…”时,它就可以工作了。但我需要这个。我如何与一个字符串进行比较?如果我没有这段代码,用户就可以始终上传带有占位符文本的内容。
谢谢你提前的帮助!

在 viewDidAppear 中它可以工作吗? - Retterdesdialogs
它在 viewWillAppear 中。 - jo1995
1
你在哪里调用了imageDidChange?它也应该在textViewDidEndEditing中被调用。 - Retterdesdialogs
1
对我来说完美无缺,谢谢!我需要在textViewDidEndEditing中也调用ìmageDidChange。给出一个答案,我会接受它! - jo1995
不需要谢。 - Retterdesdialogs
显示剩余3条评论
2个回答

1

在您更改文本后,您从未调用imageDidChange

使用:

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

     imageDidChange()
}

0

我注意到你正在使用 UITextField,你可能只想要一个占位符 "Schreibe etwas..."。因此,在 viewDidLoad 或者在 Storyboard 中将你的 TextField 的占位符设置为 "Schreibe etwas..."。这样你就不需要在委托方法中设置文本和文本颜色的 TextField 委托方法。

好吧,你需要它,不过不是为了这个。你必须在 textFieldDidEndEditing 中调用 imageDidChange 方法。

现在改变声明 isText。你只是传递了 selectedText 的引用。不要这样做,只需检查是否有文本即可。

let isText = !selectedText.isEmpty // don’t forget to !

同时重写你的条件

if isImage || isText

... 你不需要检查isText是否为nil


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接