Reputation: 1303
I'd like to save textField.text while editing. If I tap save button and call savePressed function while editing(before textFieldShouldEditing function is called), textField cannot get self.name data. Could you tell me how to save data, if savePressed function is called before textFieldShouldEndEditing func is called?
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)
}
Upvotes: 1
Views: 1896
Reputation: 1303
By using the following code, I've solved the problem.I referred this.Textfield shouldchangecharactersinrange swift
textField.addTarget(self, action: "didChangeText:", forControlEvents: .EditingChanged)
func didChangeText(textField:UITextField) {
}
Upvotes: 1