Post

Replies

Boosts

Views

Activity

How to retrieve values set in Hashable paths when using a router to navigate through views
I am using an enum as Hashable and a Router as ObservableObject to navigate through views. I send an id as parameter for the Form path and I would like to retrieve this is id in the Form view enum Paths: Hashable{ // An id is sent as parameter to be retrieved in the view case Form(id: String) } final class Router<T: Hashable>: ObservableObject { @Published var paths: [T] = [] func push(_ path: T){ paths.append(path) } } When I go from any view to the Form view, I would like to retrieve the value of the id sent in the Form path. For example, I may have in any view a button that make the user go to the Form view Button { router.push(.Form(id:"foo")) } Then, in the Form view I want to retrieve the id set when clicking on the button struct FormView: View{ @EnvironmentObject var router: Router<Paths> var body: some View{ VStack{ Text("\(id)") <--- How can I retrieve the id sent in the Form path? } } } How can I do to retrieve the value of "id"? I know I could set additional @Published fields in the router and retrieve these fields in the Form view but I think that would bloat the router and it seems more elegant to me to just set the views parameters in the paths that lead to each one of the views. Many thanks for your insights on this
2
0
484
Sep ’23