Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 367
Atimis Avatar asked Dec 06 '25 08:12

Atimis


1 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
}
like image 198
Asperi Avatar answered Dec 07 '25 22:12

Asperi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!