How to enable a button in different cases in swift?

时间:2018-12-19 11:10:55

标签: swift uibutton

I want to disable, and enable a button in different cases:

If the user comes to my share viewController the button will be disabled:

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)
}

The handleShareAndAbortButton will be called in viewDidLoad

If something changed (picture or text) I want to enable the button:

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)
    }
}

I call the imageDidChange in viewWillAppear

Here I delete the placeholder after begin editing:

   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
        }
    }
}

When the user now enter some text, the placeholder "Schreibe etwas..." will be gone and the text will be also != nil. But its not working.

Update

I found out the problem. When I delete the isText != "Schreibe etwas..." it works. But I need this. How can I compare to a String? If I don't have this code, the user is able to upload always with placeholder text.

Thanks in advance for your help!

2 个答案:

答案 0 :(得分:1)

更改文本后,您再也不会呼叫imageDidChange

使用:

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

     imageDidChange()
}

答案 1 :(得分:0)

我发现您正在使用UITextField。您可能只需要占位符"Schreibe etwas..."。因此,在viewDidLoad或情节提要中,将TextField的占位符设置为“ Schreibe etwas ...”。然后,您无需使用TextField的委托方法来设置委托方法中的文本和文本颜色。

好吧,您需要它,但不是这个。您必须通过imageDidChange

呼叫 textFieldDidEndEditing

现在声明isText进行更改。您刚刚传递了selectedText的参考。不要这样做,只需检查是否有文字

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

还重写您的条件

if isImage || isText

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