gpt4 book ai didi

swift - 如何将帖子 ID 传递给评论 VC - Swift

转载 作者:行者123 更新时间:2023-11-30 10:58:15 26 4
gpt4 key购买 nike

我无法将 postid 发送到 comments vc,以便将其保存在 Firebase 中。

我正在遵循我们正在构建的在线教程 this Firebase 上的架构但是当我将数据发送到 Firebase 时,它​​没有捕获 postId,如 this screenshot 上所示。 。我显然错过了一些东西。如果有人能阐明我的错误可能是什么,我将非常感激。下面我将放置 Comments View Controller 函数,将数据发送到 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)
}
}

这就是我从主视图 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
}
}

这是我的 Collection View 扩展

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)

}
}

}

最佳答案

问题看起来就在这段代码中

let postId = sender as! String
commentVC.postIdNew = postId

Sender 指启动segue的对象,很可能是一个按钮或TableViewCell(请参阅苹果文档:https://developer.apple.com/documentation/uikit/uiviewcontroller/1621490-prepare)

您需要将 sender 替换为! String 包含您尝试设置的 postId 的值。我假设您可以从第一个 View Controller 上的某个地方获取它。

关于swift - 如何将帖子 ID 传递给评论 VC - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53709378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com