当我导航时,下一页会快速打开和关闭。为什么?
我在一个空项目上尝试了这个,没有遇到这个问题。为什么在我的项目中前进后导航会回来?
我想创建一个嵌套导航结构。
我的项目代码:
应用程序:
@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)
}
}
}
}
空项目代码:
内容视图:
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()
}
}
}
AView
无限递归引用BView
,以及BView
引用AView
。但是你的“EMPTY PROJECT CODES:”在我使用ios 15.5上出奇地有效。在macOS 12.5,Xcode 14,目标ios-15.5上也同样有效。 - workingdog support UkraineSafeFileViewModel
和MyFile
。请展示能够重现问题的代码。 - workingdog support Ukraine