Problem
NavigationLink and Button both perform actions.
So having both NavigationLink and Button doesn't make sense, so the action of the button is ignored.
Points to note
NavigationLink is used for navigation
NavigationView is deprecated, so use NavigationStack instead
Solution
Given below is sample code using NavigationStack, use onChange(of:) to detect any changes to path.
Tip
You could even pass the path variable as a binding to the destination views
struct ContentView: View {
@State private var path = [Int]()
var body: some View {
NavigationStack(path: $path) {
VStack {
NavigationLink(value: 1) {
Rectangle()
.frame(width: 100, height: 100)
}
NavigationLink(value: 2) {
Rectangle()
.frame(width: 100, height: 100)
}
NavigationLink(value: 3) {
Rectangle()
.frame(width: 100, height: 100)
}
}
.navigationDestination(for: Int.self) { value in
Text("Selected box: \(value)")
}
}
.onChange(of: path) { newPath in
print("path: \(newPath)")
}
}
}