将Firebase数据从View Controller发送到View Controller

时间:2017-02-26 23:53:28

标签: ios swift firebase firebase-realtime-database segue

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

这就是我目前设置故事板的方式: 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
         }
    })
}

我怎样才能以最简单,最简单的方式传递这些数据?

2 个答案:

答案 0 :(得分:1)

你需要在你的表中加载主控制器的doselect方法中的数据,并将字典传递给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。

答案 1 :(得分:1)

根据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中访问它并将该数据设置为lable

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