模态呈现问题

Modal Presentation Issue

我正在尝试从课程视图的以下代码中获取 ContentView()。

.sheet(isPresented: self.$showContent) {ContentView()}

完整代码如下:

//
//  HomeList.swift
//  Design Code
//
//  Created by Govind Pathak on 09/09/19.
//  Copyright © 2019 Govind Pathak. All rights reserved.
//

import SwiftUI

struct HomeList: View {
    @State var showContent = false
    var body: some View {
        ScrollView(.horizontal , showsIndicators: false){
            HStack(spacing:30) {
                ForEach(courses) { item in
                    CourseView(
                        title: item.title,
                        image: item.image,
                        color: item.color,
                        shadowColor: item.shadowColor)
                        .sheet(isPresented: self.$showContent) {ContentView()}
                }
            } .padding(.leading , 30)
        }
    }
}
struct HomeList_Previews: PreviewProvider {
    static var previews: some View {
        HomeList()
    }
}
    struct CourseView: View {
    // @Environment(\.presentationMode) var presentationMode

    var title = "Build an app with SwiftUI"
    var image = "Illustration1"
    var color = Color("background3")
    var shadowColor = Color(red: 0.271, green: 0.235, blue: 0.788, opacity: 0.3)
    var body: some View {
        VStack(alignment: .leading) {
            Text(title)
                .font(.title)
                .fontWeight(.bold)
                .foregroundColor(Color.white)
                .padding(20)
                .lineLimit(4)
                .padding(.trailing , 50)


            Spacer()
            Image(image)
                .resizable()
                .renderingMode(.original)
                .aspectRatio(contentMode: .fit)
                .frame(width: 246, height: 150)
                .padding(.bottom , 30)
        }
        .background(color)
        .cornerRadius(30)
        .frame(width: 246, height: 360)
        .shadow(color:shadowColor , radius: 20 , x: 0, y: 20)
    }
}
struct Course : Identifiable {
    var id = UUID()
    var title: String
    var image: String
    var color: Color
    var shadowColor: Color
}
let coursesData = [
    Course(title: "Build an app with SwiftUI",
           image: "Illustration1",
           color: Color("background3"),
           shadowColor: Color("backgroundShadow3")),
    Course(title: "Design Course",
           image: "Illustration2",
           color: Color("background4"),
           shadowColor: Color("backgroundShadow4"))
]

var courses = coursesData

首先,您尝试使用 for each 遍历 HomeList 中的课程列表,但未在任何地方定义课程。

这一行:

.sheet(isPresented: self.$showContent) { ContentView() }

应该作为最后一件事,然后再关闭主体声明的括号。

您还需要一种将 showContent 值更改为 true 的方法。