sending Firebase value to text fields

丶灬走出姿态 提交于 2019-12-08 05:43:35

问题


I have a view controller with three text fields. When the user uses the app for the first time, they enter some information in those text fields and press done, then the information goes to Firebase database:

FIRDatabase.database().reference().child("info").child(self.loggedInUser!.uid).setValue(saveObject)

However, the user also has the ability to edit those three text fields. As of now, when the user edits one text field and clicks done. The information of the non-edited text fields appear in their own text fields and then the user has to click the done button again for the edited text field to be sent to Firebase:

 if City.text==""
            {
                FIRDatabase.database().reference().child("info").child(self.loggedInUser!.uid).observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in


                    if let postsDictionary = snapshot .value as? [String: AnyObject] {


                        let city = postsDictionary["City"] as? String ?? ""
                        self.City.text = city
                    }})

            }
            if Major.text==""
            {
                FIRDatabase.database().reference().child("info").child(self.loggedInUser!.uid).observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in


                    if let postsDictionary = snapshot .value as? [String: AnyObject] {


                        let major = postsDictionary["Major"] as? String ?? ""
                        self.Major.text = major
                    }})

            }
            if College.text==""
            {
                FIRDatabase.database().reference().child("info").child(self.loggedInUser!.uid).observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in


                    if let postsDictionary = snapshot .value as? [String: AnyObject] {


                        let college = postsDictionary["College"] as? String ?? ""
                        self.College.text = college
                    }})

            }
            if College.text=="" || Major.text=="" || City.text==""
            {
                FIRDatabase.database().reference().child("info").child(self.loggedInUser!.uid).observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in

                    })

            }else{


            if let City = City.text{
                if let Major = Major.text{
                    if let College = College.text{

                        let saveObject: Dictionary<String, Any> = [
                            "uid": uid,
                            "City" : City,
                            "Major" : Major,
                            "College" : College
                        ]
                        FIRDatabase.database().reference().child("info").child(self.loggedInUser!.uid).setValue(saveObject)

I don't think this is very user-friendly, I would like the user to edit whatever text field they want and I do not want the non-edited text field values to appear in their text fields and I would like the user to click on the done button once for the newly edited text field value to be sent to firebase.


回答1:


Just create this func in viewdidload. This will load your current properties in firebase. You should adopt the UITextFieldDelegate and add the function textDidBegin which then you can make the textField (sender as! textField class) blank instead of having these loaded properties.

        func reloadFirDatabase() 
        {  FIRDatabase.database().reference().child("info").child(self.loggedInUser!.uid).observeSingleEvent(of: .value, with: { (snapshot:FIRDataSnapshot) in


                          if let postsDictionary = snapshot .value as? [String: AnyObject] {
                                        let city = postsDictionary["City"] as? String ?? ""
                                        let major = postsDictionary["Major"] as? String ?? ""
                                        let college = postsDictionary["College"] as? String ?? ""
                         if City.text.isEmpty {
                                    self.City.text = city    
                         {
                         if Major.text.isEmpty {
                                    self.Major.text = major
                         }
                         if College.text.isEmpty {
                                    self.College.text = college
                         } 


                                    }})
            }

When you press done you should still load this in:

if let City = City.text{
                if let Major = Major.text{
                    if let College = College.text{

                        let saveObject: Dictionary<String, Any> = [
                            "uid": uid,
                            "City" : City,
                            "Major" : Major,
                            "College" : College
                        ]
                        FIRDatabase.database().reference().child("info").child(self.loggedInUser!.uid).setValue(saveObject)

I can guarantee you that doing it this way you only need to press the done button once.



来源:https://stackoverflow.com/questions/44600680/sending-firebase-value-to-text-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!