如果NSTextfield不为空,则启用NSButton

时间:2016-08-04 15:58:59

标签: swift macos nsbutton

我正在创建一个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)"
}
}

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,但我找到了简单的解决方案: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
        }
    }
}

enter image description here