【发布时间】: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())