This is the error that shows: "'init(destination:tag:selection🏷️ )' was deprecated in iOS 16.0: use NavigationLink(value🏷️ ) inside a List within a NavigationStack or NavigationSplitView"
I can't seem to get past these. Can anyone help me with this? Thank you a bunch!
import SwiftUI
struct FileView: View { @Environment(.presentationMode) var presentationMode
let forms = [
"Natural Slope Evaluation Form",
"Artificial Slope Evaluation Form",
"Reinforced Slope Evaluation Form"
]
@State private var isFormSelected = false
@State private var selectedForm: String? = nil
var body: some View {
NavigationView {
VStack {
List(forms, id: \.self) { form in
NavigationLink(destination: formView(for: form), tag: form, selection: binding(for: form)) {
Text(form)
}
}
}
}
}
private func formView(for form: String) -> some View {
if form == "Natural Slope Evaluation Form" {
return AnyView(NaturalSlopeFormView())
} else if form == "Artificial Slope Evaluation Form" {
// Handle other form views
} else if form == "Reinforced Slope Evaluation Form" {
// Handle other form views
}
return AnyView(EmptyView())
}
private func binding(for form: String) -> Binding<String?> {
Binding<String?>(
get: { selectedForm },
set: { newValue in
if newValue != selectedForm {
selectedForm = newValue
isFormSelected = newValue != nil
}
}
)
}
}
extension String: Identifiable { public var id: String { return self } }
struct FileView_Previews: PreviewProvider { static var previews: some View { FileView() } }
Check out the Migrating to new navigation types article to see how SwiftUI's navigation APIs changed.
New in iOS 17 is a modifier that seems to be a replacement for the tag-selection navigation link behaviour: navigationDestination(item:destination:)
.
Something like this could work:
var body: some View {
NavigationStack { // change here
VStack {
List(forms, id: \.self) { form in
// use new navigation link with a value
NavigationLink(value: form) {
Text(form)
}
}
// add new modifier
.navigationDestination(item: formBinding()) { form in
formView(for: form)
}
}
}
}
private var formBinding() -> Binding<String?> { // remove unused parameter
Binding<String?>(
get: { selectedForm },
set: { newValue in
if newValue != selectedForm {
selectedForm = newValue
isFormSelected = newValue != nil
}
}
)
}