Now, it’s pretty cool we can use bindings like this, but also we probably wouldn’t want follow this pattern literally in a real world code base. We now need to pass this binding through every layer of our application so that child features can perform navigation.
When confronted with this kind of problem in SwiftUI there are two common approaches. One is to put the path binding in the environment so that every view has immediate access to it, and the other is to provide tools to the view that allow it to push values to the path without actually having access to the path.
We are going to discuss both of these approaches, but we will start with the first one, where we put the path
binding in the environment.
Of course, you are probably thinking how does one use the SwiftUI environment in UIKit?
Well, you don’t really. Instead, UIKit has its own version of the environment, but its called “traits”, and traits even bridge to SwiftUI’s environment if you want. Traits make it possible to set values inside a view or controller hierarchy, and each layer of the hierarchy will get access to the values and even be able to override the values.
Let’s take a look.