In this version we replace default Back Button and add Forward Button to navigate between Views.
We are using selection property to specify which View should be presented.
Unfortunately there are some problems during navigation where Forward Button is used.
ContentView.swift
//==========================================================================
// ContentView
//==========================================================================
struct ContentView: View {
@State var selection : String? = nil
var body: some View {
NavigationView {
VStack {
NavigationLink("Link to John",
destination: SecondScreen(detials: "John Details", selection: $selection),
tag: "John",
selection: $selection
)
NavigationLink("Link to Bill",
destination: SecondScreen(detials: "Bill Details", selection: $selection),
tag: "Bill",
selection: $selection
)
}
}
}
}
//==========================================================================
// SecondScreen
//==========================================================================
struct SecondScreen: View {
var detials : String
@Binding var selection : String? //@Binding allows us to control selection from this View
var body: some View {
Text(detials)
.navigationBarTitle("Second Screen")
.navigationBarBackButtonHidden(true)
.navigationBarItems(
leading: Button("MyBack") { self.selection = nil },
trailing: Button("MyForward") { self.selection = "Bill" }
)
}
}
Initial Screen John Screen