【问题标题】:Update label when clicked单击时更新标签
【发布时间】:2018-05-02 19:44:20
【问题描述】:

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

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

我的程序:

 // 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
}

电流输出:

【问题讨论】:

    标签: ios swift uibutton ibaction


    【解决方案1】:

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

    【讨论】:

    • 在哪里放置“addObserver(self, forKeyPath: #keyPath(INPUTS), options: [.old, .new], context: nil)” ???
    • 在视图控制器中。它是 UIViewControllers 实现的一部分。
    猜你喜欢
    • 1970-01-01
    • 2015-12-20
    • 2013-06-30
    • 1970-01-01
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多