我无法将postid发送到注释vc,以便可以将其保存在Firebase中。
我正在关注在线教程,我们在Firebase上构建this体系结构,但是当我将数据发送到Firebase时,它并没有捕获this screenshot上的postId。我显然缺少了一些东西。如果有人可以阐明我的错误可能会很感激。下面,我将在“注释视图控制器”功能中将数据发送到Firebase。
@IBAction func sendButtonPressed(_ sender: UIButton) {
let ref = Database.database().reference()
let commentsReference = ref.child("comments")
let newCommentId = commentsReference.childByAutoId().key
let newCommentReference = commentsReference.child(newCommentId)
//current user information
guard let currentUser = Auth.auth().currentUser else {
return
}
let currentUserId = currentUser.uid
newCommentReference.setValue(["userid": currentUserId, "commentText": commentTextField.text!]) { (error, ref) in
if error != nil {
ProgressHUD.showError(error!.localizedDescription)
return
}
let postCommentRef = Database.database().reference().child("post-comments").child(self.postIdNew).child(newCommentId)
postCommentRef.setValue(true, withCompletionBlock: { (error, ref) in
if error != nil {
ProgressHUD.showError(error!.localizedDescription)
return
}
})
self.empty()
self.view.endEditing(true)
}
}
这就是我应该从Home View Controller中获取postId引用的方式。
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "commentSegue" {
let commentVC = segue.destination as! CommentViewController
let postId = sender as! String
commentVC.postIdNew = postId
}
}
这是我的收藏夹视图扩展
extension HomeViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView == self.lostCollectionView {
return posts.count
}
if collectionView == self.foundCollectionView {
return newPostsFound.count
}
if collectionView == self.adoptionCollectionView {
return postsadoption.count
}
else {
return 0
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch collectionView {
case lostCollectionView:
let lostcell: LostCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Lostcell", for: indexPath) as! LostCollectionViewCell
let post = posts[indexPath.row]
let user = users[indexPath.row]
lostcell.post = post
lostcell.user = user
//Make TextView Clickable
lostcell.phoneLostTextView.isEditable = false;
lostcell.phoneLostTextView.dataDetectorTypes = UIDataDetectorTypes.phoneNumber
//Make Comments View Clickable
lostcell.homeVC = self
return lostcell
case foundCollectionView:
let foundcell: FoundCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Foundcell", for: indexPath) as! FoundCollectionViewCell
let post = newPostsFound[indexPath.row]
foundcell.post = post
//Make TextView Clickable
foundcell.phoneFoundTextView.isEditable = false;
foundcell.phoneFoundTextView.dataDetectorTypes = UIDataDetectorTypes.phoneNumber
return foundcell
case adoptionCollectionView:
let adoptioncell: AdoptionCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "Adopotioncell", for: indexPath) as! AdoptionCollectionViewCell
let post = postsadoption[indexPath.row]
adoptioncell.post = post
//Make TextView Clickable
adoptioncell.phoneAdoptionTextView.isEditable = false;
adoptioncell.phoneAdoptionTextView.dataDetectorTypes = UIDataDetectorTypes.phoneNumber
return adoptioncell
default:
return UICollectionViewCell()
}
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
switch collectionView {
case lostCollectionView:
let vc = storyboard?.instantiateViewController(withIdentifier: "lostSelectedViewController") as? LostSelectedViewController
self.navigationController?.pushViewController(vc!, animated: true)
vc?.posts = posts[indexPath.row]
break
case foundCollectionView:
let vc = storyboard?.instantiateViewController(withIdentifier: "foundSelectedViewController") as? FoundSelectedViewController
self.navigationController?.pushViewController(vc!, animated: true)
vc?.posts = newPostsFound[indexPath.row]
break
case adoptionCollectionView:
let vc = storyboard?.instantiateViewController(withIdentifier: "adoptionSelectedViewController") as? AdoptionSelectedViewController
self.navigationController?.pushViewController(vc!, animated: true)
vc?.posts = postsadoption[indexPath.row]
break
default:
break
}
func commentsViewPressed() {
print("Hola")
performSegue(withIdentifier: "commentSegue", sender: self)
}
}
}
答案 0 :(得分:0)
问题看起来像在此代码中
let postId = sender as! String
commentVC.postIdNew = postId
Sender
指的是发起segue的对象,因此可能是按钮或TableViewCell(请参阅apples docs:https://developer.apple.com/documentation/uikit/uiviewcontroller/1621490-prepare)
您需要将sender as! String
替换为您要设置的postId的值。我假设您可以从第一个视图控制器上的某处获得它。
答案 1 :(得分:0)
实际上我的错误不在这里。我的错误是我的snapshot.key没有检索到childByAutoId,因此当我运行查询时,它给了我一个匹配的子项列表,并且由于没有单个结果,SDK会打印出您查询的位置/集合的键:posts
我要做的就是检索必须使用此行的childByAutoId:
let key = childSnapshot.key as String
我在这个stackoverflow问题-> Firebase snapshot.key not returning actual key?
中找到了答案