【问题标题】:EnvironmentVariables not working when passing variable from one view to another in SwiftUI在 SwiftUI 中将变量从一个视图传递到另一个视图时,EnvironmentVariables 不起作用
【发布时间】:2020-05-18 20:51:35
【问题描述】:

我发现了一些类似的例子,说明如何在 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

我正在尝试按照示例并使用 EnvironmentVariables 并修改它在 SceneDelegate 中首次定义的 ContentView。但是,在尝试这两个示例时,我收到错误“编译失败:'ContentView_Previews' 不是 'Environment' 的成员类型”。我正在使用 Xcode 版本 11.3.1。

按照How to pass variable from one view to another in SwiftUI 中给出的示例,以下是 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())
    }
}

...这里是 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) {
    }
}

【问题讨论】:

  • 我无法重现您的问题。它按预期工作(版本 11.3.1 (11C504))

标签: swiftui swiftui-environment


【解决方案1】:

我不依赖 Xcode 版本,这不是问题。您必须像在SceneDelegate 中一样在ContentView_Previews 中设置ContentView,提供.environmentObject,如下例所示

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

【讨论】:

  • 非常感谢!我尝试按照建议在ContentView_Previews 中设置ContentView,但我仍然收到相同的错误。我修改了帖子以包含结构的代码和SceneDelegate 以获取更多信息。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-27
  • 1970-01-01
  • 2022-11-22
  • 2019-11-18
  • 2019-03-24
  • 1970-01-01
相关资源
最近更新 更多