Skip to content

Instantly share code, notes, and snippets.

@BigZaphod
Created September 15, 2022 20:54
Show Gist options
  • Save BigZaphod/f0b87792f732e4e6e7ee3ecbab4b8ad6 to your computer and use it in GitHub Desktop.
Save BigZaphod/f0b87792f732e4e6e7ee3ecbab4b8ad6 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