我有在应用程序中运行历史记录的代码。
我的应用截图:
如何改进它以便在按下时立即显示数字(如视频中所示),而不仅仅是按=
。
// 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
}
答案 0 :(得分:1)
一个选项可以是为每个UIButton按下(数字或运算符)后使用addHistory()
调用不断更新的标签字符串定义一个单独的变量,然后更新由didSet处理的标签本身变量定义:
var resultLabelString: String = "" {
didSet {
self.resultLabelText.text = self.resultLabelText.text! + "" + resultLabelString
}
}
func addHistory(text: String){
self.resultLabelString = text
}