如何在用户正确回答后清除文本字段?

时间:2017-08-10 02:27:58

标签: ios swift uitextfield

我正在创建一个允许用户输入答案的应用。用户输入答案后,如何使文本消失?错误或正确我希望文本消失。

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 个答案:

答案 0 :(得分: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
 }