2017-08-10 60 views
0

我正在創建一個允許用戶鍵入答案的應用程序。如何在用戶輸入答案後讓文本消失?錯誤或正確我希望文字消失。如何在用戶正確回答後清除文本字段?

func textFieldDidChange(textField:UITextField) 
{ 
    if inputField?.text?.characters.count ?? 0 < 4 
    { 
     return 
    } 

    if let numbers_text = numbersLabel?.text, 
     let input_text  = inputField?.text, 
     let numbers   = Int(numbers_text), 
     let input   = Int(input_text) 
    { 
     print("Comparing: \(input_text) minus \(numbers_text) == \(input - numbers)") 

     if(input - numbers == 1111) 
     { 
      print("Correct!") 

      score += 1 

      showHUDWithAnswer(isRight: true) 
     } 
     else 
     { 
      print("Incorrect!") 

      score -= 1 

      showHUDWithAnswer(isRight: false) 
     } 
    } 

    setRandomNumberLabel() 
    updateScoreLabel() 
+1

也許嘗試的TextField.text = 「」 – koropok

+0

可以設置yourtextField.text = 「」 當用戶提交了他答。 – luckyShubhra

回答

0

Just ... make textField hidden?

inputField.resignFirstResponder() 
inputField.text = "" 
inputField.isHidden = true 
0
you can use textfield delegates to check what text is typed 

e.g. in viewDidload() { 
    textfield.delegate = self 
} 

then extend UIViewController with UITextFieldDelegate and use this 
function of delegate to compare text of textfield with your answer 

func textField(_ textField: UITextField, shouldChangeCharactersIn 
range: NSRange, replacementString string: String) -> Bool { 
    if textField.text == YOUR_ANSWER { 
     textField.text = nil 
     print(textField.text ?? "") 
    } 
    return true 
}