Swift - 在UIButton事件之后将字段设置为空

时间:2015-12-23 02:43:14

标签: ios uibutton

我有一个UIButton操作,它根据4个文本字段中的值将数据发送到PHP脚本。提交表单后,字段应重置为null。但是,我在设置字段时遇到问题。这是我的代码 - 设置为null的变量是脚本底部的用户名,密码,名称和电子邮件。任何帮助表示赞赏:

    @IBOutlet weak var txtUsername: UITextField!
    @IBOutlet weak var txtPassword: UITextField!
    @IBOutlet weak var txtName: UITextField!
    @IBOutlet weak var txtEmailAddress: UITextField!

    @IBAction func btnSubmit(sender: UIButton) {
        //Fields from app.
        var username = txtUsername.text
        var password = txtPassword.text
        var name = txtName.text
        var email = txtEmailAddress.text

        let myUrl = NSURL(string: "http://localhost/phpPost/demo.php");
        let request = NSMutableURLRequest(URL: myUrl!);
        request.HTTPMethod = "POST";

        let postString = "username=" + username + "&password=" + password + "&name=" + name + "&email=" + email;

        request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
            data, response, error in
            if error != nil {
                println("error=\(error)")
                return
            }
            println("******response = \(response)")

            // Print out response body
            let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
            println("****response data=\(responseString)")

            //Convert response sent from a server side script to a NSDictionary object:
            var err: NSError?
            var myJSON = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error:&err) as? NSDictionary

            if let parseJSON = myJSON {
                var name = parseJSON["name"] as? String
                println("name: \(name)")
            }

            username = ""
            password = ""
            name = ""
            email = ""
        }

        task.resume()
    }

2 个答案:

答案 0 :(得分:2)

不要将字符串值设置为null或空字符串或任何内容,设置text属性的UITextField值:

txtUsername.text = ""
txtPassword.text = ""
txtName.text = ""
txtEmailAddress.text = ""

更改username只会将作用域的字符串更改为当前函数,它不再与其值来自的文本字段有任何关系。因此,更改其值对文本字段没有影响。

答案 1 :(得分:2)

你没有做任何改变它应该是这样的文本字段。您需要直接访问文本字段。你现在正在做的是从它们复制字符串并改变它。

@IBOutlet weak var txtUsername: UITextField!
@IBOutlet weak var txtPassword: UITextField!
@IBOutlet weak var txtName: UITextField!
@IBOutlet weak var txtEmailAddress: UITextField!

@IBAction func btnSubmit(sender: UIButton) {

    let myUrl = NSURL(string: "http://localhost/phpPost/demo.php");
    let request = NSMutableURLRequest(URL: myUrl!);
    request.HTTPMethod = "POST";

    let postString = "username=" + username + "&password=" + password + "&name=" + name + "&email=" + email;

    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        data, response, error in
        if error != nil {
            println("error=\(error)")
            return
        }
        println("******response = \(response)")

        // Print out response body
        let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
        println("****response data=\(responseString)")

        //Convert response sent from a server side script to a NSDictionary object:
        var err: NSError?
        var myJSON = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error:&err) as? NSDictionary

        if let parseJSON = myJSON {
            var name = parseJSON["name"] as? String
            println("name: \(name)")
        }

        self.txtUsername.text = ""
        self.txtPassword.text = ""
        self.txtName.text = ""
        self.txtEmailAddress.text = ""
    }

    task.resume()
}

如果您需要清除文本字段,无论如何,您都要将文本字段设置为块。因为在异步响应后调用了块。

@IBOutlet weak var txtUsername: UITextField!
@IBOutlet weak var txtPassword: UITextField!
@IBOutlet weak var txtName: UITextField!
@IBOutlet weak var txtEmailAddress: UITextField!

@IBAction func btnSubmit(sender: UIButton) {

    let myUrl = NSURL(string: "http://localhost/phpPost/demo.php");
    let request = NSMutableURLRequest(URL: myUrl!);
    request.HTTPMethod = "POST";

    let postString = "username=" + username + "&password=" + password + "&name=" + name + "&email=" + email;

    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        data, response, error in
        if error != nil {
            println("error=\(error)")
            return
        }
        println("******response = \(response)")

        // Print out response body
        let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
        println("****response data=\(responseString)")

        //Convert response sent from a server side script to a NSDictionary object:
        var err: NSError?
        var myJSON = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error:&err) as? NSDictionary

        if let parseJSON = myJSON {
            var name = parseJSON["name"] as? String
            println("name: \(name)")
        }
    }

    self.txtUsername.text = ""
    self.txtPassword.text = ""
    self.txtName.text = ""
    self.txtEmailAddress.text = ""
    task.resume()
}