1

I have found a few similar examples of how to pass variables among multiple views in SwiftUI:

  1. Hacking with Swift - How to use @EnvironmentObject to share data between views
  2. How to pass variable from one view to another in SwiftUI

I am trying to follow the examples and use EnvironmentVariables and modify the ContentView where it's first defined in the SceneDelegate. However, when trying both examples, I get the error "Compiling failed: 'ContentView_Previews' is not a member type of 'Environment'". I am using Xcode Version 11.3.1.

Following the example given in How to pass variable from one view to another in SwiftUI, here is code contained in ContentView:

class SourceOfTruth: ObservableObject{
    @Published var count = 0
}

struct ContentView: View {
    @EnvironmentObject var truth: SourceOfTruth
    var body: some View {
        VStack {
            FirstView()
            SecondView()
        }
    }
}

struct FirstView: View {
    @EnvironmentObject var truth: SourceOfTruth
    var body: some View {
       VStack{
        Text("\(self.truth.count)")
           Button(action:
            {self.truth.count = self.truth.count-10})
           {
               Text("-")
           }
       }
    }
}

struct SecondView: View {
    @EnvironmentObject var truth: SourceOfTruth
    var body: some View {
        Button(action: {self.truth.count = 0}) {
            Text("Reset")
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView().environmentObject(SourceOfTruth())
    }
}

... and here is the contents of SceneDelegate:

import UIKit
import SwiftUI

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?
    var truth = SourceOfTruth() // <- Added

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    //    let contentView = ContentView()
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: ContentView().environmentObject(SourceOfTruth())) // <- Modified
            self.window = window
            window.makeKeyAndVisible()
        }
    }
    func sceneDidDisconnect(_ scene: UIScene) {
    }
    func sceneDidBecomeActive(_ scene: UIScene) {
    }
    func sceneWillResignActive(_ scene: UIScene) {
    }
    func sceneWillEnterForeground(_ scene: UIScene) {
    }
    func sceneDidEnterBackground(_ scene: UIScene) {
    }
}
Dan Segall
  • 11
  • 2

1 Answers1

1

I does not depend on Xcode version and it is not an issue. You have to set up ContentView in ContentView_Previews in the same way as you did in SceneDelegate, provide .environmentObject, as in below example

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView().environmentObject(_Your_object_here())
    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Thank you very much! I tried setting up `ContentView` in `ContentView_Previews` as suggested, but I'm still receiving the same error. I revised the post to include the code for the structs and `SceneDelegate` for additional information. – Dan Segall Feb 03 '20 at 19:56