【问题标题】:No ObservableObject of type <TypeName> found. A View.environmentObject(_:) for <TypeName> may be missing as an ancestor of this view未找到 <TypeName> 类型的 ObservableObject。 <TypeName> 的 View.environmentObject(_:) 作为该视图的祖先可能会丢失
【发布时间】:2021-05-22 15:59:56
【问题描述】:

我有三个视图(AddMatchView、TeamPickerView 和 TeamsOfCountryView)。一切都应该像这样工作:从 AddTeamView 我转到 TeamPickerView,在那里我选择国家并转到 TeamsOfCountryView,在点击我需要的团队后,两个视图(TeamPickerView 和 TeamsOfCountryView)都应该立即关闭,这要归功于公共 Bool 变量,并且选定的团队(Team 类型的对象)传递给父 AddMatchView。但是选择团队后,只关闭了TeamsOfCountryView,TeamPickerView出现错误:Fatal error: No ObservableObject of type DBService found。 DBService 的 View.environmentObject (_ :) 作为该视图的祖先可能会丢失。

在我决定为我的视图创建一个 ViewModel 之前,一切都正常运行。即,如果我将 @State 变量从 AddMatchView 转移到 TeamPickerView,那么错误不会很糟糕,但我使用来自 @ObservedObject 的属性

主父视图:

struct AddMatchView: View {
@ObservedObject var viewModel = AddMatchViewModel()

@State isPresented = false //This works!
@State team: Team? //This works!

var body: some View {
    //...
    Button(action: { viewModel.isPresented.toggle() }){
        //...
    }.fullScreenCover(isPresented: $viewModel.isPresented) { TeamPickerView(team: $viewModel.home, isPresented: $viewModel.isPresented)}
//.fullScreenCover(isPresented: $isPresented) { TeamPickerView(team: $home, isPresented: $isPresented)} THIS WORKS!!
//...
}

使用后出现错误的ViewModel:

class AddMatchViewModel: ObservableObject{
@Published var home: Team?
@Published var isPresented = false
//...
}

TeamsPickerView:

struct TeamPickerView: View {
    @EnvironmentObject var db: DBService 
    
    @Binding var team: Team?
    
    @Binding var isPresented: Bool

    @State private var searchText = ""
    
    var body: some View {
        NavigationView {
            VStack{
                SearchBar(text: $searchText)
                Form{
                    //ERROR in below line after selecting team in child TeamsOfCountryView: No ObservableObject of type DBService found. A View.environmentObject(_:) for DBService may be missing as an ancestor of this view.
                    List (db.countries.filter({ searchText.isEmpty ? true : $0.name.contains(searchText) })) { country in
                        NavigationLink(destination: TeamsOfCountryView(countryID: country.documentID, team: $team, isPresented: $isPresented)) {
                            HStack{
                                Image(uiImage: Flag(countryCode: country.code)!.image(style: .roundedRect)).resizable().scaledToFit().frame(maxWidth: 30, maxHeight: 30)
                                Text(country.name)
                            }
                        }
                    }
                }
                
            }
            .navigationBarTitle(Text("Countries"))
            .navigationBarItems(trailing: Button(action: {isPresented = false }){
                Text("Close")
            })
        }
    }
}

TeamsOfCountryView:

struct TeamsOfCountryView: View {
    @EnvironmentObject var db: DBService
    
    @Binding var team: Team?

    @Binding var isPresented: Bool

    //...

    var body: some View {
        VStack{
            //...
            Form{
                List (teams.filter({ searchText.isEmpty ? true : $0.name.contains(searchText) })) { team in
                    Button(action: {
                        //After that, an error occurs in the parent TeamPickerView
                        self.team = team
                        self.isPresented = false
                    }){
                        //...
                    }
                }
            }
            //...
        }
    }

小更新:尽可能简化示例

class ViewModel: ObservableObject{
    @Published var isPresented = false
}

class EnvObj: ObservableObject{
    @Published var foo = "test"
}

struct ContentView: View {
    @ObservedObject var viewModel = ViewModel()

    //@State var isPresented = false   no error if we use it instead of viewModel.isPresented
    
    var body: some View {
        Button("Open Child View A"){
            viewModel.isPresented.toggle()
        }.fullScreenCover(isPresented: $viewModel.isPresented){ChildViewA(isPresented: $viewModel.isPresented)}
    }
}

struct ChildViewA: View {
    @EnvironmentObject var envObj: EnvObj
    @Binding var isPresented: Bool
    
    @State var openChildB = false

    var body: some View {
        NavigationView{
            VStack{
            //ERROR HERE: No ObservableObject of type EnvObj found. A View.environmentObject(_:) for EnvObj may be missing as an ancestor of this view.
            Text(envObj.foo)
            NavigationLink(destination: ChildViewB(isPresented: $isPresented)){
                Text("Open Child View B")
                
            }
            }
        }
    }
}

struct ChildViewB: View {
    @EnvironmentObject var envObj: EnvObj
    @Binding var isPresented: Bool
    
    var body: some View {
        Button("Close Child View A and B"){
            isPresented = false
        }
    }
}

【问题讨论】:

  • ContentView 的初始化是什么样的? (在 SceneDelegate 或等效项中)这是您要注入 DBService 对象的位置。
  • @John-nimis 我认为这里一切正常:ContentView().environmentObject(DBService()).environmentObject(SessionStore())

标签: swift swiftui


【解决方案1】:

您需要将@EnvironmentObject 注入到每个环境中(记住每个.sheet.fullScreenCover 都会创建一个新环境):

Button("Open Child View A"){
    viewModel.isPresented.toggle()
}
.fullScreenCover(isPresented: $viewModel.isPresented) {
    ChildViewA(isPresented: $viewModel.isPresented)
        .environmentObject(EnvObj()) // inject here
}

注意:必须先创建 EnvironmentObject。如果它不是一开始就创建和注入的,你就无法通过@EnvironmentObject var envObj: EnvObj 访问它。

此外,您实际上并不需要直接在 fullScreenCover 闭包中创建依赖项。您可以将它们放在根级别并相应地注入。

【讨论】:

  • 它有效,谢谢!但是为什么如果我们在视图模型中使用@State 变量而不是@Published,即使没有将@EnvironmentObject 注入新环境,也不会出现错误?
  • @Heimdallr 你不能在ObservableObject 中使用@State。这是一个仅查看的包装器。它在其他任何地方都行不通。
  • 我知道,但是我的意思是如果在视图中声明了@State,那么我们将它传递给子视图(fullScreenCover),那么@EnvironmentObject 的错误就不会发生
  • @Heimdallr 不,这是不可能的。如果您在某些视图中声明 @EnvironmentObject,您还必须将其注入环境。我使用 @State var isPresented = false 而不是 $viewModel.isPresented 检查了您的另一个示例 - 它失败并出现完全相同的错误。
  • 太奇怪了,它对我来说没有错误(Xcode 12.0.1)...
【解决方案2】:

我最好的猜测是,当您将 DBService 对象传递给 ContentView 时,您需要对其进行强引用。在声明内容视图的SceneDelegate(或App 对象)中,而不是ContentView(DBService()),执行以下操作:

let dbService = DBService() // <- strong reference, stored property at class/struct level

func scene() {
    ContentView(dbService) // <- this is the stored instance, not a brand new DBService
}

// (or)

var body: some Scene {
    ContentView(dbService) // <- this is the stored instance, not a brand new DBService
}

【讨论】:

  • 不幸的是它不起作用:(我也试过@StateObject dbService = DBService()
猜你喜欢
  • 2021-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-08
  • 1970-01-01
  • 2020-05-11
  • 2020-05-11
  • 2021-12-30
相关资源
最近更新 更多