问题描述:

I am creating a OSX app and would like to enable a button when all textfield are filled. But do not have much experience with osx app as there seem to be some difference from ios.

This is what I have tried.

override func viewDidLoad() {

super.viewDidLoad()

btnCalculate.enabled = false

}

override func controlTextDidChange(obj: NSNotification) {

if panelsWideTextField.stringValue.isEmpty {

btnCalculate.enabled = false

} else {

btnCalculate.enabled = true

}

}

Hope someone has a good tip for me :-)

EDIT:

Complete code.

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

}

}

相关阅读:
Top