1

In the following working example, editing the name of the published Test.Data will work, editing the name of the parent will work, but editing the name of the child will cause the NavigationView to pop.

Is there any way for the NavigationView to not pop when editing a nested?

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")
            }
        }
    }
}
coenttb
  • 21
  • 2
  • Your ContentView needs to be top level - it shouldn’t be inside your ObservedObject class. – Chris May 14 '20 at 21:16
  • Could you elaborate on this? I have no reason other than namespacing for this. – coenttb May 15 '20 at 06:09
  • Your top class definition should be completely closed ( `}` ) and then your ContentView struct should be declared separately. This might just be a typo of a misplaced parenthesis in the above code. From my (limited) SwiftUI experience, I wouldn’t have thought that would run at all. – Chris May 15 '20 at 06:12
  • Also, I’m confused by the nested structs (though it is possible to use them that way). What view and data layout are you trying to achieve? I might be able to come up with something. – Chris May 15 '20 at 06:18
  • @Chris There’s no reason that nesting affects anything. Perhaps try it and see moving it out doesn’t affect the issue. – grg Mar 23 '21 at 12:56

0 Answers0