如果 NSTextfield 不为空,则启用 NSButton

enable NSButton if NSTextfield is not empty

我正在创建一个 OSX 应用程序,并希望在所有文本字段都已填充时启用一个按钮。但是没有太多使用 osx 应用程序的经验,因为似乎与 ios.

有一些不同

这是我试过的。

override func viewDidLoad() {
    super.viewDidLoad()

    btnCalculate.enabled = false

}

override func controlTextDidChange(obj: NSNotification) {

    if panelsWideTextField.stringValue.isEmpty {
        btnCalculate.enabled = false
    } else {
        btnCalculate.enabled = true
    }
}

希望有人能给我一个好的提示:-)

编辑:

完整代码。

import Cocoa
//import AppKit

class ViewController: NSViewController, NSTextFieldDelegate {



@IBOutlet weak var panelHightTextField: NSTextField!
@IBOutlet weak var panelWidthTextField: NSTextField!
@IBOutlet weak var panelPitchTextField: NSTextField!
@IBOutlet weak var panelsHighTextField: NSTextField!
@IBOutlet weak var panelsWideTextField: NSTextField!
@IBOutlet weak var resWidthLabel: NSTextField!
@IBOutlet weak var resHightLabel: NSTextField!
@IBOutlet weak var lblScreenWidth: NSTextField!
@IBOutlet weak var lblScreenHight: NSTextField!
@IBOutlet weak var lblScreenArea: NSTextField!
@IBOutlet weak var btnCalculate: NSButton!
@IBOutlet weak var lblAmountPanels: NSTextField!



var panelHight = ""
var panelWidth = ""
var panelPitch = ""
var panelsHigh = ""
var panelsWidth = ""


var resWidth : Float = 0
var resHigh : Float = 0
var screenHight : Float = 0
var screenWidth : Float = 0
var screenArea : Float = 0
var ammountPanels : Float = 0



override func viewDidLoad() {
    super.viewDidLoad()

    btnCalculate.enabled = false

}

override func controlTextDidChange(obj: NSNotification) {
    if panelsWideTextField.stringValue.isEmpty {
        btnCalculate.enabled = true
    } else {
        btnCalculate.enabled = false
    }
}


override var representedObject: AnyObject? {
    didSet {
        // Update the view, if already loaded.
    }
}


@IBAction func calculateResButton(sender: AnyObject) {
    takeUserData()
    calculatehight()
    calculatewidth()
    calculateArea()
    calculateAmountPanels()
}


func takeUserData(){
    panelHight = panelHightTextField.stringValue
    panelWidth = panelWidthTextField.stringValue
    panelPitch = panelPitchTextField.stringValue
    panelsHigh = panelsHighTextField.stringValue
    panelsWidth = panelsWideTextField.stringValue
}

// Calculating resolution and physical hight
func calculatehight(){
    let fpanelHight = Float(panelHight)
    let fpanelPitch = Float(panelPitch)
    let fpanelsHigh = Float(panelsHigh)

    resHigh = fpanelHight! * fpanelsHigh! / fpanelPitch!
    screenHight = fpanelHight! * fpanelsHigh! / 1_000

    printText()

}

// Calculating resolution and physical width
func calculatewidth(){
    let fpanelWidth = Float(panelWidth)
    let fpanelPitch = Float(panelPitch)
    let fpanelsWidth = Float(panelsWidth)


    resWidth = fpanelWidth!*fpanelsWidth!/fpanelPitch!
    screenWidth = fpanelWidth!*fpanelsWidth! / 1_000

    printText()

}

// Calculating sqm of LED screen
func calculateArea(){

    let fpanelHight = Float(panelHight)
    let fpanelsHigh = Float(panelsHigh)
    let fpanelWidth = Float(panelWidth)
    let fpanelsWidth = Float(panelsWidth)


    screenArea = (fpanelHight! * fpanelsHigh!) * (fpanelWidth! * fpanelsWidth!) / 1_000_000

    printText()

}

// Calculating the amount of panels used.
func calculateAmountPanels(){

    let fpanelsHigh = Float(panelsHigh)
    let fpanelsWidth = Float(panelsWidth)

    ammountPanels = (fpanelsWidth! * fpanelsHigh!)

    printText()

}

// Outputting text to labels with correct format.
func printText(){

    let formatResHigh = String(format: "%0.0f", resHigh)
    let formatResWidth = String(format: "%0.0f", resWidth)
    let formatScreenHight = String(format: "%0.2f", screenHight)
    let formatScreenWidth = String(format: "%0.2f", screenWidth)
    let formatScreenArea = String(format: "%0.0f", screenArea)
    let formatAmmountPanels = String(format: "%0.0f", ammountPanels)

    resHightLabel.stringValue = "\(formatResHigh)px"
    resWidthLabel.stringValue = "\(formatResWidth)px"
    lblScreenHight.stringValue = "\(formatScreenHight)m"
    lblScreenWidth.stringValue = "\(formatScreenWidth)m"
    lblScreenArea.stringValue = "\(formatScreenArea) sqm"
    lblAmountPanels.stringValue = "\(formatAmmountPanels)"
}
}

我遇到了同样的问题,但我找到了简单的解决方案:checkbox 可以启用和禁用 NSButton。

import Cocoa

class ViewController: NSViewController {

    @IBOutlet weak var btnCalculate: NSButton!
    @IBOutlet weak var checkBox: NSButton!


    override func viewDidLoad() {
        super.viewDidLoad()
        btnCalculate.enabled = false
        checkBox.state = 0
    }

    @IBAction func checkAgree(sender: NSButton) {
        if btnCalculate.stringValue.characters.count > 0 && checkBox.state == 1 {
            btnCalculate.enabled = true
        } else {
            btnCalculate.enabled = false
        }
    }
}