【发布时间】:2020-03-04 20:53:39
【问题描述】:
任何人都可以提供帮助,以便当用户单击客户名称(使用表格视图)时,他们的详细信息将出现在第二个视图控制器(VC)上。我认为这是因为我还没有将 documentID 拉到这里。我尝试将 documentID 添加到 Second VC .document(documentID)
初始 VC 代码
var customerList = [CustomerLsModel]()
override func viewDidLoad() {
super.viewDidLoad()
let db = Firestore.firestore()
db.collection("customers").getDocuments() { (snapshot, error) in
if let error = error{
print("Can't get customers: \(error)")
let alert = UIAlertController(title: "Error", message: "There was an issue in pulling your information. Please close the application and try again.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
self.present(alert, animated: true)
} else {
for document in snapshot!.documents{
let firstname = document["firstname"]
let lastname = document["lastname"]
let address = document["address"]
let customer = CustomerLsModel(firstname: firstname as! String?, lastname: lastname as! String?, address: address as! String?)
self.customerList.append(customer)
self.orderAndLimit()
}
self.customerTable.reloadData()
}
}
}
}
第二个风投
let db = Firestore.firestore()
db.collection("customers").document().getDocument { (document, Error) in
if let document = document, document.exists{
self.firstNameLabel.text = document.get("firstname") as? String
}
}
这是我的客户模型类
class CustomerLsModel{
var firstname: String!
var lastname: String?
var address: String?
init(firstname: String?, lastname: String?, address: String?){
self.firstname = firstname
self.lastname = lastname
self.address = address
}
【问题讨论】:
-
你的vc是怎么连接的?如果它们已经在内存中,您可以直接传递这些值,而无需在 firebase 再次请求它
-
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let vc = storyboard?.instantiateViewController(withIdentifier: "CustomerRecord") as? CustomerRecordViewController self.navigationController?.pushViewController(vc!, animated: true) } 这就是它们的连接方式
-
你为什么不通过 segue 传递值?
-
document("name of the document") 这里你必须包含文件的名称/id
标签: swift firebase uiviewcontroller google-cloud-firestore