你好,我正在使用新的导航堆栈,我真的不明白它在类似我的情况下是如何工作的。我有第一个视图(DashboardView(,从那里我有两个导航目标(isPresented(,从第二个视图开始,我必须转到一个带有列表的视图(AllVocabulariesView(,每行之后都必须转到另一个视图(VocabularyView(,这是我可以通过第一个导航目标从DashboardView(isPresentation(访问的保存。
在仪表板下方查看(我删除了不必要的代码(
NavigationStack {
VStack {
LazyVGrid(columns: columns, alignment: .center, spacing: spacing, pinnedViews: [])
{
ForEach(vmHome.vocabularies.prefix(5)) { vocabulary in
VocabularyBannerView(name: vocabulary.name ?? "", language: vmHome.getLanguageName(code: vocabulary.language ?? ""), isFavourite: vocabulary.isFavourite)
.onTapGesture {
selectedVocabulary = vocabulary
showVocabularyView.toggle()
}
.contextMenu {
Button {
vmHome.setVocabularyAsFavourite(entity: vocabulary)
} label: {
Label("Favourite", systemImage: "star")
}
Button(role: .destructive) {
vmHome.deleteVocabulary(vocabulary: vocabulary)
} label: {
Label("Delete", systemImage: "trash")
}
} preview: {
VocabularyBannerView(name: vocabulary.name ?? "", language: vmHome.getLanguageName(code: vocabulary.language ?? ""), isFavourite: vocabulary.isFavourite)
}
}
}
Button {
showAllVocabularyView.toggle()
} label: {
Text("Show all")
.font(.caption)
}
}
.navigationTitle("Dashboard")
.toolbar(.hidden, for: .navigationBar)
.navigationDestination(isPresented: $showVocabularyView) {
if selectedVocabulary != nil {
VocabularyView(vmHome: vmHome, vocabulary: selectedVocabulary!)
}
}
.navigationDestination(isPresented: $showAllVocabularyView) {
AllVocabulariesView(vmHome: vmHome)
}
}
所以第一个去了VocabularyView,第二个去了AllVocabulariesView也没关系,但现在问题出在我像一样构建的AllVocabulaiesView内部
NavigationStack{
List {
ForEach(vmHome.vocabularies) { vocabulary in
NavigationLink(value: vocabulary) {
Text(vocabulary.name ?? "")
}
}
}
.navigationDestination(for: VocabularyEntity.self, destination: { vocabulary in
VocabularyView(vmHome: vmHome, vocabulary: vocabulary)
})
.navigationTitle("Your vocabularies")
.navigationBarTitleDisplayMode(.inline)
}
两个认为,如果我删除navigationstack,那一行会被高亮显示,但什么都没有发生,使用navigationstock,它会转到视图,但没有动画,后退按钮会回到DashboardView,而不是AllVocabulariesView
在几个应用程序中进行测试后,我得出的结论是不应该嵌套NavigationStack。
事实上,您可以在顶层使用.navigationDestination,它将继续为所有嵌套的NavigationLink工作。
虽然我不能在没有完整代码的情况下进行测试,但我建议您进行以下修改:
- 从AllVocabulariesView中删除NavigationStack
- 将您的.navigationDestination分组到您放置其他导航目的地的最高级别
仪表板视图:
NavigationStack {
VStack {
LazyVGrid(columns: columns, alignment: .center, spacing: spacing, pinnedViews: [])
{
ForEach(vmHome.vocabularies.prefix(5)) { vocabulary in
VocabularyBannerView(name: vocabulary.name ?? "", language: vmHome.getLanguageName(code: vocabulary.language ?? ""), isFavourite: vocabulary.isFavourite)
.onTapGesture {
selectedVocabulary = vocabulary
showVocabularyView.toggle()
}
.contextMenu {
Button {
vmHome.setVocabularyAsFavourite(entity: vocabulary)
} label: {
Label("Favourite", systemImage: "star")
}
Button(role: .destructive) {
vmHome.deleteVocabulary(vocabulary: vocabulary)
} label: {
Label("Delete", systemImage: "trash")
}
} preview: {
VocabularyBannerView(name: vocabulary.name ?? "", language: vmHome.getLanguageName(code: vocabulary.language ?? ""), isFavourite: vocabulary.isFavourite)
}
}
}
Button {
showAllVocabularyView.toggle()
} label: {
Text("Show all")
.font(.caption)
}
}
.navigationTitle("Dashboard")
.toolbar(.hidden, for: .navigationBar)
.navigationDestination(isPresented: $showVocabularyView) {
if selectedVocabulary != nil {
VocabularyView(vmHome: vmHome, vocabulary: selectedVocabulary!)
}
}
.navigationDestination(isPresented: $showAllVocabularyView) {
AllVocabulariesView(vmHome: vmHome)
}
.navigationDestination(for: VocabularyEntity.self, destination: { vocabulary in
VocabularyView(vmHome: vmHome, vocabulary: vocabulary)
})
}
所有Vocabularies视图:
List {
ForEach(vmHome.vocabularies) { vocabulary in
NavigationLink(value: vocabulary) {
Text(vocabulary.name ?? "")
}
}
}
.navigationTitle("Your vocabularies")
.navigationBarTitleDisplayMode(.inline)