我目前在一个名为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
}
})
}
我怎样才能以最简单,最简单的方式传递这些数据?
答案 0 :(得分:1)
你需要在你的表中加载主控制器的doselect方法中的数据,并将字典传递给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。
答案 1 :(得分: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
中访问它并将该数据设置为lable
。
希望这将清除您的数据传递概念。