gpt4 book ai didi

swift - 未找到 类型的 ObservableObject。 View.environmentObject(_ :) for may be missing as an ancestor of this view

转载 作者:行者123 更新时间:2023-12-05 03:44:47 26 4
gpt4 key购买 nike

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

一切正常,直到我决定为我的 View 创建一个 ViewModel。即,如果我将 @State 变量从 AddMatchView 转移到 TeamPickerView,那么错误就不会发生,但我使用来自 @ObservedObject 的属性

主父 View :

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
}
}
}

最佳答案

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

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

注意:必须先创建 EnvironmentObject。如果它不是首先创建和注入(inject)的,则不能通过 @EnvironmentObject var envObj: EnvObj 访问它。

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

关于swift - 未找到 <TypeName> 类型的 ObservableObject。 View.environmentObject(_ :) for <TypeName> may be missing as an ancestor of this view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66281331/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com