我试图将包含嵌套对象数组的数据对象从“主”视图传递到“详细信息”视图。目前,我能够显示数据对象的父级,但是我正努力尝试找出如何将嵌套的数据数组传递到详细信息页面。以下是我的数据模型的示例,以及如何显示当前信息。我不确定我需要在NavigationLink元素中传递信息的地方键入什么代码。我确信可能有更好的方法来执行此操作,但是,我只是一名设计师,出于原型制作原因而试图弄清这一点。
// Passports.swift //
import Foundation
import SwiftUI
struct Passports: Identifiable {
let id : Int
let passportPremium: Bool
let passportActive: Bool
let passportTitle: String
let passportDates: String
let venues: [Venue]
}
struct Venue: Identifiable {
let id = UUID()
let title : String
let venueArea: String
let venueItems: [venueItem]
}
struct venueItem {
let title: String
let productDescription: String
let productPrice: Double
let productType: String
let newStatus: Bool
let diningPlan: Bool
let kidFriendly: Bool
let vegetarian: Bool
let glutenFree: Bool
let featuredProduct: Bool
let containsAlcohol: Bool
}
extension Passports {
static func all() -> [Passports] {
return [
Passports (
id: 1001,
passportPremium: false,
passportActive: true,
passportTitle : "Passport Title Example",
passportDates: "October 20 - November 3, 2019",
venues: [
Venue (
title: "Venue Name",
venueArea: "Germany",
venueItems: [
venueItem (
title: "Potato Dumpling",
productDescription: "Potato Dumpling with Mushroom Sauce",
productPrice: 0.00,
productType: "Food",
newStatus: false,
mealPlan: false,
kidApproved: true,
vegetarian: false,
glutenFree: false,
featuredProduct: false,
containsAlcohol: false
),
venueItem (
title: "Pork Schnitzel",
productDescription: "Pork Schnitzel with Mushroom Sauce and Spaetzle",
productPrice: 0.00,
productType: "Food",
newStatus: false,
mealPlan: false,
kidApproved: false,
vegetarian: false,
glutenFree: false,
featuredProduct: false,
containsAlcohol: false
)
])
]
)
]
}
}
// PassportsView //
import SwiftUI
struct PassportsView: View {
var model = Passports.all()
var body: some View {
NavigationView {
ForEach(self.model) { item in
NavigationLink (destination: PassportDetails(passportTitle: item.passportTitle, venues: [Venue()] ) ) {
GeometryReader { geometry in
HStack {
VStack(alignment: .leading) {
Text(item.passportTitle)
.fontWeight(.semibold)
.foregroundColor(Color.white)
.multilineTextAlignment(.leading)
Text(item.passportDates)
.foregroundColor(Color.white)
}.frame(width: geometry.size.width, height: 120)
.background(Color("Purple"))
.cornerRadius(6)
}
}
}.padding(.horizontal)
}
}
}
}
// PassportDetails //
struct PassportDetails: View {
var passportTitle = "Passport Name"
var venues: [Venue] = [
Venue(title: "Test Venue", venueArea: "Test Area", venueItems: [venueItem]())
]
var venueProd: [venueItem] = [
venueItem(title: "Test item", productDescription: "Test Description", productPrice: 9.99, productType: "Food", newStatus: false, mealPlan: true, kidApproved: false, vegetarian: false, glutenFree: false, featuredProduct: false, containsAlcohol: false)
]
var body: some View {
NavigationView {
List {
HStack {
Text("Test")
Spacer()
Text("9.99")
}
}
}.navigationBarTitle(Text(passportTitle))
}
}
答案 0 :(得分:0)
在PassportDetails
中执行以下操作:
var venues: [Venue]
并在Passports
中执行以下操作:
NavigationLink (destination: PassportDetails(passportTitle: item.passportTitle, venues: item.venues) )
PS:您应该对passportTitle进行相同的操作,这意味着在PassportDetails
中放置var passportTitle: String