title: SwiftUI dynamically pop NavigationLink with a list of links
body: <p>I want to pop a NavigationLink from within code. I followed this article and it works for one link (<a href="https://swiftui-lab.com/bug-navigationlink-isactive/" rel="nofollow noreferrer">https://swiftui-lab.com/bug-navigationlink-isactive/</a>). However, when using a list of links, one has to use a separate boolean for each NavigationLink.</p>
  
  <p>So I thought the smart way to do this is with an EnvironmentObject that holds a dictionary with a boolean for each ChildView:</p>
  
  <pre class="lang-swift prettyprint-override"><code>class Navigation: ObservableObject{
      @Published var show:[UUID:Bool] = [:]
  }
  </code></pre>
  
  <p>Let's say we want to have a variable number child views (of type MyObject).</p>
  
  <p>What needs to be changed in order to make it work?</p>
  
  <pre><code>struct MyObject:Identifiable{
      var id = UUID()
      var name:String
  }
  
  struct ContentView: View {
      @EnvironmentObject var navigation:Navigation
  
      var objects = [MyObject(name: "view1"), MyObject(name: "view2"), MyObject(name: "view3")]
  
      init() {
          for object in objects{
              self.navigation.show[object.id] = false
          }
      }
  
      var body: some View {
          NavigationView{
              VStack{
                  ForEach(objects, id:\.self.id){ object in
                      NavigationLink(destination: Child(object:object), isActive: self.$navigation.show[object.id], label: {
                          Text(object.name)
                      })
                  }
              }
          }
      }
  }
  
  struct Child: View {
      @EnvironmentObject var navi:Navigation
      var object:MyObject
  
      var body: some View {
          Button(action:{self.navi.show[self.object.id] = false}, label: {
              Text("back")
          })
      }
  }
  
  </code></pre>