SwiftUI->线程1:致命错误:找不到类型为MyObject.Type的可观察对象(工作表中的EnvironmentObject) [英] SwiftUI -> Thread 1: Fatal error: No observable object of type MyObject.Type found (EnvironmentObject in sheet)

查看:165
本文介绍了SwiftUI->线程1:致命错误:找不到类型为MyObject.Type的可观察对象(工作表中的EnvironmentObject)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SwiftUI构建应用程序.当我尝试显示工作表(以前是模态")时,出现此错误消息:

I'm building an app with SwiftUI. When I was trying to display a sheet (previously Modal), this error message appears:

Thread 1: Fatal error: No observable object of type BixiStationCombinedListViewModel.Type found.
A View.environmentObject(_:) for BixiStationCombinedListViewModel.Type may be missing as an ancestor of this view.

当我使用@State变量显示包含使用MapKit的Map View的模式时,会发生此错误.

This error occurs when I'm using a @State variable to display a modal that includes a Map View using MapKit.

我不知道为什么以及如何实施新的Environment Object.

I don't see why and how I should implement a new Environment Object.

是因为我在点击CardView时选择的Station应该被全局存储并且信息应该传递给专用的View吗?

Is it because the Station I select when tapping on the CardView should be stored globally and the info should be passed to the dedicated View?

View处理@State:

struct CardView: View {
    
    @EnvironmentObject var bixiModel: BixiStationCombinedListViewModel
    @State private var isModalOpen: Bool = false
        
    var station: BixiStationCombinedViewModel
        
    var body: some View {
        
        ZStack(alignment: .leading) {
            
                Card()
                
                StationTextInfo(station: station)

        } .onTapGesture {
            self.isModalOpen = true
            print(self.isModalOpen)
        }
        .sheet(isPresented: self.$isModalOpen) {
            BixiStationDetailView(station: self.station)
        }

    }
}

我要在工作表中显示的View:

The View I'm trying to show within the sheet:

struct BixiStationDetailView: View {
    
    @EnvironmentObject var bixiModel: BixiStationCombinedListViewModel
        
    var station: BixiStationCombinedViewModel
    
    var body: some View {
        VStack {
            MapView(coordinate: station.coordinate, name: station.name)        
        }
    }
}

最后一个对象:

class BixiStationCombinedListViewModel: ObservableObject {
    
    init() {
        fetchDataFromApi()
    }
    
    @Published var stationsCombinedList = [BixiStationCombinedViewModel]()
    
    var stationsInformationList = [BixiStationInformationViewModel]()
    var stationsDataList = [BixiStationDataViewModel]()
        
    func fetchDataFromApi() {

        }        
    }
}

如何摆脱错误消息并显示正确的View?

How can I get rid of the error message and display the proper View?

推荐答案

您必须将环境对象传递给BixiStationDetailView,否则它将没有任何内容绑定到其@EnvironmentObject.

You have to pass your environment object to BixiStationDetailView, otherwise it won't have anything to bind to its @EnvironmentObject.

.sheet(isPresented: self.$isModalOpen) {
    BixiStationDetailView(station: self.station)
        .environmentObject(self.bixiModel)
}

由于您要以表格形式显示BixiStationDetailView,因此它不是CardView的子视图,因此不会继承其@EnvironmentObject.

Since you're presenting BixiStationDetailView as a sheet, it isn't a subview of your CardView and therefore doesn't inherit its @EnvironmentObject.

这篇关于SwiftUI->线程1:致命错误:找不到类型为MyObject.Type的可观察对象(工作表中的EnvironmentObject)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆