【问题标题】:How to enable a button in different cases in swift?如何在不同情况下快速启用按钮?
【发布时间】:2019-05-19 20:58:45
【问题描述】:

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

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

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..." 有用。但我需要这个。如何与字符串进行比较?如果我没有此代码,则用户可以始终使用占位符文本进行上传。

提前感谢您的帮助!

【问题讨论】:

  • 在 viewDidAppear 中有效吗?
  • 它在viewWillAppear
  • 用图片可以,但是如果我只想发布文字,它就行不通
  • 你在哪里调用 imageDidChange?它也应该在 textViewDidEndEditing 中调用
  • 非常适合我,谢谢!我还需要在textViewDidEndEditing 中调用ìmageDidChange` 做出回答,我会接受!

标签: swift uibutton


【解决方案1】:

更改文本后,您永远不会致电imageDidChange

用途:

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

     imageDidChange()
}

【讨论】:

    【解决方案2】:

    我看到您使用的是UITextField。您可能只想拥有占位符"Schreibe etwas..."。因此,在 viewDidLoad 或 Storyboard 中将 TextField 的占位符设置为“Schreibe etwas ...”。那么你就不需要TextField的委托方法来设置委托方法中的文本和文本颜色了。

    嗯,你需要它,但不是为了这个。您必须致电imageDidChangetextFieldDidEndEditing

    现在更改声明 isText。您刚刚传递了selectedText 的引用。不要这样,只检查是否有文字

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

    也重写你的条件

    if isImage || isText
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-20
      • 2022-09-28
      相关资源
      最近更新 更多