使用@environmentObject 和@StateObject 时遇到问题

Trouble using @environmentObject and @StateObject

我正在创建我的第一个应用程序,但在使用@EnvironmentObject 和@StateObject 时遇到问题。当我 运行 模拟器时,它会打开我的 ContentView()。我试图将它带到打开 MainView() 的位置,它有一个 tabView。现在 tabView 没有显示我的 ContentView()。我假设我在错误的地方有@EnvironmentObject 和@StateObject。在我的 ContentView 中,它显示了一个列表和 addButton。我基本上是在尝试让用户通过填写​​表格来更新程序。

这是主要方法

import SwiftUI

@main
struct Location_ScoutApp: App {
    
   @StateObject var listViewModels: ListViewModel = ListViewModel()
    
    var body: some Scene {
        WindowGroup {
          //  MainView(){
               ContentView()
            .environmentObject(listViewModels)
            //}
        }
    }
}

这是我的主视图。

import SwiftUI

struct MainView: View {
    
    var body: some View {
        
        TabView {
            MapView()
                .tabItem {
                    Label("Map", systemImage: "map.circle")
                }
// this is where i am having trouble.
            ContentView()
                .tabItem {
                    Label("Explore", systemImage: "magnifyingglass")
                
            }
            
            ProfileView()
                .tabItem {
                    Label("Profile", systemImage: "person.crop.circle")
                }
        }
    }

}
struct MainView_Previews: PreviewProvider {
    static var previews: some View {
        MainView()

    }
}

这是我的内容视图:

import SwiftUI

struct ContentView: View {
    @EnvironmentObject var listViewModel: ListViewModel
    
    var body: some View {
        List {
            ForEach(listViewModel.items) { item in
// this is where i am getting my new error.
                ListRowView(item: item)
                    .onTapGesture {
                        listViewModel.updateItem(item: item)
                    }
                
            }
        }
        
        .navigationTitle("Explore")
        .navigationBarItems(
            leading: EditButton(),
            trailing:
                NavigationLink("Add", destination: addALandmarkForm()))
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        NavigationView {
        ContentView()
    }
        .environmentObject(ListViewModel())
    }
}

没有可重现的示例,很难调试。但是,此代码基于您的代码,运行良好。或许您可以发现您的实现与我的实现之间的差异:


struct Item : Identifiable {
    var id = UUID()
    var title : String
}

class ListViewModel: ObservableObject {
    @Published var items : [Item] = [.init(title: "Test 1"),.init(title: "Test 2")]
}

@main
struct Location_ScoutApp: App {
    
   @StateObject var listViewModels: ListViewModel = ListViewModel()
    
    var body: some Scene {
        WindowGroup {
          MainView()
            .environmentObject(listViewModels)
        }
    }
}

struct MainView: View {
    
    var body: some View {
        
        TabView {
            Text("Map")
                .tabItem {
                    Label("Map", systemImage: "map.circle")
                }

            ContentView()
                .tabItem {
                    Label("Explore", systemImage: "magnifyingglass")
                
            }
            
            Text("profile")
                .tabItem {
                    Label("Profile", systemImage: "person.crop.circle")
                }
        }
    }

}

struct ContentView: View {
    @EnvironmentObject var listViewModel: ListViewModel
    
    var body: some View {
        NavigationView {
           List {
             ForEach(listViewModel.items) { item in
                 Text(item.title)
             }
           }
           .navigationTitle("Explore")
        }
    }
}