单击时更新标签

时间:2017-11-19 13:51:07

标签: ios swift uibutton ibaction

我有在应用程序中运行历史记录的代码。

如何改进它以便在按下时立即显示数字,而不仅仅是按 =

我的程序:

 // Connected to button "="
@IBAction func equalitySignPressed(sender: UIButton) {
  if stillTyping {
     secondOperand = currentInput
  }
dotIsPlaced = false

addHistory(text: operationSign + displayResultLabel.text!)

switch operationSign {
   case "+":
      operateWithTwoOperands{$0 + $1}
   case "-":
      operateWithTwoOperands{$0 - $1}
   case "×":
      operateWithTwoOperands{$0 * $1}
   case "÷":
      operateWithTwoOperands{$0 / $1}
default: break
  }
}
func addHistory(text: String){
     //Add text
    resultLabelText.text =  resultLabelText.text! + "" + text
}

当前输出:

enter image description here gif

1 个答案:

答案 0 :(得分:0)

您可以在此处使用键值观察(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"
    }
}