SwiftUI 2: the way to open view in new window

前端 未结 3 2027
既然无缘
既然无缘 2021-02-08 09:23

Lets imagine that I have an App

var storeVM = BookStoreViewModel(bla1: bla1, bla2: bla2, bla3: bla3)

@SceneBuilder var body: some Scene {
    WindowGroup {
              


        
3条回答
  •  无人共我
    2021-02-08 09:43

    Try something like the following (just idea - cannot test)

    class BookViewModel: ObservableObject {} 
    
    class AppState: ObservableObject {
        @Published var selectedBook: BookViewModel?
    }
    
    @main
    struct SwiftUI2_MacApp: App {
        @StateObject var appState = AppState()
    
        @SceneBuilder 
        var body: some Scene {
            WindowGroup {         // main scene
                ContentView()
                  .environmentObject(appState)   // inject to update
                                                 // selected book
            }
    
            WindowGroup("Book Viewer") { // other scene
                if appState.selectedBook != nil {
                    Book(model: appState.selectedBook)
                }
            }
        }
    }
    

提交回复
热议问题