从视图控制器发送Firebase数据到视图控制器

从视图控制器发送Firebase数据到视图控制器

问题描述:

我目前在ViewController中有一个名为MainViewController的UITableView,它从Firebase数据库加载所有内容。我还有另一个名为DetailsViewController的ViewController,它可以加载通过segue点击的任何单元格的数据。我想要做的是创建另一个名为CommentViewController的ViewController,并从之前点击过的同一单元格中加载信息(Segue来自DetailsViewController)。从视图控制器发送Firebase数据到视图控制器

这是我现在有我的故事板设置: enter image description here

这是怎么了,我目前从MainViewController加载数据到DetailsViewController

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     performSegue(withIdentifier: "showDetails", sender: self) 
} 

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! DetailsViewController 
      controller.postDetails = postDetails 
     } 
    } 
} 

// DetailsViewController 
var postDetails: String? 

override func viewDidLoad() { 
    super.viewDidLoad() 
    FIRDatabase.database().reference().child("posts").child(postDetails!).observeSingleEvent(of: .value, with: { (snapshot) in 
     if let dictionary = snapshot.value as? [String: AnyObject] { 
      self.title = dictionary["title"] as? String 
      self.priceLabel.text = dictionary["price"] as? String 
      self.ratingLabel.text = dictionary["rating"] as? String 
      self.usernameLabel.text = dictionary["username"] as? String 
      self.detailsTextView.text = dictionary["description"] as? String 
     } 
    }) 
} 

我怎么能以最简单的通过这个数据和最简单的方法?

您需要在您的表的主控制器didselect方法中加载数据并将字典传递给detailsController并为所有插座赋值。见下面代码

MainController 

var dictDetails: [String:AnyObject]? 
. 
. 
. 
. 
. 



func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
      let post = posts[indexPath.row] as! [String: AnyObject] 
       let postDetails = post["postID"] as? StringFIRDatabase.database().reference().child("posts").child(postDetails!).observeSingleEvent(of: .value, with: { (snapshot) in 
      if let dictionary = snapshot.value as? [String: AnyObject] { 
        self.dictDetails = dictionary 
        performSegue(withIdentifier: "showDetails", sender: self) 
      } 
     }) 

    } 

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "showDetails" { 

       let controller = segue.destination as! DetailsViewController 
       controller.dictionary = self.dictDetails 

     } 
    } 

    // DetailsViewController 
    var dictionary: [String:AnyObject]? 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.title = self.dictionary["title"] as? String 
       self.priceLabel.text = self.dictionary["price"] as? String 
       self.ratingLabel.text = self.dictionary["rating"] as? String 
       self.usernameLabel.text = self.dictionary["username"] as? String 
       self.detailsTextView.text = self.dictionary["description"] as? String 

    } 

需要在mainController中使用dictDetails Dictionary。

+0

我很抱歉有些混淆,但我已经为DetailsViewController传递了数据,但是想知道如何将数据传递给CommentsViewController – gabe

+0

您从主要细节传递给细节的方式相同。在commentViewController中使用公共变量,并在detailsView控制器中为preparesegue赋予它的值。 – sschunara

根据sschunara的答案,让我为你的答案添加额外的代码。

你把所有的细节在你DetailViewController's字典

// DetailsViewController 
    var dictionary: [String:AnyObject]? 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.title = self.dictionary["title"] as? String 
       self.priceLabel.text = self.dictionary["price"] as? String 
       self.ratingLabel.text = self.dictionary["rating"] as? String 
       self.usernameLabel.text = self.dictionary["username"] as? String 
       self.detailsTextView.text = self.dictionary["description"] as? String 

    } 

只是阿恩你必须通过数据一样像MainViewController-> Detail ViewController

现在DetailViewController -> CommentViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "showComments" { 

      let controller = segue.destination as! CommentViewController 
      controller.dictionary = self.dictDetails 

    } 
} 

通行证整部字典,或只是评论字符串根据您的要求,你可以访问它在你的CommnetsViewController并设置该d ata到lable

希望这会清除您的数据传递概念。