2017-02-11 6 views
0

現在、画像に3つのUILabelsと1つのTextViewがあり、そのデータを別のビューコントローラで使用したいと考えています。 View Controllerは現在、プッシュセグを介してセルに接続されていますが、これを行うにはどうすればよいですか?ビューコントローラのセルからのFirebaseデータへのアクセス

私はこれを持っているが、それは動作しません:

let uid = FIRAuth.auth()?.currentUser?.uid 
    FIRDatabase.database().reference().child("users").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in 
     if let dictionary = snapshot.value as? [String: AnyObject] { 
      self.userLabel.text = dictionary["username"] as? String 
     } 
    }) 

答えて

0

私は実際にこれを行うための方法を見つけることができました。それは実際には非常に簡単です

prepareForSegue

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

PostDetailsViewController私はDetailsViewController(別のビューコントローラ)とMainViewControllerに追加する必要がありますこれらの

var postDetails: String? 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view. 

    FIRDatabase.database().reference().child("posts").child(postDetails!).observeSingleEvent(of: .value, with: { (snapshot) in 
     if let dictionary = snapshot.value as? [String: AnyObject] { 
      self.idLabel.text = dictionary["postID"] as? String 
     } 
    }) 
} 
0

は、送信元と宛先のVCの両方にタイプ辞書の保存された変数を追加します。varユーザデータ:辞書を!あなたは辞書にスナップショットを変換した後、次にどれ

は、辞書データにソースVCのユーザーデータを設定し、

if let dictionary = snapshot.value as? [String: AnyObject] { 
    self.userLabel.text = dictionary["username"] as? String 
    self.userdata = dictionary 
    self.performSegue(withIdentifier: "<segueIDHere>", sender: self) 
} 

そして実装セグエを実行しますか、ユーザデータ=文字列を聞かせて空の辞書を初期化することができますVCは、データを渡すために、ソースのVCでセグエ方法の準備:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    if segue.identifier == "<segueIDHere>" { 
     let destinationVC = segue.destination as! <ClassNameofVCHere> 
     destinationVC.userdata = self.userdata 
    } 
} 
+0

? – gabe

関連する問題