View pops automatically when in navigation view

I want to have a navigation view with 3 views where the third view need to pop to the root view when button is clicked.Here is the code I'm implementing:

import SwiftUI

class NavigationHelper: ObservableObject {
    @Published var selection:String? = nil
}

struct View1: View {
    
    @ObservedObject var navigationHelper:NavigationHelper = NavigationHelper()
    
    var body: some View {
        NavigationView{
            VStack {
                Text("View1")
                    .padding()
                
                Button {
                    navigationHelper.selection = "View2"
                } label: {
                    Text("Go To View2")
                }

                
                NavigationLink(tag: "View2", selection: $navigationHelper.selection) {
                    View2()
                } label: {
                    EmptyView()
                }.isDetailLink(false)
            }
        }.environmentObject(navigationHelper)

    }
}

struct View2: View {
    @EnvironmentObject var navigationHelper:NavigationHelper
    
    var body: some View {
        VStack {
            Text("View2")
                .padding()
            
            Button {
                navigationHelper.selection = "View3"
            } label: {
                Text("Go To View3")
            }
            
            
            NavigationLink(tag: "View3", selection: $navigationHelper.selection) {
                View3()
            } label: {
                EmptyView()
            }
        }
    }
}


struct View3: View {
    @EnvironmentObject var navigationHelper:NavigationHelper
    
    var body: some View {
        VStack {
            Text("View3")
                .padding()
            
            Button {
                navigationHelper.selection = nil
            } label: {
                Text("Go To Root")
            }
        }
    }
}

When View3 is opened it pops back to View1 when opened and cannot understand why? For some reason the selection property becomes nil maybe.

Could you show the definition of your root view ? What do you expect by setting to nil ?

For some reason the selection property becomes nil maybe.

Your guess seems to be wrong.

The most critical thing in your code is that a single selection: is shared between two NavigationLinks in different levels of navigation.

When you execute this line of code:

navigationHelper.selection = "View3"

The NavigationLink in View1 gets inactive:

                //↓This NavigationLink gets inactive when `navigationHelper.selection` has any other value than `"View2"`
                NavigationLink(tag: "View2", selection: $navigationHelper.selection) {
                    View2()
                } label: {
                    EmptyView()
                }.isDetailLink(false)

So, one possible workaround would be something like this:

import SwiftUI

class NavigationHelper: ObservableObject {
    @Published var view2IsActive: Bool = false
    @Published var view3IsActive: Bool = false
}

struct View1: View {
    
    @ObservedObject var navigationHelper:NavigationHelper = NavigationHelper()
    
    var body: some View {
        NavigationView{
            VStack {
                Text("View1")
                    .padding()
                
                Button {
                    navigationHelper.view2IsActive = true
                } label: {
                    Text("Go To View2")
                }

                
                NavigationLink.init(isActive: $navigationHelper.view2IsActive) {
                    View2()
                } label: {
                    EmptyView()
                }.isDetailLink(false)
            }
        }.environmentObject(navigationHelper)

    }
}

struct View2: View {
    @EnvironmentObject var navigationHelper:NavigationHelper
    
    var body: some View {
        VStack {
            Text("View2")
                .padding()
            
            Button {
                navigationHelper.view3IsActive = true
            } label: {
                Text("Go To View3")
            }
            
            
            NavigationLink(isActive: $navigationHelper.view3IsActive) {
                View3()
            } label: {
                EmptyView()
            }
        }
    }
}


struct View3: View {
    @EnvironmentObject var navigationHelper:NavigationHelper
    
    var body: some View {
        VStack {
            Text("View3")
                .padding()
            
            Button {
                navigationHelper.view2IsActive = false
            } label: {
                Text("Go To Root")
            }
        }
    }
}

By Go to Root view I mean go back to View1

View pops automatically when in navigation view
 
 
Q