How do I pass a Binding via the new .navigationDestination(for: , destination: )?
import SwiftUI
enum TestEnum: String, Hashable, CaseIterable {
case first, second, third
}
struct ContentView: View {
@State private var test: TestEnum = .first
var body: some View {
NavigationStack {
VStack {
NavigationLink(value: test, label: {
Text(test.rawValue)
})
}
// This does not work, as it won't allow me to use $caze
.navigationDestination(for: TestEnum.self, destination: { caze in
SecondView(test: $caze)
})
}
}
}
struct SecondView: View {
@Environment(\.presentationMode) var presentationMode
@Binding var test: TestEnum
var body: some View {
ForEach(TestEnum.allCases, id: \.self) { caze in
Button(action: {
test = caze
presentationMode.wrappedValue.dismiss()
}, label: {
Text(caze.rawValue)
})
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
In SwiftUI 3.0 I'd simply use:
NavigationLink(destination: SecondView(test: $test), label: {
Text(test.rawValue)
})
Is this still the correct approach, as we cannot pass a Binding yet?
Not really interested in complex workarounds like using an EnvironmentObject and passing an index, as the SwiftUI 3.0 approach works fine.
However, if there is a proper way of passing a Binding via .navigationDestination(for: , destination: ) I'll happily use it.
NavigationLink(destination: SecondView(test: $test), label: { Text(test.rawValue) })
with theNavigationStack
instead of thenavigationDestination
.NavigationView
is deprecated but notNavigationLink(destination: ..)
– ErihaBinding
isn'tHashable
– Leverett.navigationDestination
with aBinding
, for example in a programmatic navigation with bound properties... Is there a way to makeBinding<Stuff>
Hashable
? – Diffusivity