NavigationLink selection not working as expected

I am probably missing something but I can't make the NavigationLink work inside a List.
I'm using NavigationLink(destination, tag, selection) And i would like to pop to the root view with a tap on a button as you can see in this sample project:
Code Block struct ContentView : View {
@State var selectedView : Int? = nil var colors : [String] = ["blue", "yellow", "green", "red", "black"]
var body : some View {
NavigationView {
List {
ForEach (colors.indices)
{
index in NavigationLink (
destination : ColorDetail (
selectedView : self.$selectedView,
color : colors [index]
),
tag : index,
selection : self.$selectedView,
label : { Text(colors[index]) }
)
}
}
}
}
}
struct ColorDetail : View {
@Binding var selectedView : Int? var color : String var body : some View {
VStack {
Text (color)
Text ("SelectedView: \(selectedView ?? 99)")
Button ("set SelectedView to nil and go back")
{ self.selectedView = nil }
}
}
}


Why if I set selectedView to nil nothing happens? How can i pop to the root view (ContentVIew) from the ColorDetail on a button tap?

Just copy this code and try it, it will build.
Why should the ColorDetail pop the view, when you are just resetting the selectedView state? To pop you have to dismiss the presented View like so:

Code Block swift
struct ColorDetail : View {
//get the presentation mode from the environment:
    @Environment(\.presentationMode) var presentation
    @Binding var selectedView : Int?
    var color : String
    var body : some View {
        VStack {
            Text (color)
            Text ("SelectedView: \(selectedView ?? 99)")
            Button ("set SelectedView to nil and go back")
            {
                self.selectedView = nil
//Pop the detail view:
                self.presentation.wrappedValue.dismiss()
            }
        }
    }
}


Hope it helps

Klaus
NavigationLink selection not working as expected
 
 
Q