Skip to content

Instantly share code, notes, and snippets.

@productinfo
Forked from BigZaphod/NavigationPathBug.swift
Created November 24, 2022 08:51
Show Gist options
  • Save productinfo/26795bac4f49778b39e4cd8038d6da61 to your computer and use it in GitHub Desktop.
Save productinfo/26795bac4f49778b39e4cd8038d6da61 to your computer and use it in GitHub Desktop.
struct ContentView: View {
@State var data: [String] = []
@State var navPath = NavigationPath()
var body: some View {
NavigationStack(path: $navPath) {
ZStack {
Button("Hit It") {
navPath.append(0)
}
}
.navigationDestination(for: Int.self) { index in
// This doesn't work - it thinks the array is empty.
childViewFromViewBuilder(for: index)
// This one DOES work - it finds the data in the array just fine.
//childViewFromRegularFunction(for: index)
}
}
.task {
data = ["Item 1", "Item 2", "Item 3"]
}
}
@ViewBuilder func childViewFromViewBuilder(for index: Int) -> some View {
if data.indices.contains(index) {
Text(data[index])
} else {
Text("Not found.")
}
}
func childViewFromRegularFunction(for index: Int) -> some View {
if data.indices.contains(index) {
return Text(data[index])
} else {
return Text("Not found.")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment