如何快速启用不同情况下的按钮? [英] How to enable a button in different cases in swift?

查看:29
本文介绍了如何快速启用不同情况下的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

handleShareAndAbortButton 将在 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)
    }
}

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

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

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.

更新

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

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.

预先感谢您的帮助!

推荐答案

在更改文本后永远不要调用 imageDidChange.

You never call imageDidChange after you changed the text.

使用:

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

     imageDidChange()
}

这篇关于如何快速启用不同情况下的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆