禁用按钮并将其变为灰色

时间:2016-06-25 21:21:46

标签: swift button conditional-statements

我有一个按钮,按下后会打开一个新视图。

the button is called " cálculo "

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let secondViewController:ContasView = segue.
destinationViewController as! ContasView

我不希望在标签值不为零之前启用它。我通常将这种按钮看作灰色文本按钮。 那么,有两个问题:  1.如何禁用这种情况?  2.一旦禁用,如何变成灰色文本按钮?

2 个答案:

答案 0 :(得分:0)

开始聆听:

textField1.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
textField2.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)
textField3.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)

检查值:

func textFieldDidChange(textField: UITextField) {
    button.enabled = !textField1.text.isEmpty && !textField2.text.isEmpty && !textField3.text.isEmpty

答案 1 :(得分:0)

var button = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    button.userInteractionEnabled = false
    button.setTitle("ButtonTextHere", forState: .Normal)
    button.setTitleColor(UIColor.grayColor(), forState: .Normal)
    view.addSubview(button)

代码中的某处,只要标签发生变化,您就可以

button.userInteractionEnabled = true
button.setTitleColor(UIColor.blueColor(), forState: .Normal)