1

在下面的工作示例中,编辑已发布的 Test.Data 的名称将起作用,编辑父级的名称将起作用,但编辑子级的名称将导致 NavigationView 弹出。

有什么方法可以让 NavigationView 在编辑嵌套时不弹出?

class Test:ObservableObject {
@Published var data:Data = .init()

    struct Data:Identifiable {
        var id:UUID = UUID()
        var name:String = ""
        var parents:[Parent] = [.init()]
    }

    struct ContentView:SwiftUI.View {
        @EnvironmentObject var test:Test

        var body: some SwiftUI.View {
            NavigationView {
                Form {
                    $test.data.name.view(with: "Name")
                    ForEach(enumerating: test.data.parents) { index, item in
                        NavigationLink(destination: Form { Parent.View(parent: self.$test.data.parents[index]) }) {
                            Text(item.name)
                        }
                    }
                }
            }
        }
    }
}

struct Parent:Identifiable {
    var id:UUID = UUID()
    var name:String = ""
    var children:[Child] = [.init()]

    struct View: SwiftUI.View {
        @Binding var parent:Parent
        var body: some SwiftUI.View {
            Group {
                $parent.name.view(with: "Name of the parent")
                ForEach(enumerating: parent.children) { index, item in
                    NavigationLink(destination: Form { Child.View(child: self.$parent.children[index]) }) {
                        Text(item.name)
                    }
                }
            }
        }
    }
}


struct Child:Identifiable {
    var id:UUID = UUID()
    var name:String = ""

    struct View: SwiftUI.View {
        @Binding var child:Child
        var body: some SwiftUI.View {
            Group {
                $child.name.view(with: "Name of the child")
            }
        }
    }
}
4

0 回答 0