Swiftui嵌套导航
当我导航时,下一页打开并快速关闭。为什么 ?
我在一个空项目上尝试了此问题,但没有遇到这个问题。为什么在我的项目中,导航在前进后会回来? 我想创建一个嵌套导航结构。
我的项目代码:
app :
@main
struct FileApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.onAppear {
UserDefaults.standard.set(false, forKey: "_UIConstraintBasedLayoutLogUnsatisfiable")
}
}
}
}
contentView:
struct ContentView: View {
@StateObject var safeFileVM: SafeFileViewModel = SafeFileViewModel()
var body: some View {
NavigationView {
AView(safeFileVM: safeFileVM)
}
}
}
aview:
struct AView: View {
@ObservedObject var safeFileVM: SafeFileViewModel
@State var currentLocation = ""
@State var currentFolder: String = "Safe File"
var body: some View {
List(safeFileVM.arrayOfFiles, id: \.id) { folderItem in
BView(safeFileVM: safeFileVM, currentFile: folderItem, currentLocation: currentLocation)
}
.navigationTitle(Text(currentFolder))
.onAppear {
safeFileVM.additionPath = currentLocation
safeFileVM.takeArrayOfItems()
}
}
}
bview:
struct BView: View {
@ObservedObject var safeFileVM: SafeFileViewModel
@State var currentFile: MyFile
@State var currentLocation: String
var body: some View {
VStack {
if currentFile.typeOfFile == "folder" {
NavigationLink(currentFile.fileName) {
AView(safeFileVM: safeFileVM, currentLocation: currentLocation + "/" + currentFile.fileName, currentFolder: currentFile.fileName)
}
.isDetailLink(false)
} else {
Text(currentFile.fileName)
}
}
}
}
空的项目代码:
contentView:
struct ContentView: View {
var body: some View {
NavigationView {
AView()
.navigationBarTitleDisplayMode(.inline)
}
}
}
aview:
struct AView: View {
var body: some View {
List(0..<5) { item in
BView(title: item)
}
}
}
bview:
struct BView: View {
var title: Int = 0
var body: some View {
NavigationLink("test \(title)") {
AView()
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的
aview
'sonappear
中,您调用takearrayofitems
,我认为,在该中,在该.artayoffiles
属性中属性查看模型。在您的层次结构中,您的顶级
aview
创建了链接到bview
s的项目列表,以及bview> bview
link to link tos的链接。 > Aview
再次。当调用该嵌套
aview
时,onappear
再次调用 - 因此,takearrayofitems
可能正在突变文件夹的根层,这使链接进一步无效。路径;因此,导航视图将自己重置回根。您可能应该注意确保层次结构的任何刷新不会使驱动您UI的数据无效。观看 /a>涉及SwiftUi如何使用对象标识在需要重新绘制接口时使用对象身份进行操作的 /a>。
而且,如果您的应用程序仅能是ios16,则您还可以访问SwiftUI中的新导航UI组件,该组件提供由一系列状态对象驱动的堆栈,从而使数据更改时保持堆栈位置更加容易。下。有关此主题的一个有用视频是 swiftui fackigation 来自WWDC22 。
In your
AView
'sonAppear
, you calltakeArrayOfItems
which, I presume, mutates the.arrayOfFiles
property in the view model.In your hierarchy, your top level
AView
creates a list of items which link toBView
s, and the folder links inBView
link toAView
again.When that nested
AView
is called,onAppear
is called again - sotakeArrayOfItems
is probably mutating the root layer of folders, which invalidates the links further down the path; and so the navigation view resets itself back to the root.You should probably take care to ensure that any refreshing of your hierarchy doesn't invalidate the data driving your UI. It might also be useful to watch Demystifying SwiftUI from WWDC21 which goes into, among other things, how SwiftUI uses object identity to work out when the interface needs to be redrawn.
And if your app can be iOS16 only, you'll also have access to the new navigation UI components in SwiftUI, which provide for stacks to be driven by an array of state objects, making it much easier to maintain stack position when data is changing underneath. A useful video on this topic is The SwiftUI cookbook for navigation from WWDC22.