模态表达问题

时间:2019-09-09 15:09:34

标签: swift swiftui xcode11

我正在尝试从课程视图的以下代码中获取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

1 个答案:

答案 0 :(得分:0)

首先,您要尝试遍历HomeList中的课程列表,但每个课程都没有定义。

此行:

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

在关闭主体声明的括号之前,应该做的最后一件事。

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