使用NavigationLink(destination: ProductDetailsView(displayedProduct_id: detaliiProdus.id))
,将detaliiProdus.id
从mainview
传递到ProductDetailsView
。
我的ProductDetailsView
外观:
struct ProductDetailsView: View {
var displayedProduct_id = ""
@ObservedObject var displayedProduct = ReadProductDetail(productID: "9gioS1HgMl")
var body: some View {
VStack{
Image(uiImage: displayedProduct.poza_principala)
Text(displayedProduct.tip_vanzare)
Text(displayedProduct.titlu_produs)
etc.}
我的问题是如何使用displayedProduct_id
来显示产品详细信息,而不是使用我现在拥有的硬编码值
要阅读我已经上过课的信息:
class ReadProductDetail: ObservableObject {
@Published var poza_principala : UIImage = UIImage(imageLiteralResourceName: "logo")
@Published var tip_vanzare = ""
@Published var titlu_produs = ""
@Published var pret = ""
@Published var marime = ""
@Published var gen = ""
@Published var descriere = ""
@Published var user = ""
@Published var telefon = ""
@Published var rezultat = [DisplayedProductDetailes]()
var productID = ""
init(productID: String) {
let query = PFQuery(className:"Produse")
query.includeKey("poza_principala")
query.includeKey("tip_vanzare")
query.includeKey("marime")
query.includeKey("gen")
query.includeKey("user")
query.getObjectInBackground(withId: productID) { (object, error) in
if error == nil && object != nil {
let poza_p_main1 = object!["poza_principala"] as! PFObject
let poza_p_main2 = poza_p_main1["poza_principala"] as! PFFileObject
poza_p_main2.getDataInBackground { (imageData: Data?, error: Error?) in
if let error = error {
print(error.localizedDescription)
} else if let imageData = imageData {
self.poza_principala = UIImage(data:imageData)!
}
}
let tip_vanzare_main = object!["tip_vanzare"] as! PFObject
self.tip_vanzare = tip_vanzare_main["tip_vanzare"] as? String ?? "N/A"
self.titlu_produs = object!["titlu_produs"] as? String ?? "N/A"
self.pret = object!["pret"] as? String ?? "N/A"
let marime_main = object!["marime"] as! PFObject
self.marime = marime_main["marime"] as? String ?? "N/A"
let gen_main = object!["gen"] as! PFObject
self.gen = gen_main["gen"] as? String ?? "N/A"
self.descriere = object!["descriere"] as? String ?? "N/A"
let user_main = object!["user"] as! PFObject
self.user = user_main["username"] as? String ?? "N/A"
self.telefon = object!["telefon"] as? String ?? "N/A"
} else {
self.titlu_produs = "Indisponibil"
}
}
}//end init() }
我尝试使用@State
和@Binding
,但遇到一些错误。
答案 0 :(得分:0)
以下内容应为您工作
struct ProductDetailsView: View {
@ObservedObject var displayedProduct: ReadProductDetail
init(displayedProduct_id: String) {
self.displayedProduct = ReadProductDetail(productID: displayedProduct_id)
}
var body: some View {
// ... other code
}
}