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
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With