单击时更新标签
Update label when clicked
我有 运行 应用程序历史记录的代码。
我怎样才能改进它以便在按下时立即显示数字,而不仅仅是按下 =?
我的程序:
// Connected to button "="
@IBAction func equalitySignPressed(sender: UIButton) {
if stillTyping {
secondOperand = currentInput
}
dotIsPlaced = false
addHistory(text: operationSign + displayResultLabel.text!)
switch operationSign {
case "+":
operateWithTwoOperands{[=10=] + }
case "-":
operateWithTwoOperands{[=10=] - }
case "×":
operateWithTwoOperands{[=10=] * }
case "÷":
operateWithTwoOperands{[=10=] / }
default: break
}
}
func addHistory(text: String){
//Add text
resultLabelText.text = resultLabelText.text! + "" + text
}
当前输出:
这里可以使用Key-Value observing(KVO)
addObserver(self, forKeyPath: #keyPath(INPUTS), options: [.old, .new], context: nil)
您可以将值观察为
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == #keyPath(INPUT) {
// Update UI
resultLabelText = "NEW_VALUE"
}
}
我有 运行 应用程序历史记录的代码。
我怎样才能改进它以便在按下时立即显示数字,而不仅仅是按下 =?
我的程序:
// Connected to button "="
@IBAction func equalitySignPressed(sender: UIButton) {
if stillTyping {
secondOperand = currentInput
}
dotIsPlaced = false
addHistory(text: operationSign + displayResultLabel.text!)
switch operationSign {
case "+":
operateWithTwoOperands{[=10=] + }
case "-":
operateWithTwoOperands{[=10=] - }
case "×":
operateWithTwoOperands{[=10=] * }
case "÷":
operateWithTwoOperands{[=10=] / }
default: break
}
}
func addHistory(text: String){
//Add text
resultLabelText.text = resultLabelText.text! + "" + text
}
当前输出:
这里可以使用Key-Value observing(KVO)
addObserver(self, forKeyPath: #keyPath(INPUTS), options: [.old, .new], context: nil)
您可以将值观察为
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == #keyPath(INPUT) {
// Update UI
resultLabelText = "NEW_VALUE"
}
}