This was a headscratcher so I'm pasting the code sample below where I got it to work.
Suppose I have a view that will sit somewhere in my navigation stack, called SourceView
. From the perspective of the wider application, this view has some buttons or actions that should trigger navigation to one or more other views, which we'll call TargetView1
and TargetView2
.
The basic SwiftUI way of achieving this is to make SourceView
aware of this navigation requirement, and implementing the navigation inside SourceView
, by embedding a NavigationLink
in its view hierarchy. A NavigationLink
is a visible UI component akin to a button that a user can tap.