2017-02-26 153 views
-2

我目前在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 
     } 
    }) 
} 

我怎麼能以最簡單的通過這個數據和最簡單的方法?

回答

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 Dictionary。

+0

我很抱歉有些混淆,但我已經爲DetailsViewController傳遞了數據,但是想知道如何將數據傳遞給CommentsViewController – gabe

+0

您從主要細節傳遞給細節的方式相同。在commentViewController中使用公共變量,並在detailsView控制器中爲preparesegue賦予它的值。 – sschunara

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並設置該d ata到lable

希望這會清除您的數據傳遞概念。

相關問題