[英]Passing a document reference to a second view controller
Can someone please show me how I can make it so that when I click on a specific row in a tableView, a document reference is passed to a second ViewController allowing me to show the fields inside the subcollection "Friends". 有人可以告诉我如何做到这一点,以便当我单击tableView中的特定行时,文档引用将传递给第二个ViewController,使我可以显示子集合“ Friends”中的字段。 At the moment I can do this but without using autoID.
目前,我可以执行此操作,但无需使用autoID。 Please may someone how I can do this using an autoID?
请有人可以使用自动识别码来做到这一点吗? Any help would be greatly appreciated, many thanks!!
任何帮助将不胜感激,非常感谢!
Current Firebase Console 当前的Firebase控制台
What I would like - Firebase Console 我想要的-Firebase控制台
First ViewController 第一个ViewController
func loadData() {
db.collection("Users").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
let data = document.data()
let name = data["name"] as? String ?? ""
let newName = UsersNames(name: name)
self.nameArray.append(newName)
}
self.tableView.reloadData()
}
}
}
Second ViewController 第二个ViewController
func loadData() {
db.collection("Users").document("Hello").collection("Friends").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
let data = document.data()
let name = data["name"] as? String ?? ""
let details = data["details"] as? String ?? ""
let newFriends = Friends(friendName: name, friendDetails: details)
self.friendsArray.append(newFriends)
}
self.tableView.reloadData()
}
}
}
If you want to do it that way, firstly you need to add a documentID property to your UsersNames
object: 如果要这样做,首先需要将documentID属性添加到
UsersNames
对象:
struct UsersNames {
var documentID: String //<-- Add this
var name: String
}
Then update your loadData()
function in your First VC to get the documentID
from each Firestore
document and append in into your Array
: 然后更新您的
loadData()
在你的第一个VC函数来获取documentID
从各Firestore
文件,并附加到你的Array
:
for document in querySnapshot!.documents {
let data = document.data()
let documentID = document.documentID //<-- Add this
let name = data["name"] as? String ?? ""
let newName = UsersNames(documentID: documentID, name: name) //<-- Change this
self.nameArray.append(newName)
}
In First VC, you want to perform a Segue
to your Second VC when a cell is selected, and pass the documentID
for the selected object in your Array
to the Second VC. 在First VC中,要在选择单元格时对第二VC执行
Segue
,然后将Array
所选对象的documentID
传递给Second VC。
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "SecondVC", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let indexPath = tableView.indexPathForSelectedRow {
let destinationVC = segue.destination as! SecondVC
let documentId = nameArray[indexPath.row].documentID
destinationVC.documentID = documentID
}
}
In your SecondVC create a property to receive the documentID: 在您的SecondVC中,创建一个属性以接收documentID:
var documentID: String!
In your SecondVC loadData()
function, you can now access the documentID that was passed from your First VC: 在您的SecondVC
loadData()
函数中,现在可以访问从第一个VC传递来的documentID:
db.collection("Users").document(documentID).collection("Friends").getDocuments() { //..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.