找不到类型的 ObservableObject [英] No ObservableObject of type found

查看:33
本文介绍了找不到类型的 ObservableObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用模态表从主视图快速访问包含收藏夹项目的列表.最喜欢的对象保存在 EnvironmentObject 数组中.在模式表中有一个按钮,您基本上可以在其中从收藏夹列表中添加/删除对象.但是,当您删除项目时,EnvironmentObject 变空并且应用程序崩溃:

I'm trying to quick access a list with favourite items from the master view with a modal sheet. The favourite objects are kept in an EnvironmentObject array. In the modal sheet there is a button, where you can basically add/remove the object from the favourites list. However, when you remove an item, the EnvironmentObject gets empty and the app crashes:

线程 1:致命错误:未找到 FavouritesList 类型的 ObservableObject.

Thread 1: Fatal error: No ObservableObject of type FavouritesList found.

在日志中它说:

FavouritesListView.environmentObject(_:) 作为此视图的祖先可能会丢失.:文件

A View.environmentObject(_:) for FavouritesList may be missing as an ancestor of this view.: file

如何确保它自然返回到 ContentView?

How do I ensure it goes back to the ContentView naturally?

import SwiftUI

struct ContentView: View {
    @EnvironmentObject var favouriteList: FavouritesList
    @State private var presentingSheet = false

    var body: some View {
        NavigationView {
            List {
                NavigationLink(destination: JudgementsView()) {
                    Text("Judgements")
                }
                NavigationLink(destination: SecondaryView()) {
                    Text("Secondary acts")
                }
                ScrollView(.horizontal, showsIndicators: false) {
                    VStack {
                        if favouriteList.items.isEmpty {
                            Text("Nothing favoured")
                        } else {
                            ForEach(favouriteList.items, id: \.self) { id in
                                VStack {
                                    HStack {
                                        ForEach(judgementsTAXraw.filter {
                                            $0.id == id
                                        }) { judgement in
                                            NavigationLink(destination: FileViewer(file: judgement.id)) {
                                                Button(judgement.title) {
                                                    self.presentingSheet = true

                                                }.sheet(isPresented: self.$presentingSheet) {
                                                    ModalSheet(file: judgement.CELEX)

                                                }
                                            }

                                        }
                                    }
                                    HStack {
                                        ForEach(secondaryTAXraw.filter {
                                            $0.id == id
                                        }) { secondary in
                                            NavigationLink(destination: FileViewer(file: secondary.id)) {
                                                Text(secondary.title).padding()
                                            }

                                        }
                                    }

                                }


                            }
                        }

                    }
                }

            }
            .navigationBarTitle(Text("Test"))
        }
    }
}

struct ModalSheet: View {
    var file: String

    @State private var showCopySheet = false

    @EnvironmentObject var favouriteList: FavouritesList
    var body: some View {
        NavigationView {
            Text("Modal").navigationBarItems(trailing:
                Button(action: {
                    self.showCopySheet = true
                }) {
                    Image(systemName: "doc.on.doc").frame(minWidth: 40)
                }.actionSheet(isPresented: $showCopySheet) {
                    ActionSheet(title: Text("What do you want to do?"), buttons: [
                        .destructive(Text("favText"), action: {
                            if let index = self.favouriteList.items.firstIndex(of: self.file) {

                                self.favouriteList.items.remove(at: index)


                            } else {
                                self.favouriteList.items.append(self.file)

                            }
                        }),
                        .cancel()
                    ])
                }.frame(minWidth: 50)
            )
        }
    }
}

推荐答案

我认为你需要像 environmentObject 一样将你的 favouriteList 传递给 ModalSheet试试

I think you need to pass your favouriteList to ModalSheet like environmentObject try

ModalSheet(file: judgement.CELEX).environmentObject(favouriteList)

这篇关于找不到类型的 ObservableObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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