2017-11-19 16 views
0

私はアプリケーションで履歴を実行するためのコードを持っています。クリックするとラベルが更新されます

=を押すだけではなく、数値をすぐに表示するにはどうすればよいですか?

私のプログラムの開発:

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

答えて

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" 
    } 
} 
+0

としての価値を置くことができます "addObserver(自己、forKeyPath:#keyPath(入力)、オプション:[.oldという、.new]、コンテキスト:ゼロ)" ??? – B2Fq

+0

でviewController。 UIViewControllerの実装の一部です。 –

関連する問題