簡體   English   中英

如何在 swiftUI 中使用 EnvironmentObject 獲取原始 object 的價值

[英]How to get value on original object with EnvironmentObject in swiftUI

class GameSettings: ObservableObject {
    @Published var score = 0
    @Published var score1:Int? = 0
}


struct ScoreView: View {
    @EnvironmentObject var settings: GameSettings
    var body: some View {
        NavigationView {
            NavigationLink(destination: ContentView3()) {
                Text("Score: \(settings.score)")
            }
        }
    }
}

struct ContentView3: View {
    @StateObject var settings = GameSettings()
    @EnvironmentObject var settings111: GameSettings

    var body: some View {
        NavigationView {
            VStack {
                // A button that writes to the environment settings
                Text("Current Score--->\(settings.score))")
                Text(settings111.score1 == nil ? "nil" : "\(settings111.score1!)")
                Button("Increase Score") {
                    settings.score += 1
                }

                NavigationLink(destination: ScoreView()) {
                    Text("Show Detail View")
                }
            }
            .frame(height: 200)
        }
        .environmentObject(settings)
    }
}

因此,當用戶在ContentView3和導航路線中執行了一些更改時,如果用戶登陸到同一屏幕,即ContentView3 ,那么我怎樣才能獲得GameSettings object 的最新值呢? 我嘗試創建@EnvironmentObject var settings111: GameSettings但崩潰。

您是否也將.environmentObject()添加到 YourApp.swift 中?
如果沒有,您必須像這樣添加它

生命周期:SwiftUI

@main
struct YourApp: App {
    var settings: GameSettings = GameSettings()

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(settings)
        }
    }
}

生命周期:UIKit

在 SceneDelegate.swift

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).

        // Create the SwiftUI view that provides the window contents.
        let contentView = ContentView()
        var settings: GameSettings = GameSettings() // added line

        // Use a UIHostingController as window root view controller.
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView.environmentObject(settings))) // added ".environmentObject(settings)" after contentView
            self.window = window
            window.makeKeyAndVisible()
        }
    }

預習

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

在此處輸入圖像描述

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM