【发布时间】:2017-02-26 23:53:28
【问题描述】:
我目前在名为MainViewController 的 ViewController 中有一个 UITableView,它从 Firebase 数据库加载我的所有内容。我还有另一个名为 DetailsViewController 的 ViewController,它从通过 segue 单击的任何单元格加载数据。我要做的是创建另一个名为 CommentViewController 的 ViewController 并从之前单击的同一单元格加载信息(Segue 来自 DetailsViewController)。
这就是我目前将数据从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