title: SwiftUI: Dismiss View Within macOS NavigationView
body: <p>As detailed <a href="https://stackoverflow.com/a/57279591">here</a> (on an iOS topic), the following code can be used to make a SwiftUI <code>View</code> dismiss itself:</p>
  
  <pre><code>@Environment(\.presentationMode) var presentationMode
  // ...
  presentationMode.wrappedValue.dismiss()
  </code></pre>
  
  <p>However, this approach doesn't work for a native (not Catalyst) macOS <code>NavigationView</code> setup (such as the below), where the selected view is displayed alongside the <code>List</code>.</p>
  
  <p>Ideally, when any of these sub-views use the above, the list would go back to having nothing selected (like when it first launched); however, the dismiss function appears to do nothing: the view remains exactly the same.</p>
  
  <p>Is this a bug, or expected macOS behaviour?</p>
  
  <p>Is there another approach that can be used instead?</p>
  
  <pre><code>struct HelpView: View {
  
      var body: some View {
  
          NavigationView {
              List {
                  NavigationLink(destination:
                      AboutAppView()
                  ) {
                      Text("About this App")
                  }
                  NavigationLink(destination:
                      Text("Here’s a User Guide")
                  ) {
                      Text("User Guide")
                  }
              }
          }
      }
  }
  
  struct AboutAppView: View {
  
      @Environment(\.presentationMode) var presentationMode
  
      public var body: some View {
          Button(action: {
              self.dismissSelf()
          }) {
              Text("Dismiss Me!")
          }
      }
  
      private func dismissSelf() {
          presentationMode.wrappedValue.dismiss()
      }
  }
  </code></pre>
  
  <p>FYI: The real intent is for less direct scenarios (such as triggering from an <code>Alert</code> upon completion of a task); the button setup here is just for simplicity.</p>