从视图控制器中的单元访问Firebase数据 [英] Accessing Firebase data from cell in view controller

查看:123
本文介绍了从视图控制器中的单元访问Firebase数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个图片,三个UILabels和一个单元格中的TextView,并希望在单独的视图控制器中使用这些数据。视图控制器目前通过push segue连接到单元格,我该怎么做才能做到这一点?



我有这个,但它不起作用:

  let uid = FIRAuth.auth()?currentUser?.uid 
FIRDatabase.database()。reference (users)。child(uid!)。observeSingleEvent(of:.value,with:{(snapshot)in
如果让dictionary = snapshot.value as?[String:AnyObject] {
self .userLabel.text = dictionary [username] as?String
}
})


解决方案

我实际上能够找到一种方法来做到这一点。实际上很简单

prepareForSegue


$ b

  override func prepare(for segue :UIStoryboardSegue,发件人:任何?){
如果segue.identifier ==showDetails{
如果let indexPath = self.postsTableView.indexPathForSelectedRow {
let post = posts [indexPath.row]如! [String:AnyObject]
let postDetails = post [postID] as?字符串
让controller = segue.destination as! PostDetailsViewController
controller.postDetails = postDetails
}
}
}



PostDetailsViewController

  var postDetails:String? 

覆盖func viewDidLoad(){
super.viewDidLoad()
//在加载视图之后执行其他任何设置。

FIRDatabase.database()。reference()。child(posts)。child(postDetails!)。observeSingleEvent(.value,其中:{(snapshot)in
if让dictionary = snapshot.value as?[String:AnyObject] {
self.idLabel.text = dictionary [postID] as?String
}
})
}


I currently have a picture, three UILabels, and a TextView in a cell and would like to use that data in a separate view controller. The view controller is currently connected via a push segue to the cell, what should I do make this happen?

I have this but it doesn't work:

let uid = FIRAuth.auth()?.currentUser?.uid
    FIRDatabase.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in
        if let dictionary = snapshot.value as? [String: AnyObject] {
            self.userLabel.text = dictionary["username"] as? String
        }
    })

解决方案

I was actually able to find a way to do this. It's quite simple actually

prepareForSegue

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showDetails" {
        if let indexPath = self.postsTableView.indexPathForSelectedRow {
            let post = posts[indexPath.row] as! [String: AnyObject]
            let postDetails = post["postID"] as? String
            let controller = segue.destination as! PostDetailsViewController
            controller.postDetails = postDetails
        }
    }
}

PostDetailsViewController

var postDetails: String?

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    FIRDatabase.database().reference().child("posts").child(postDetails!).observeSingleEvent(of: .value, with: { (snapshot) in
        if let dictionary = snapshot.value as? [String: AnyObject] {
            self.idLabel.text = dictionary["postID"] as? String
        }
    })
}

这篇关于从视图控制器中的单元访问Firebase数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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