如何在编辑时保存textField.text

时间:2015-08-29 01:50:44

标签: swift uitextfield

我想在编辑时保存textField.text。如果在编辑时点击保存按钮并调用savePressed函数(在调用textFieldShouldEditing函数之前),textField将无法获取self.name数据。如果在调用textFieldShouldEndEditing func之前调用savePressed函数,你能告诉我如何保存数据吗?

func textFieldShouldEndEditing(textField: UITextField) -> Bool {  //delegate method

    self.name = textField.text

    return true
}

func textFieldShouldReturn(textField: UITextField) -> Bool {   //delegate method

    textField.resignFirstResponder()

    return true
}

func savePressed(sender: UIButton){
    var parameters = [
        "id":   currentUserId,
        "name": name
    ]
    Alamofire.request(.POST, uri.usersApi + "/save_profile", parameters: parameters as? [String : AnyObject], encoding: .JSON)
        .responseJSON { (request, response, data, error) in
            var jsonObj = JSON(data!)
    }

    fetchData()
    self.dismissViewControllerAnimated(true, completion: nil)
}

1 个答案:

答案 0 :(得分:1)

通过使用以下代码,我解决了这个问题。我提到了这一点。Textfield shouldchangecharactersinrange swift

textField.addTarget(self, action: "didChangeText:", forControlEvents: .EditingChanged)
func didChangeText(textField:UITextField) { 
}