捕获用户输入的Swifit(我的第一个程序)

时间:2015-10-27 11:16:28

标签: swift

我无法弄清楚a.sucess和a.attempt的值是如何来自用户在成功和尝试框中编写数字的输入。

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var Attempts: UITextField!
@IBOutlet weak var Sucess: UITextField!
@IBOutlet weak var Check: UIButton!
@IBOutlet weak var thepage: UILabel!



override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view, typically from a nib.
}

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



@IBAction func Check(sender: UIButton) {

    var str = "here"

    class Tries {

        var Attempts:Float64 = 100.00
        var Sucess:Float64 = 50.00
        init()  {

        }

        func Shot() {


            if Attempts < 99.5  {
                print ("you need more attempts :( Shoot some more!!")
            }




            else if Attempts > 99 && Sucess > 49 && Sucess / Attempts > 0.4999
            {


                print("accepted")
            } else {
                print("failed ")
            }
        }
    }

    var a = Tries()

    a.Attempts = 200 // this input should come from the user writing input in      the textfield... (NEED HELP HERE)
    a.Sucess = 100 // this input should come from the user writing input in the textfield... (NEED HELP HERE)
    a.Shot() // This is the function that should load when you click on the button: click

}

我想要a.Attempts和a.Sucess从用户在两个方框中写下他/她自己的号码来获取价值。

1 个答案:

答案 0 :(得分:0)

首先,你的代码编写方式非常糟糕。你写的东西很难读。调用变量更清晰,不调用大写字母的对象,需要调用名称表示类型而不是对象的名称。以此为例:https://github.com/raywenderlich/swift-style-guide以使您的代码更清晰。 关于你的问题。也许我不太明白这个问题,但显然你只需要阅读UITextField对象的值,你就可以这样做:

let strValue = textField.text  // in your case Attempts.text

然后您需要将其转换为CGFloat

let floatValue = CGFloat((strValue as NSString).floatValue)

然后你可以在你的对象中使用它:

a.Attempts = floatValue

您仍应检查转换是否正常,然后才分配值。

您可以这样做以解决您的问题:

import UIKit

class ValuesChecker {

    class func checkValues(firstValue: Float, secondValue: Float) -> String {
        if firstValue < 99.5  {
            return "you need more attempts :( Shoot some more!!"
        } else if firstValue > 99 && secondValue > 49 && secondValue / firstValue > 0.4999 {
            return "accepted"
        } else {
            return "failed "
        }
    }
}

class ViewController: UIViewController {

    @IBOutlet weak var firstTextField: UITextField!
    @IBOutlet weak var secondTextField: UITextField!

    @IBAction func actionWithPressButton(sender: AnyObject) {
        let firstValue = (firstTextField.text as NSString).floatValue
        let secondValue = (secondTextField.text as NSString).floatValue

        println(ValuesChecker.checkValues(firstValue, secondValue: secondValue))
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

评论:

  1. 您不需要在您的案例中创建类对象。您可以创建静态类方法,如checkValues
  2. 您可以以任何方式提供测试结果,而不是String,而不是在程序中稍后使用