我使用 SwiftUI 创建了一个应用程序,当我尝试显示按钮时,会出现以下错误消息:
Thread 1: Fatal error: No ObservableObject of type ModelData found. A View.environmentObject(_:) for ModelData may be missing as an ancestor of this view.
Run Code Online (Sandbox Code Playgroud)
当我尝试使用 an@EnvironmentObject尝试显示我的应用程序的视图之一时,会发生这种情况。
我的代码是
struct OpportunityDetail: View {
@EnvironmentObject var modelData: ModelData
var opportunity: Opportunity
var opportunityIndex: Int {
modelData.opportunities.firstIndex(where: { $0.id == opportunity.id })!
}
var body: some View {
ScrollView {
MapView(coordinate: opportunity.locationCoordinate)
.frame(height: 300)
.ignoresSafeArea(edges: .top)
CircleImage(opportunity: opportunity)
.offset(y: -130)
.padding(.bottom, -130)
VStack {
VStack(alignment: .leading) {
Text(opportunity.position)
.font(.title)
HStack …Run Code Online (Sandbox Code Playgroud)