'Why does my NavigationView bounce back to its initial view?

I'm trying to create a programmatically-driven view navigation with SwiftUI 2 with an environmentObject that is used to store the selection of the current view in a NavigationView.

I'm aware the below code navigation could simply work with only navigation links directly but my actual code is more complex and views will have to be navigated based on receiving a network response among possibly other mechanisms and I want to create a clear and unified navigation logic for this so I'm trying to use the currentView property in the environment object.

ContentView:

import SwiftUI

enum NavigationViewTarget: String {
    case ContentView
    case RegistrationView
    case LoginView
    case LandingView
}

final class GlobalViewModel: ObservableObject {
    @Published var currentView: NavigationViewTarget? = .ContentView
}

struct ContentView: View {
    @StateObject var viewModel = GlobalViewModel()
    
    var body: some View {
        GeometryReader {
            screen in
            
            NavigationView {
                ScrollView(showsIndicators: false) {
                    VStack(alignment: .leading) {
                        HStack {
                            NavigationLink(destination: RegistrationView(), tag: .RegistrationView, selection: $viewModel.currentView) { EmptyView() }.hidden()
                            NavigationLink(destination: LoginView(), tag: .LoginView, selection: $viewModel.currentView) { EmptyView() }.hidden()
                            Button(action: { viewModel.currentView = .RegistrationView }) {
                                Text("Register")
                            }
                            Button(action: { viewModel.currentView = .LoginView }) {
                                Text("Login")
                            }
                        }
                    }
                }
                .navigationViewStyle(StackNavigationViewStyle())
            }
        }
        .environmentObject(viewModel)
    }
}

RegistrationView:

import SwiftUI

struct RegistrationView: View {
    @EnvironmentObject var viewModel: GlobalViewModel
    
    var body: some View {
        GeometryReader {
            screen in
            
            ScrollView(showsIndicators: false) {
                VStack(alignment: .leading) {
                    HStack {
                        NavigationLink(destination: LandingView(), tag: .LandingView, selection: $viewModel.currentView) { EmptyView() }.hidden()

                        Button(action: { viewModel.currentView = .LandingView }) {
                            Text("Submit")
                        }
                    }
                }
            }
        }
    }
}

LandingView:

struct LandingView: View {
    @EnvironmentObject var viewModel: GlobalViewModel
    
    var body: some View {
        Text("Landing View")
    }
}

LoginView:

import SwiftUI

struct LoginView: View {
    @EnvironmentObject var viewModel: GlobalViewModel
    
    var body: some View {
        Text("Login View")
    }
}

The app navigates to RegistrationView and when tapping the Submit button in it it navigates to LandingView but then automatically bounces back to ContentView. Any ideas why this happens?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source