【问题标题】:Send Firebase data from View Controller to View Controller将 Firebase 数据从 View Controller 发送到 View Controller
【发布时间】:2017-02-26 23:53:28
【问题描述】:

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

这就是我目前的 Storyboard 设置方式:

这就是我目前将数据从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
         }
    })
}

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

【问题讨论】:

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


    【解决方案1】:

    您需要在主控制器的表中加载数据 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字典。

    【讨论】:

    • 很抱歉造成混乱,但我已经很好地传递了 DetailsViewController 的数据,但想知道如何将数据传递给 CommentsViewController
    • 与从主要传递到细节的方式相同。在 commentViewController 中获取公共变量,并在 detailsView 控制器中的 preparesegue 中赋予它的值。
    【解决方案2】:

    根据 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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-15
      • 1970-01-01
      • 1970-01-01
      • 2019-11-10
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多