SwiftUI - When I create multiplatform app, how to change view in scene?

Viewed 842

I am trying to create a multi-platform app from SwiftUI.

Here is my code in in

@main
struct MyApp: App
@State var sceneManager = SceneManager.shared
    
    var body: some Scene {
        WindowGroup {
            
            if sceneManager.state == .landing {
                
                LandingPageView()
            } else if sceneManager.state == .historyRecord {
                
                HistoryRecordView()
            }
        }
    }

when my login did success, I will change SceneManager.shared property, from .landing to .historyRecord

but the view didn't change, how should I change root view of scene? Thanks

2 Answers

SwiftUI 2.0 introduced StateObject for such purpose, so go with the following pattern

@StateObject var sceneManager = SceneManager.shared

and make

class SceneManager: ObservableObject {
   @Published var state: StateTypeHere
}
Related