|
import SwiftUI |
|
|
|
struct RootStringNavStack: View { |
|
@State |
|
var navigationPath: [String] = [] |
|
|
|
// For hook-printing. |
|
var navigationPathBinding: Binding<[String]> { |
|
.init( |
|
get: { navigationPath }, |
|
set: { |
|
print("===> navigationPath", $0) |
|
navigationPath = $0 |
|
} |
|
) |
|
} |
|
|
|
var body: some View { |
|
NavigationStack(path: navigationPathBinding) { |
|
StringNavStackChildView( |
|
title: "String NavStack", |
|
navigationPath: $navigationPath |
|
) |
|
// Top-level navigationDestination only. |
|
.navigationDestination(for: String.self) { route in |
|
// TODO: Why `navigationPath.count = 1` is printed after `N >= 2` push transition? |
|
let _ = print("===> navigationDestination, route = \(route), navigationPath.count = \(navigationPath.count)") |
|
StringNavStackChildView(title: route, navigationPath: $navigationPath) |
|
} |
|
} |
|
} |
|
} |
|
|
|
struct StringNavStackChildView: View { |
|
let title: String |
|
|
|
@Binding |
|
var navigationPath: [String] |
|
|
|
init(title: String, navigationPath: Binding<[String]>) { |
|
self.title = title |
|
self._navigationPath = navigationPath |
|
} |
|
|
|
var body: some View { |
|
List { |
|
NavigationLink(value: "1") { |
|
Text("Link 1") |
|
} |
|
NavigationLink(value: "2") { |
|
Text("Link 2") |
|
} |
|
NavigationLink(value: "3") { |
|
Text("Link 3") |
|
} |
|
} |
|
.navigationTitle(title) |
|
.toolbar { |
|
Button(action: { navigationPath = [] }) { |
|
Text("Go Home") |
|
} |
|
} |
|
} |
|
} |
|
|
|
/* |
|
// 1. Tap Link 1 |
|
===> navigationPath ["1"] |
|
===> navigationDestination, route = 1, navigationPath.count = 1 |
|
===> navigationDestination, route = 1, navigationPath.count = 1 |
|
|
|
// 2. Tap Link 2 |
|
===> navigationPath ["1", "2"] |
|
===> navigationDestination, route = 2, navigationPath.count = 2 |
|
===> navigationDestination, route = 1, navigationPath.count = 1 |
|
===> navigationDestination, route = 2, navigationPath.count = 1 |
|
|
|
// 3. Tap Link 3 |
|
===> navigationPath ["1", "2", "3"] |
|
===> navigationDestination, route = 3, navigationPath.count = 3 |
|
===> navigationDestination, route = 1, navigationPath.count = 1 |
|
===> navigationDestination, route = 2, navigationPath.count = 1 |
|
===> navigationDestination, route = 3, navigationPath.count = 1 |
|
|
|
// 4. Back button |
|
===> navigationPath ["1", "2"] |
|
===> navigationDestination, route = 1, navigationPath.count = 2 |
|
===> navigationDestination, route = 2, navigationPath.count = 2 |
|
===> navigationDestination, route = 1, navigationPath.count = 1 |
|
===> navigationDestination, route = 2, navigationPath.count = 1 |
|
|
|
// 5. Back button |
|
===> navigationPath ["1"] |
|
===> navigationDestination, route = 1, navigationPath.count = 1 |
|
===> navigationDestination, route = 1, navigationPath.count = 1 |
|
*/ |