简体   繁体   中英

Is it possible to combine WindowGroup and DocumentGroup in a SwiftUI iOS app?

I am developing a SwiftUI iOS app which originally started out with A WindowGroup. In another view, I have decided that I need to present that view as a DocumentGroup scene to take advantage of all the features that come with it. I don't want the entry point into the app to be a DocumentGroup scene. I would just like the entry point into my App to be a WindowGroup scene, then in another view from the WindowGroup Scene, I would like to call the DocumentGroup scene to open as if the app was a document-based app. Any help will be much appreciated. Thanks

I tested in Xcode 13.3 and macOS 12.3 I noticed if WindowGroup is declared first then only that window opens and the file browser doesn't when launching the app and if closing the window and then clicking on the app's dock icon it opens a new window again. Unfortunately I noticed a bug that if you command+quit the app (rather than stopping it) on next relaunch it shows both the window and the file browser instead of just the window (I'll report that).

import SwiftUI

@main
struct DocumentTestApp: App {
    var body: some Scene {
        WindowGroup("Hello") { // group order matters
            VStack {
                Text("Hello")
             
                Button("New") {
                    NSDocumentController.shared.newDocument(nil)
                }
            }
            .padding()
        }
        }
        DocumentGroup(newDocument: DocumentTestDocument()) { file in
            ContentView(document: file.$document)
        }
        DocumentGroup(newDocument: DocumentTestDocument2()) { file in
            ContentView2(document: file.$document)
        }
        
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM