计算器历史(斯威夫特)

时间:2017-11-05 09:47:30

标签: ios swift xcode

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

我的应用截图:

img

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

gif

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

1 个答案:

答案 0 :(得分:1)

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

var resultLabelString: String = "" {
      didSet {
          self.resultLabelText.text = self.resultLabelText.text! + "" + resultLabelString
      }
}

func addHistory(text: String){
    self.resultLabelString =  text
}