SwiftUI->线程1:致命错误:找不到类型MyObject.Type的可观察对象 [英] SwiftUI -> Thread 1: Fatal error: No observable object of type MyObject.Type found

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

问题描述

我正在使用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的地图视图"的模式时,会发生此错误.

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

我是Swift编程和SwiftUI的新手.错误消息可能对您很清楚,但对我来说却不是.

I'm new to Swift Programming and SwiftUI. The error message may be clear for you, but not really for my.

我不知道为什么以及如何实现新的环境对象.

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

是因为我点击CardView时选择的station应该被全局存储并将信息传递到专用视图吗?

Is it because the station I select when taping on the CardView should be stored globally and pass the info to the dedicated 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)
        }

    }
}

我要在工作表中显示的视图

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() {

        }        
    }
}

我可以得到错误消息并显示正确的视图吗?

I can I get ride 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的可观察对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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