【问题标题】:History in calculator (Swift)计算器中的历史 (Swift)
【发布时间】:2017-11-05 09:47:30
【问题描述】:

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

我的应用截图:

我该如何改进它,以便在按下时立即显示数字(如视频所示),而不仅仅是按下=

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


    【解决方案1】:

    一个选项可以是为标签字符串定义一个单独的变量,在每次按下 UIButton(数字或运算符)后使用对 addHistory() 的调用不断更新该标签字符串,然后由变量内部的 didSet 处理标签本身的更新定义:

    var resultLabelString: String = "" {
          didSet {
              self.resultLabelText.text = self.resultLabelText.text! + "" + resultLabelString
          }
    }
    
    func addHistory(text: String){
        self.resultLabelString =  text
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-04
      • 1970-01-01
      • 2021-04-15
      • 1970-01-01
      • 2018-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多