2017-11-19 22 views
0

응용 프로그램에서 기록을 실행하기위한 코드가 있습니다.클릭하면 레이블이 업데이트됩니다.

=을 누르는 것뿐만 아니라 숫자를 바로 표시 할 수 있도록 개선하려면 어떻게해야합니까?

내 programm에 :

// 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 (INPUTS), 옵션 : [.old로, .new를, 컨텍스트 : 전무)" ??? – B2Fq

+0

에서 viewController. UIViewController 구현 부분. –