Question Details

No question body available.

Tags

swift sidebar swiftui-navigationlink detailview navigationsplitview

Answers (1)

Accepted Answer Available
Accepted Answer
June 15, 2025 Score: 0 Rep: 293,970 Quality: High Completeness: 80%

When you activate a NavigationLink in the sidebar, the destination view gets "pushed" onto the detail column. So after you click on a navigation link, the detail view is either displaying detailPage(v: 1) or detailPage(v: 2). It is no longer displaying the original detailPage(v: modelData.value) that you put in detail: { ... }.

Since you passed a constant (1 or 2) to the v parameter, the detailPage will always display that, regardless of what model.value is.

The use of onChange is totally redundant. Before you click on the navigation links, the detail column is detailPage(v: modelData.value), so v is the same as modelData.value anyway. After you click on the navigation link, v is a constant that never changes, so onChange cannot possibly be triggered at all. Keep in mind that the navigation links push a new view to replace the original detail column - these are separate views, each with their own onChange detecting changes of their own vs.

In the first place though, this is not how NavigationLinks are designed to be used in the sidebar column of a NavigationSplitView. The only documented way to use NavigationLinks in the sidebar column is "coordinate with a list". That is,

final class ModelData: ObservableObject {
    @Published var value: Int = 1
}

struct ContentView: View { @StateObject var modelData = ModelData() var body: some View { NavigationSplitView{ List(selection: $modelData.value) { NavigationLink("Detail Page 1", value: 1) NavigationLink("Detail Page 2", value: 2) } }detail:{ Text("Detail Page") Text("\(modelData.value)").font(.largeTitle) } .environmentObject(modelData) } }