Post

Replies

Boosts

Views

Activity

Reply to How to create UIHostingController and pass environmentObject?
UPDATE: Ended up creating a wrapper view MyView_RUNTIME which has the runtime dependencies specified. This way in the #preview block you can instantiate MyView with your dummy environment variables, while in the UIHostingController you can instantiate the real ones. ie: // use within UIHostingController (ie: super.init( coder: aDecoder, rootView: DebugView_RUNTIME() )) struct MyView_RUNTIME : View { var body: some View { MyView().environment( \.database, ServiceLocator.database ) // NOTE: real DB registered during the bootstrapping of the runtime app } } // use within previews #Preview { MyView().environment( \.database, MyDatabase( inMemory: true )) // in-memory, dummy database }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Oct ’23
Reply to How to create UIHostingController and pass environmentObject?
How do you get this working with previews though? Presumable with previews you may want to pass in dummy environment objects/services: MyView().environment( \.database, database ) whereas in the runnable app you might want the hosting controller to pass along other services. Specifying it in the view definition itself doesn't allow you to pass it in from the outside.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Oct ’23