22.5 Environment Objects
Observed objects are best used when a particular state needs to be used by a few SwiftUI views within an app. When one view navigates to another view which needs access to the same observed or state object, the originating view will need to pass a reference to the observed object to the destination view during the navigation (navigation will be covered in the chapter entitled “SwiftUI Lists and Navigation”). Consider, for example, the following code:
.
.
@StateObject var demoData : DemoData = DemoData()
.
.
NavigationLink(destination: SecondView(demoData)) {
Text("Next Screen")
}
In the above declaration, a navigation link is used to navigate to another view named SecondView, passing through a reference to the demoData observed object.
While this technique is acceptable for many situations, it can become complex when many views within an app need access to the same observed object...