title: EnvironmentObject refresh/hand over problems using NavigationLink
body: <p>I am currently developing an app for watchOS 6 (independent app) using Swift/SwiftUI in XCode 11.5 on macOS Catalina.</p>
  <p>Before a user can use my app, a configuration process is required. As the configuration process consists of several different views which are shown one after each other, I implemented this by using navigation links.</p>
  <p>After the configuration process has been finished, the user should click on a button to return to the &quot;main&quot; app (main view). For controlling views which are on the same hierarchical level, my plan was to use an EnvironmentObject (as far as I understood, an EnvironmentObject once injected is handed over to the subviews and subviews can use the EnvironmentObject) in combination with a &quot;controlling view&quot; which controls the display of the views. Therefore, I followed the tutorial: <a href="https://blckbirds.com/post/how-to-navigate-between-views-in-swiftui-by-using-an-environmentobject/" rel="nofollow noreferrer">https://blckbirds.com/post/how-to-navigate-between-views-in-swiftui-by-using-an-environmentobject/</a></p>
  <p>This is my code:</p>
  <p><strong>ContentView.swift</strong></p>
  <pre><code>struct ContentView: View {
      var body: some View {
          ContentViewManager().environmentObject(AppStateControl())        
      }
  }
  
  struct ContentViewManager: View {
      @EnvironmentObject var appStateControl: AppStateControl
      
      var body: some View {
          VStack {
              if(appStateControl.callView == &quot;AppConfig&quot;) {
                  AppConfig()
              }
              if(appStateControl.callView == &quot;AppMain&quot;) {
                  AppMain()
              }
          }
      }
  }
  </code></pre>
  <p><strong>AppStateControl.swift</strong></p>
  <pre><code>class AppStateControl: ObservableObject {
      @Published var callView: String = &quot;AppConfig&quot;
  }
  </code></pre>
  <p><strong>AppConfig.swift</strong></p>
  <pre><code>struct AppConfig: View {
      @EnvironmentObject var appStateControl: AppStateControl
      var body: some View {
          VStack {
              Text(&quot;App Config Main&quot;)
              NavigationLink(destination: DetailView1().environmentObject(appStateControl)) {
                  Text(&quot;Show Detail View 1&quot;)
              }
          }
      }
  }
  
  struct DetailView1: View {
      @EnvironmentObject var appStateControl: AppStateControl
      var body: some View {
          VStack {
              Text(&quot;App Config Detail View 1&quot;)
              NavigationLink(destination: DetailView2().environmentObject(appStateControl)) {
                  Text(&quot;Show Detail View 2&quot;)
              }
          }
      }
  }
  
  struct DetailView2: View {
      @EnvironmentObject var appStateControl: AppStateControl
      var body: some View {
          VStack {
              Text(&quot;App Config Detail View 2&quot;)
              Button(action: {
                  self.appStateControl.callView = &quot;AppMain&quot;
              }) {
               Text(&quot;Go to main App&quot;)
              }
          }
      }
  }
  </code></pre>
  <p><strong>AppMain.swift</strong></p>
  <pre><code>struct AppMain: View {
      var body: some View {
          Text(&quot;Main App&quot;)
      }
  }
  </code></pre>
  <p>In a previous version of my code (without the handing over of the EnvironmentObject all the time) I got a runtime error (<em>&quot;Thread 1: Fatal error: No ObservableObject of type AppStateControl found. A View.environmentObject(_:) for AppStateControl may be missing as an ancestor of this view.&quot;</em>) caused by line 41 in AppConfig.swift. In the internet, I read that this is probably a bug of NavigationLink (see: <a href="https://www.hackingwithswift.com/forums/swiftui/environment-object-not-being-inherited-by-child-sometimes-and-app-crashes/269" rel="nofollow noreferrer">https://www.hackingwithswift.com/forums/swiftui/environment-object-not-being-inherited-by-child-sometimes-and-app-crashes/269</a>, <a href="https://twitter.com/twostraws/status/1146315336578469888" rel="nofollow noreferrer">https://twitter.com/twostraws/status/1146315336578469888</a>). Thus, the recommendation was to explicitly pass the EnvironmentObject to the destination of the NavigationLink (above implementation). Unfortunately, this also does not work and instead a click on the button &quot;Go to main App&quot; in <em>&quot;DetailView2&quot;</em> leads to the view <em>&quot;DetailView1&quot;</em> instead of <em>&quot;AppMain&quot;</em>.</p>
  <p>Any ideas how to solve this problem? To me, it seems that a change of the EnvironmentObject in a view called via a navigation link does not refresh the views (correctly).</p>
  <p>Thanks in advance.</p>