如何在兄弟视图之间传递环境对象?
How do I pass environment object between sibling views?
我正在尝试在同级视图之间传递一些数据。
我得到了一个带有按钮的 VerseDetailView
:
@StateObject var favoritesViewModel = FavoritesViewModel()
...
Button("Add to favorite") {
favoritesViewModel.add(verse: verse)
}
我的 FavoritesViewModel
看起来像这样:
class FavoritesViewModel: ObservableObject {
@Published var favoriteVerses: [Verse] = []
func add(verse: Verse) {
favoriteVerses.append(verse)
}
}
如何通过 favoriteVerses
显示在完全不同的子视图中?
我的主应用程序文件如下所示:
var body: some Scene {
WindowGroup {
TabView {
NavigationView {
BookView() // VerseDetailView is a child of this view
}
.tabItem {
Image(systemName: "book")
Text("Books")
}
NavigationView {
FavoritesView() // I want to get the array of favoriteVerses here
}
.tabItem {
Image(systemName: "bookmark")
Text("Favorites")
}
}
}
}
我的 FavoritesView
看起来像这样:
struct FavoritesView: View {
@EnvironmentObject var favoritesViewModel: FavoritesViewModel
// is this correct?
// I get a hread 1: "Fatal error: No ObservableObject of type FavoritesViewModel found" error
var body: some View {
List {
Section(header: Text("Favorite verses")) {
ForEach(favoritesViewModel.favoriteVerses) { verse in
Text(verse.verse)
}
}
}
}
}
我试着在主应用程序文件的顶部添加这个:
var favoritesViewModel = FavoritesViewModel()
...
NavigationView {
FavoritesView().environmentObject(favoritesViewModel)
}
但这也行不通
将其添加到普通顶视图(在您的情况下是TabView
),喜欢
TabView {
NavigationView {
BookView() // VerseDetailView is a child of this view
}
.tabItem {
Image(systemName: "book")
Text("Books")
}
NavigationView {
FavoritesView() // I want to get the array of favoriteVerses here
}
.tabItem {
Image(systemName: "bookmark")
Text("Favorites")
}
}
.environmentObject(favoritesViewModel) // << here !!
您可以像这样将 environmentObject 传递给每个视图:
TabView {
NavigationView {
BookView() // VerseDetailView is a child of this view
}
.tabItem {
Image(systemName: "book")
Text("Books")
}
.environmentObject(favoritesViewModel)
NavigationView {
FavoritesView() // I want to get the array of favoriteVerses here
}
.tabItem {
Image(systemName: "bookmark")
Text("Favorites")
}
.environmentObject(favoritesViewModel)
}
我正在尝试在同级视图之间传递一些数据。
我得到了一个带有按钮的 VerseDetailView
:
@StateObject var favoritesViewModel = FavoritesViewModel()
...
Button("Add to favorite") {
favoritesViewModel.add(verse: verse)
}
我的 FavoritesViewModel
看起来像这样:
class FavoritesViewModel: ObservableObject {
@Published var favoriteVerses: [Verse] = []
func add(verse: Verse) {
favoriteVerses.append(verse)
}
}
如何通过 favoriteVerses
显示在完全不同的子视图中?
我的主应用程序文件如下所示:
var body: some Scene {
WindowGroup {
TabView {
NavigationView {
BookView() // VerseDetailView is a child of this view
}
.tabItem {
Image(systemName: "book")
Text("Books")
}
NavigationView {
FavoritesView() // I want to get the array of favoriteVerses here
}
.tabItem {
Image(systemName: "bookmark")
Text("Favorites")
}
}
}
}
我的 FavoritesView
看起来像这样:
struct FavoritesView: View {
@EnvironmentObject var favoritesViewModel: FavoritesViewModel
// is this correct?
// I get a hread 1: "Fatal error: No ObservableObject of type FavoritesViewModel found" error
var body: some View {
List {
Section(header: Text("Favorite verses")) {
ForEach(favoritesViewModel.favoriteVerses) { verse in
Text(verse.verse)
}
}
}
}
}
我试着在主应用程序文件的顶部添加这个:
var favoritesViewModel = FavoritesViewModel()
...
NavigationView {
FavoritesView().environmentObject(favoritesViewModel)
}
但这也行不通
将其添加到普通顶视图(在您的情况下是TabView
),喜欢
TabView {
NavigationView {
BookView() // VerseDetailView is a child of this view
}
.tabItem {
Image(systemName: "book")
Text("Books")
}
NavigationView {
FavoritesView() // I want to get the array of favoriteVerses here
}
.tabItem {
Image(systemName: "bookmark")
Text("Favorites")
}
}
.environmentObject(favoritesViewModel) // << here !!
您可以像这样将 environmentObject 传递给每个视图:
TabView {
NavigationView {
BookView() // VerseDetailView is a child of this view
}
.tabItem {
Image(systemName: "book")
Text("Books")
}
.environmentObject(favoritesViewModel)
NavigationView {
FavoritesView() // I want to get the array of favoriteVerses here
}
.tabItem {
Image(systemName: "bookmark")
Text("Favorites")
}
.environmentObject(favoritesViewModel)
}