2017-02-26 16 views
-2

現在、FireboxデータベースからすべてのコンテンツをロードするMainViewControllerというViewController内にUITableViewがあります。また、別のViewControllerという名前のDetailsViewControllerがあります。これは、セグを介してクリックされたセルからデータをロードします。私がやろうとしているのは、CommentViewControllerという名前の別のViewControllerを作成し、前にクリックした同じセルから情報をロードすることです(SegueはDetailsViewControllerから来ます)。View ControllerからView ControllerにFirebaseデータを送信

これは私が現在、私のストーリーボードのセットアップを持っているかである:これは私は現在、私はほとんどの最も簡単にこのデータを渡すことができますどのように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 
     } 
    }) 
} 

にデータをロードしています方法です enter image description here

と最も簡単な方法は?

答えて

1

メインコントローラのテーブルdidselectメソッドにデータをロードし、ディクショナリをdetailsControllerに渡してすべてのアウトレットに値を渡す必要があります。メインコントローラでdictDetails辞書を取るために必要なコード

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 

    } 

、以下を参照してください。

+0

私は混乱して申し訳ありませんが、すでにDetailsViewControllerのデータを渡していますが、CommentsViewController – gabe

+0

にデータを渡す方法を知りたいと思います。 commentViewControllerでpublic変数を取得し、detailsViewコントローラのpreparesegueにその値を渡します。 – sschunara

1

sschunaraの回答として、私はあなたの答えに余分なコードを追加させてください。

あなただけaginあなたが今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 

    } 
} 

峠辞書全体または単にコメントなどの同じデータを渡す必要があり、あなたの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 

    } 

内のすべての詳細を持っていますあなたの要件に応じてあなたの文字列にアクセスして、あなたのCommnetsViewControllerにその文字列にアクセスし、それをataをlableに入力します。

希望すると、データの受け渡しの概念がクリアされます。

関連する問題