in a new SwiftUI mac app project, paste below code
import SwiftUI
struct ContentView: View {
@State private var path = [Int]()
var body: some View {
NavigationSplitView {
NavigationLink {
NavigationStack(path: $path) {
VStack {
Button("Show 1") {
path = [1]
}
}
.navigationDestination(for: Int.self) { selection in
Text("You selected (selection)")
}
}
} label: {
Text("sidebar object")
}
} detail: {
Text("detail")
}
}
}
run the project, then click the “sidebar object” button in sidebar, then click the “Show 1”, would somehow show a yellow warning mark, screenshot here
However, after asking some AI and twisting its code, I make it works just by surround Text(“detail”) with another NavigationStack, see below code:
import SwiftUI
struct ContentView: View {
@State private var path = [Int]()
var body: some View {
NavigationSplitView {
NavigationLink {
NavigationStack(path: $path) {
VStack {
Button("Show 1") {
path = [1]
}
}
.navigationDestination(for: Int.self) { selection in
Text("You selected (selection)")
}
}
} label: {
Text("sidebar object")
}
} detail: {
NavigationStack {
Text("detail")
}
}
}
}
Can someone tell me what happen here? Is this the right way of doing things?
p.s. I used to use UIKit, and just trying to learn SwiftUI not long ago
Jay Tang is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.