我有一个ProfileViewController,其中包含一个.xib文件作为视图。在此视图中,当前用户数据已从Firebase数据库加载到文本字段中。当我单击ProfileViewController中的“更新配置文件”按钮时,将调用PopUpViewController,并且弹出视图会显示在ProfileViewController上,其中包含用户可以更新的文本字段(例如:年龄,体重),以及关闭和更新按钮。 。我的问题是,当我按下弹出窗口上的“更新”按钮时,我希望用户个人资料信息在.xib视图上自动更新并关闭弹出窗口。我想知道是否有人可以帮助我?现在已经困扰了我一段时间了。
我尝试将“ self.removeAnimate()”放入更新按钮的动作中。但是,如果我转到上一个视图控制器,然后再返回到用户配置文件,它只会更新文本字段。如果我从更新按钮中删除“ self.removeAnimate()”并仅将其放在关闭按钮上,则当我按下更新然后关闭按钮时,它将立即显示更新的信息。
var ref: DatabaseReference?
//function for updating the current users information in the database.
func updateUser(){
guard let uid = Auth.auth().currentUser?.uid else { return }
let userReference = Database.database().reference()
let values = ["age" : userAgeTextField.text! as String,
"weight": userWeightTextField.text! as String]
userReference.child("users").child(uid).updateChildValues(values)
}
///popup cancel button
@IBAction func cancelButton(_ sender: Any) {
self.removeAnimate()
}
///this is where the updateUser function is called once the user presses the update button, here is where I want to close the popup as well when the update button is clicked.
@IBAction func updateButton(_ sender: Any) {
updateUser()
//self.removeAnimate()
}
//function for removing the popup
func removeAnimate(){
UIView.animate(withDuration: 0.25, animations: {
self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.view.alpha = 0.0;
}, completion:{(finished: Bool) in
if(finished){
self.view.removeFromSuperview()
}
});
}
答案 0 :(得分:0)
您可以添加一个完成框,如建议Firebase docs在更新完成时收到通知,例如使用Swift 5结果:
func updateUser(completion: @escaping (Result<Void, Error>) -> Void) {
guard let uid = Auth.auth().currentUser?.uid else { return }
let userReference = Database.database().reference()
let values = ["age" : userAgeTextField.text! as String,
"weight": userWeightTextField.text! as String]
userReference.child("users").child(uid).updateChildValues(values) {
(error: Error?, ref: DatabaseReference) in
if let error = error {
print("Data could not be saved: \(error).")
completion(.failure(error))
} else {
print("Data saved successfully!")
completion(.success())
}
}
}
然后:
@IBAction func updateButton(_ sender: Any) {
updateUser { [weak self] result in
switch result {
case .success:
self?.removeAnimate()
case .failure(let error):
// handle error
}
}
}