Xcode 14 NavigationPath – two views pushed onto nav stack when multiple links in form section

165 views Asked by At
  • Create new SwiftUI project
  • Replace ContentView with the code below
  • Tap either link.
  • Both views are pushed onto the nav stack
class ContentViewModel: ObservableObject {
    @Published var navigationPath = NavigationPath()
}

struct ContentView: View {
    @StateObject var viewModel = ContentViewModel()

    var body: some View {
        NavigationStack(path: $viewModel.navigationPath) {
            Form {
                Section {
                    VStack {
                        NavigationLink(value: 1) {
                            Text("Location")
                        }

                        NavigationLink(value: 2) {
                            Text("Category")
                        }
                    }
                }
            }.navigationDestination(for: Int.self) { route in
                switch route {
                case 1: Text("Location")
                case 2: Text("Category")
                default: Text("Unknown")
                }
            }.navigationTitle("Home")
        }
    }
}

What am I doing wrong? I would only expect the tapped item to be pushed into the stack.

1

There are 1 answers

0
Aaron Bratcher On BEST ANSWER

Removing the VStack solves the problem