我正在尝试更新的ProductViewController
上有一个名为 idLabel 的标签。以下是发送信息的VC
。有一次我按下 playThis 按钮时文本更新,标签会从股票标签文本变为零,所以我知道发生了什么事情,但现在它在{{1}它只显示没有。如何让它在接收端显示文字?
这是发送VC:
viewDidLoad
以下是import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
import FirebaseStorage
class ListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableViewProducts: UITableView!
var delegate: ListViewController?
var ref:DatabaseReference?
var databaseHandle: DatabaseHandle?
var postData = [String]()
override func viewDidLoad() {
super.viewDidLoad()
ref = Database.database().reference()
databaseHandle = ref?.child("0").observe(.childAdded, with: { (snapShot) in
let post = snapShot.value as? String
if let actualPost = post {
self.postData.append(actualPost)
self.tableViewProducts.reloadData()
}
})
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(postData)
return postData.count
}
//This places the text on the ViewControllerTableViewCell which does show up
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
cell?.textLabel?.text = postData[indexPath.row]
cell?.textLabel?.textColor = UIColor.white
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetails", sender: self)
}
//This code is what sends the text data elswhere
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? ProductViewController {
destination.updater?.id = postData[(tableViewProducts.indexPathForSelectedRow?.row)!]
}
}
}
:
ProductViewController
答案 0 :(得分:0)
如果您只是计划传递id而不是对象,那么请使用一个简单的变量。
您准备segue的代码将成为
destination.variable = id_to_pass.
现在您在ProductViewController中获得了id,您可以在viewdidload
中更新它答案 1 :(得分:-1)
ListViewController
中updater
的设置值出现问题
您正在将值设置为尚未初始化的对象。
//This code is what sends the text data elswhere
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? ProductViewController {
destination.updater?.id = postData[(tableViewProducts.indexPathForSelectedRow?.row)!]
}
}
按以下方式更新代码
//This code is what sends the text data elswhere
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? ProductViewController,
let index = tableViewProducts.indexPathForSelectedRow?.row {
// Check whether postData array count is greater than index
let updaterId = postData.count > index ? postData[index] : ""
// Initialize "productsList" instance and assign the id value and sent this object to next view controller
var updater = productsList()
updater.id = updaterId
destination.updater = updater
}
}