将Firebase值发送到文本字段 [英] sending Firebase value to text fields

查看:177
本文介绍了将Firebase值发送到文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图控制器有三个文本字段。当用户第一次使用应用程序时,他们在这些文本字段中输入一些信息,然后按完成,然后将信息发送到Firebase数据库:

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

但是,用户也可以编辑这三个文本字段。截至目前,当用户编辑一个文本字段,并点击完成。未编辑的文本字段的信息出现在他们自己的文本字段中,然后用户必须再次单击完成按钮才能将编辑的文本字段发送到Firebase:

< pre $ 如果City.text ==
{
FIRDatabase.database()。reference()。child(info)。child(self.loggedInUser !.uid).observeSingleEvent(of:.value,with:{(snapshot:FIRDataSnapshot)in

$ b如果let postsDictionary = snapshot .value as?[String:AnyObject] {


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


$如果Major.text ==
{
FIRDatabase.database()。reference()。child(info)。child(self.loggedInUser!.uid) .observeSingleEvent(:.value,其中:{(snapshot:FIRDataSnapshot)in


如果let postsDictionary =快照。值为? [String:AnyObject] {


let major = postsDictionary [Major] as?字符串?
self.Major.text = major
}})

}
如果College.text ==
{

$ b $中有一个childDatabase()。reference()。child(info)。child(self.loggedInUser!.uid).observeSingleEvent(of:.value,与:{(snapshot:FIRDataSnapshot) b
如果让postsDictionary = snapshot .value as?[String:AnyObject] {


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

}
如果College.text ==|| Major.text ==|| City.text ==
{
FIRDatabase.database()。reference()。child(info)。child(self.loggedInUser!.uid).observeSingleEvent(of:.value,在


$ b其他$ {


如果允许City = City.text {:快照:FIRDataSnapshot)
如果让Major = Major.text {
如果让College = College.text {

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

我不认为这是非常用户友好的,我希望用户编辑他们想要的任何文本字段,我不希望非编辑的文本字段值出现在他们的文本字段,我希望用户点击完成按钮一次为新编辑的文本字段值发送到firebase。

解决方案

只需在viewdidload中创建这个func即可。这将加载您的当前属性在firebase。您应该采用UITextFieldDelegate并添加函数textDidBegin,然后您可以使textField(发件人为!t​​extField类)为空,而不是具有这些加载的属性。

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

$ b如果让postsDictionary = snapshot .value as?[String: AnyObject] {
let city = postsDictionary [City] as?String ??
let major = postsDictionary [Major] as?String ??
let college = postsDictionary [College] as?String ??
如果City.text.isEmpty {
self.City.text = city
{
如果Major.text.isEmpty {
self.Major.text = major
}
如果College.text.isEmpty {
self.College.text = college
}


}})
}

当您按下完成时,您仍然应该加载:

 如果让City = City.text {
如果让Major = Major.text {
如果让College = College.text {

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

我可以保证你这样做,你只需要按一下done按钮。


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.

解决方案

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.

这篇关于将Firebase值发送到文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆