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

标签 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()

最佳答案

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
 }

关于ios - 用户正确回答后如何清除文本字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45603480/

相关文章:

iOS View.layer.border 有白边

ios - 在iOS应用程序中显示3D图表

ios - 当键盘存在时,使用 UITextView 向上移动 UIAlertController 样式警报

swift - UITableview 在重新加载时滚动到顶部

swift - 如何启用/禁用 TextField SWIFT

iphone - UITextField 键盘不出现

ios - FIR 数据库查询 : how to do an inner join

ios - Storyboard设计模式(分享或不分享)

iOS 13 : How can I tweak the leading/descend/line height of a custom font in UIKit/SwiftUI

ios - 如何识别哪个 textField 在动态创建的包含 2 个 uiTextFields 的 UITableCell 上触发了 textFieldDidEndEditing?