简体   繁体   English

UITextField输入不起作用

[英]UITextField input not working

I'm making an app in Xcode where a screen comes up asking you to multiply 2 numbers, and then you put the product in a text field and if the answer is right there will a button that is gray will turn blue. 我正在Xcode中制作一个应用,出现一个屏幕,要求您将两个数字相乘,然后将产品放在文本字段中,如果答案正确,则灰色的按钮将变为蓝色。

The problem is is that it wont work even if I'm sure of my answer while testing the app it wont work, the button will stay grey and the app will think the answer is wrong, please anyone help me and tell me what is wrong with my code! 问题是,即使我在测试应用程序时也能确定我的答案是行不通的,该按钮仍会保持灰色,并且该应用程序会认为答案是错误的,请任何人帮助我并告诉我有什么问题用我的代码!

    var firstNumber = arc4random()
    var secondNumber = arc4random()
    @IBOutlet weak var AnswerButton: UIButton!
    @IBOutlet weak var OutcomeNumber: UILabel!
    @IBOutlet weak var textField: UITextField!


    @IBAction func textFieldEditingDidChange(_ sender: Any) {
        print("textField: \(textField.text)")
        if ValidatePassword(text: textField.text!) {
            AnswerButton.isEnabled = true
        } else {
            AnswerButton.isEnabled = false
        }
    }

    @IBAction func AnswerButtonPressed(_ sender: Any) {
        print("you may enter")

        view.backgroundColor = UIColor.green
    }

    func ValidatePassword(text: String) -> Bool {
        var result = false
        if text == ("(\(firstNumber*secondNumber))") {
            result = true
        }
        return result

    }

    @IBAction func GObutton(_ sender: Any) {
        OutcomeNumber.text = "\(firstNumber)×\(secondNumber)"
        firstNumber = arc4random_uniform(10)
        secondNumber = arc4random_uniform(10)
    }

    @IBAction func GoMedium(_ sender: Any) {
        OutcomeNumber.text = "\(firstNumber)×\(secondNumber)"
        firstNumber = arc4random_uniform(50)
        secondNumber = arc4random_uniform(50)
    }

    @IBAction func GoHard(_ sender: Any) {
        OutcomeNumber.text = "\(firstNumber)×\(secondNumber)"
        firstNumber = arc4random_uniform(100)
        secondNumber = arc4random_uniform(100)
    }


        override func viewDidLoad() {
        super.viewDidLoad()
        AnswerButton.isEnabled = false
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

Maybe the problem is on this part: 也许问题出在这部分上:

func ValidatePassword(text: String) -> Bool {
    var result = false
    if text == "(\(firstNumber) *  \(secondNumber))" {
        result = true
    }
    return result

}

Between * and \\(secondNumber) are two spaces. *\\(secondNumber)有两个空格。 When you try with text (1 * 3) this function returns false but with (1 * 3) returns true 当您尝试使用文本(1 * 3)此函数返回false但是使用(1 * 3)返回true

On the other hand, is best to use Regular Expressions to evaluate this. 另一方面,最好使用Regular Expressions对此进行评估。

The result in Playground Playground的结果 在此处输入图片说明

Mhergon is right. Mhergon是正确的。 The problem is your function that validates the answer. 问题是您的功能可以验证答案。 Your code structure and the way you compute and accepts the answer is not in the right way. 您的代码结构以及计算和接受答案的方式都不正确。 But looks good for a starter. 但是对于初学者来说看起来不错。 Anyways, take a look at your ValidatePassword code. 无论如何,请看一下您的ValidatePassword代码。

Try your code in the PlayGround of Xcode. 在Xcode的PlayGround中尝试您的代码。

Take a look at this example taken from your code, just different variables: 看一下从您的代码中获得的这个示例,只是不同的变量:

let x = 3
let y = 4

print("(\(x) *  \(y))")

That prints "(3 * 4)" 打印"(3 * 4)"

Now, change the print to this: 现在,将打印内容更改为此:

print("(\(x*y))")

It now prints the correct computation and answer which is 12 . 现在,它将打印正确的计算和答案为12

EDIT 编辑

You are answering the wrong question. 您在回答错误的问题。 Generate the random numbers before printing them out to the user. 生成随机数,然后将其输出给用户。

You can check this sample I made. 您可以检查我做的这个样品。 To check for shorter codes as well. 还要检查较短的代码。 :) :)

https://github.com/glennposadas/TextFieldGame-iOS https://github.com/glennposadas/TextFieldGame-iOS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM