【问题标题】:SwiftUI - Pass Nested Array of Objects to Details ViewSwiftUI - 将嵌套的对象数组传递给详细信息视图
【发布时间】:2023-03-27 18:00:01
【问题描述】:

我正在尝试将包含嵌套对象数组的数据对象从主视图传递到详细信息视图。目前,我能够显示数据对象的父级别,但我正在努力弄清楚如何将嵌套的数据数组传递到详细信息页面。下面是我的数据模型的示例,以及我当前将如何显示信息。我不确定我需要为在 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)
            }
        }
    }
}

// 护照详情 //

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))
}
}

【问题讨论】:

    标签: arrays swiftui pass-data


    【解决方案1】:

    在PassportDetails 中执行以下操作:

    var venues: [Venue]

    在Passports 中这样做:

    NavigationLink (destination: PassportDetails(passportTitle: item.passportTitle, venues: item.venues) )
    

    PS:你应该对passportTitle做同样的事情,意思是PassportDetails put var passportTitle: String

    【讨论】:

    • 基于上述建议,现在我试图弄清楚如何在列表中显示场地数组。 List(self.venues) { ven in NavigationLink () { HStack { Text(ven.title) } } }
    • 你遇到了什么问题?
    • 当我尝试在 Text 元素中显示值时,出现错误“表达式类型不明确,没有更多上下文”。
    • 您可以提出另一个问题并添加更多信息,我可以尝试在那里提供帮助
    猜你喜欢
    • 2021-06-21
    • 2019-12-24
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多