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

  • 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.

Accepted Reply

If you take out the VStack it works correctly.

Also, if you only need a text link, you can use NavigationLink("Location", value: 1).

  • The text link was for illustrative purposes. Thanks!

Add a Comment

Replies

If you take out the VStack it works correctly.

Also, if you only need a text link, you can use NavigationLink("Location", value: 1).

  • The text link was for illustrative purposes. Thanks!

Add a Comment