【问题标题】:Why is the Button enable not working when TextView has contents?为什么当 TextView 有内容时 Button enable 不起作用?
【发布时间】:2021-05-27 21:50:50
【问题描述】:

这可能是一个非常简单的新手错误,但我试图确保按钮被禁用,直到 TextView 中包含文本。

我设置

Button.isEnabled = false

然后添加此代码。我已经以各种方式尝试了这段代码的变体,我似乎无法将它附加到 textView 本身,我无法想象为什么这不起作用。

@IBAction func valueChanged(_ sender: AnyObject)
{
    if(title.text != "")
    {
        Button.isEnabled = true
    }
    else
    {
        Button.isEnabled = false
    }
}

感谢 StackedOverflow 社区 ...

【问题讨论】:

  • 这些都与 SwiftUI 无关

标签: swift xcode button uibutton


【解决方案1】:

设置按钮启用 alpha 以使外观更好,然后通过 valueChanged 设置启用/禁用

@IBOulet private weak var myButton: UIButton! {
    didSet {
        set(myButton, enabled: false)
    }
}

@IBAction private func valueChanged(_ sender: UITextView) {
  set(myButton, enabled: !sender.text.isEmpty)
}

private func set(_ button: UIButton, enabled: Bool) {
  button.isEnabled = enabled
  button.alpha = enabled ? 1 : 0.5
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    相关资源
    最近更新 更多